当前位置: 代码迷 >> Java Web开发 >> 用JSP+tomcat4+SQLserver做登陆模块时的有关问题
  详细解决方案

用JSP+tomcat4+SQLserver做登陆模块时的有关问题

热度:196   发布时间:2016-04-17 13:54:51.0
用JSP+tomcat4+SQLserver做登陆模块时的问题
我的index模块:
<FORM   ACTION= "login.jsp "   METHOD= "POST ">

<table   border= "0 "   bgcolor= "#F2EDF8 ">
    <tr   bgcolor= "#EAE2F3 ">
        <td   bgcolor= "#E2D8EF "> 学号: </TD> <TD> <input   NAME= "user "   TYPE= "text "   SIZE= "12 "   maxlength= "12 "> </TD>
    </tr>
    <tr   bgcolor= "#EAE2F3 ">
        <td   bgcolor= "#E2D8EF "> 口令: </TD> <TD> <input   NAME= "password "   TYPE= "password "   SIZE= "10 "   maxlength= "10 "> </TD>
    </tr>
</table>


login.jsp的代码:
<%@   page   contentType= "text/html;charset=gb2312 "   %>
<%@   page   import= "grad.util.user.*; "%>
<%@   page   session= "true "   %>
<html>
<head>
<title> login </title>
</head>
<body   bgcolor= "#ffffff ">
<h1>
<%
  String   password=request.getParameter( "password ");
  String   usercode=request.getParameter( "user ");
//out.print(password+usercode);
  op_user   ope=new   op_user();//构造用户管理对象
if(ope.isLogin(usercode,password)){//调用isLogin方法判断是否允许用户登录
  user   us=ope.getUserByUserCode(usercode);//允许用户登录则取得用户信息
    session.setAttribute( "usercode ",usercode);//把用户编号写入session
    response.sendRedirect( "student/index.html ");  
}
else{
out.print( " <script   language=\ "javascript\ "> alert(\ "请重新登录\ ") </script> ");
response.sendRedirect( "index.jsp ");
}
%>
</h1>
</body>
</html>


定义的user类:
package   grad.util.user;

public   class   user   {
public   user   (){
}
String   id= " ";       //用户名
String   password= " ";       //用户密码
public   void   setId(String   id)
{
this.id=id;
}
public   String   getId()
{
return   this.id;
}
public   void   setPassword(String   password)
{
this.password=password;
}
public   String   getPassword()
{
return   this.password;
}

}

登陆验证时用到的两个方法:
package   grad.util.user;

import   grad.util.DataBase;
import   java.sql.*;
import   java.util.*;


public   class   op_user{
public   op_user(){
}
public   user   getUserByUserCode(String   usercode)
{
Connection   conn=null;
Statement   st=null;
ResultSet   rs=null;
String   sql= "select   *   from   user   where   id= ' "+usercode+ " ' ";
try
{
DataBase   ds   =new   DataBase();
conn   =   ds.conn;
user   us=new   user();
rs   =   st.executeQuery(sql);                      
  相关解决方案