javaBean验证出错 - Web 开发 / 其他
public class CheckLogin { public String checkLogin(String username,String userPassword){ BaseConn conn=null; //已定义好数据库连接 String index=""; try{ conn=new BaseConn(); String sql="select * from userInfo where nickname=?"; PreparedStatement ps=conn.preparedStatement(sql); ps.setString(1, username); ResultSet rs=conn.executeQuery(); if(rs.next()){ if(rs.getString("userPassword").equals(userPassword)){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ); String time=sdf.format(new java.util.Date());//登录时间 sql="update userInfo set lastLoginTime=? where nickname=?"; ps=conn.preparedStatement(sql); ps.setString(1, time); ps.setString(2, username); conn.executeUpdate(); return index="succes"; } else{ return index="wrongpassword"; } } else{ return index="no"; } } catch(Exception e){ e.printStackTrace(); return index; } finally{ conn.close(); } }
<jsp:useBean id="check" class="mytalk.CheckLogin"/> <body> <% request.setCharacterEncoding("gb2312"); String nickname=request.getParameter("nickname"); <!--用户的输入名--> String userPassword=request.getParameter("userPassword");<!--密码--> String loginMsg=check.checkLogin(nickname,userPassword); if(loginMsg.equals("success")) { session.setAttribute("user",nickname); session.setAttribute("login","success"); response.sendRedirect("chatRoomList.jsp"); } else if(loginMsg.equals("wrongpassword")){ out.println("你输入的用户名或密码错误,请核实后重新输入"); if(session.getAttribute("login")!=null) session.removeAttribute("login"); } else if(loginMsg.equals("no")){ check.saveToDataBase(nickname,userPassword); session.setAttribute("user",nickname); session.setAttribute("login","success"); response.sendRedirect("chatRoomList.jsp"); } else{ out.println("程序出错!"); } %> </body>