一个用户登录控制
jsp页面:
<%@page contentType= "text/html;charset=gbk "%>
<%@page import= "login.* "%>
<% request.setCharacterEncoding( "gbk ");
String posn=request.getParameter( "userposn ");//接收表单传过来的数据
String name=request.getParameter( "username ");
String psw=request.getParameter( "psw ");
login.Ckecklog checklog=new login.Ckecklog(posn,name,psw);
if(checklog.getCheck()==checklog.getCknum()){
response.sendRedirect( "connect.jsp ");
}
else{
out.println( "登陆失败 ");
}
%>
控制类
package login;
import condb.*;
import java.io.UnsupportedEncodingException;
import java.sql.*;
public class Ckecklog {
//变量申明
private String posn;
private String name;
private String psw;
private String kan;
private int check=0;
private int cknum=1;
//用户登陆验证
public Ckecklog(String userposn,String username,String psw) throws SQLException, ClassNotFoundException {
this.posn=userposn;
this.name=username;
this.psw=psw;
condb.Connect lianjie=new condb.Connect();//这是连接数据库的,没有问题
Statement stmt=(Statement) lianjie.getStmt();
ResultSet rs=stmt.executeQuery( "select stf_posn,stf_name,password from kc_starig ");
while(rs.next()){
if(rs.getString( "stf_posn ")==posn&&rs.getString( "stf_name ")==name&&rs.getString( "password ")==psw){
check=1;
break;
}//这里是判断页面传过来的用户名、口令是否和数据库的一样就把check设为1
}
}
public int getCheck(){
return check;
}
public int getCknum(){
return cknum;
}
}