一个用户登录控制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;
}
}问题是我输入的用户名,口令等都和数据库能对上,但是check就是得不到1,登陆总是失败
请帮我看一下是什么地方的原因,如果好用的登录控制也请赐教!
<%@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;
}
}问题是我输入的用户名,口令等都和数据库能对上,但是check就是得不到1,登陆总是失败
请帮我看一下是什么地方的原因,如果好用的登录控制也请赐教!
会不会是==的问题?
还是应该是equals?
if(rs.getString("stf_posn")==posn&&rs.getString("stf_name")==name&&rs.getString("password")==psw)
应该为:
if(rs.getString("stf_posn").equals(posn)
&&rs.getString("stf_name").equals(name)
&&rs.getString("password").equals(psw))
应该用equals方法而不是“==”
应该用 equal
方法有21. 过滤器
2. Servlet+Bean+Jsp
select stf_posn,stf_name,password from where stf_posn=... and stf_name=... and stf_password=..
直接判断 rs.next(),何必那么复杂呢.
select stf_posn,stf_name,password from where stf_posn=... and stf_name=... and stf_password=..也试过了,无效
同样是数据库无法找到对应项,返回表是空表