当前位置: 代码迷 >> Java Web开发 >> 这个代码click如何写才能执行下面的代码
  详细解决方案

这个代码click如何写才能执行下面的代码

热度:208   发布时间:2016-04-16 21:51:29.0
这个代码click怎么写才能执行下面的代码
<%@ page language="java"  pageEncoding="utf-8"%>
<%@ page import="com.xieyj.*"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <title>查询信息</title>
  </head>
  
  <body>
  <h1 >请输入账号密码!</h1>
   <p><strong>账  号:</strong> <input id="LoginID" name="LoginID" type="text" size="8" maxlength="20" class="textarea-2" value=""/></p>
   <p><strong>密  码:</strong> <input id="Passwd" name="Passwd" type="password" size="8" maxlength="20" class="textarea-2" value=""/> </p>
   <p><input type="button" value="注  册" class="btn-2" onclick="???"/> </p>
   
         
   <%
   String loginID = "LoginID"; 
   String passwd = "Passwd"; 
   User user = new User(); 
   user.setName(loginID);
   user.setPasswd(passwd);
   Connection con = DBcon.getCon();   
   try {
   Statement stmt = (Statement)con.createStatement();
   ResultSet rs = stmt.executeQuery(" SELECT * FROM T_LOGIN where LoginID = '"+user.getName().toString()+"'");
   out.println("查询结果如下:");
   if (rs.next()) {
         out.println("账号:"+rs.getString("LoginID")+"    密码:"+rs.getString("Passwd")+"    手机:"+rs.getString("Phone")+"    邮箱:"+rs.getString("Email"));
       } else {
       out.println("<script>alert('用户不存在!')</script>");
       }
       
       
       DBcon.closeAllMethod(con,stmt,rs);
       } catch (Exception e) {
       e.printStackTrace();
       } 
    %>
     
  </body>
</html>

------解决思路----------------------
这么写代码累吗?
 <p><input type="button" value="注  册" class="btn-2" onclick="return  beforeSubmit()"/ > </p>
<script  type="">
         function beforeSubmit(){
      //业务代码(请求)
     //返回false,不提交,true,表单提交
 }
</script>
------解决思路----------------------
把代码放在这个if块中就可执行:

if("post".equalsIgnoreCase(request.getMethod())){
//全部代码
}

注意:
html代码中,input控件要放在form中(楼主代码中未发现form),并且要将form标签method属性="post",action属性就不要写,这样默认post给自己。
------解决思路----------------------
onclick方法只需负责校验数据和提交表单就行了。
引用:
把代码放在这个if块中就可执行:

if("post".equalsIgnoreCase(request.getMethod())){
//全部代码
}

注意:
html代码中,input控件要放在form中(楼主代码中未发现form),并且要将form标签method属性="post",action属性就不要写,这样默认post给自己。

------解决思路----------------------
可以用onclick做一层校验工作,但Java代码里也需要做校验。
另外你的java代码要修正:

//String loginID = "LoginID"; 
//String passwd = "Passwd"; 
String loginID = request.getParameter("LoginID"); 
String passwd = request.getParameter("Passwd"); 

//Connection con = DBcon.getCon(); 
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
con = DBcon.getCon(); 
stmt = ...
rs = ...
...

//最后加上finally{
//执行各种关闭方法
//}

好好学吧,骚年.....

引用:
Quote: 引用:

把代码放在这个if块中就可执行:

if("post".equalsIgnoreCase(request.getMethod())){
//全部代码
}

注意:
html代码中,input控件要放在form中(楼主代码中未发现form),并且要将form标签method属性="post",action属性就不要写,这样默认post给自己。

这个的话加了<form>,onclick还需要吗,要的话里面该怎么写呢
  相关解决方案