我的目的是当数据库中由此用户信息且登录成功的时候,跳到
showinfo.jsp这个页面,但是现在数据库中明明有数据但它还是会跳到
errorpage.jsp。个人感觉是因为没有查询到数据库的数据,但不知道具体怎么改!
- Java code
public class LoginServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private DataBase database = null; //初始化函数 public void init() throws ServletException { database = new DataBase(); } //处理get请求方法 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } //处理post请求方法 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //获取用户当前会话 String username = ""; String password = ""; String page = null; if(request.getParameter("txtUserName") != null) username = request.getParameter("txtUserName"); //获取用户名 if(request.getParameter("txtPassword") != null) password = request.getParameter("txtPassword"); //获取密码 //判断用户名、密码是否匹配 boolean temp = false; //System.out.println(username + ", " + password); Connection con = database.getConnection(); temp = DBUtilitya.isValid(con, username, password); //关闭数据库连接 database.closeConnection(con); if(temp){ InfoItem infoitem = null; Connection con2 = database.getConnection(); infoitem = DBUtilitya.getUser(con2, username); database.closeConnection(con2); //将用户信息对象InfoItem存储在session对象当中 session.setAttribute("infoitem",infoitem); //如果用户通过验证,则返回用户有关信息 page = "showinfo.jsp"; } else{ //如果验证失败,则显示验证失败页面 page = "errorpage.jsp"; } //重定向到相应的页面 response.sendRedirect(page); return; }}
- Java code
public class DBUtilitya { //判断用户名、密码是否正确 public static boolean isValid(Connection con, String username, String userpwd){ PreparedStatement prepStmt = null; ResultSet rs = null; //存储查询结果 String selectStatement = "select * from userinfo1 where username='" +username+"' and userpwd = '" +userpwd+"' "; boolean temp = false; try{ prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1,username); prepStmt.setString(2,userpwd); rs = prepStmt.executeQuery(); //查询数据库并返回结果记录集 if (rs.next()) //数据库里含有此用户名、密码对则返回true temp = true; else temp = false; //否则返回false,用户名、密码不匹配 rs.close(); prepStmt.close(); }catch(Exception e){ e.printStackTrace(); } return temp; //返回验证结果 } //根据用户名从数据库中获取用户的注册信息 public static InfoItem getUser(Connection con, String username){ PreparedStatement prepStmt = null; ResultSet rs = null; //结果记录集合 InfoItem infoitem = null; String queryString = "select username,roomnumber1,roomnumber2, roomnumber3, roomnumber4 from userinfo1 where username='" +username+"'"; try{ prepStmt = con.prepareStatement(queryString); prepStmt.setString(1, username); rs = prepStmt.executeQuery(); //查询数据库并返回结果记录集合 if(rs.next()) { //数据库里含有此用户名、密码对则返回InfoItem对象 infoitem = new InfoItem(rs.getString("username"), rs.getString("roomnumber1"), rs.getString("roomnumber2"), rs.getString("roomnumber3"), rs.getString("roomnumber4") ); } else { //否则返回null infoitem = null; } rs.close(); prepStmt.close(); }catch(Exception e){ e.printStackTrace(); } return infoitem; }