注册功能实现了,也存进数据库了,但跳转到登录页面就无法成功登录,一直跳到登录不成功的页面。这是为啥?
//链接数据库的方法
public class Conn {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
public Connection getConn(){
try{
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest","root","123456");
st = conn.createStatement();
}catch(Exception e){
e.printStackTrace();;
}
return conn;
}
// 处理登录的方法
public String loginMethod(){
try{
String sql1 = "select * from user where username=' "+getUsername()+" ' and password=' "+getPassword()+" '";
PreparedStatement pst=conn.getConn().prepareStatement(sql1);
ResultSet rs=pst.executeQuery();
if(rs.next()){
ActionContext.getContext().getSession().put("user",username );
return "loginin";
}else{
this.addFieldError("error", "用户名或密码错误!!");
return "loginout";
}
}catch(Exception e){
e.printStackTrace();
return "loginout";
}
// stuuts.xml配置
UserLogin.jsp
WelcomeLo.jsp
// UserLogin.jsp页面
/s:fielderror
请您输入用户名与密码
WelcomeLo.jsp页面
欢迎${sessionScope.user},您已经登录!