我想在用户输入名称和密码后重定向 url,但是有问题。有人说add return
after forward(request,response)
,这个方法不行。
RequestDispatcher dispatcher; String username = request.getParameter("username"); String passwd = request.getParameter("passwd"); if(username != null && passwd != null){ try{ Database database = new Database("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/picshow","root","000000"); ResultSet rs = database.query("select * from ps_user where name=?", username); if(rs.next()){ if(rs.getString("passwd").equals(passwd)){ HttpSession session = request.getSession(); session.setAttribute("username", username); System.out.println("login successful"); dispatcher = request.getRequestDispatcher(request.getContextPath()+"/personalpage.jsp"); dispatcher.forward(request, response); return; }else { errMsg = "invalid password"; } }else { errMsg="user not exist"; } }catch(Exception e){ e.printStackTrace(); } if(errMsg != null && !errMsg.equals("")){ dispatcher = request.getRequestDispatcher(request.getContextPath()+"/login.jsp"); request.setAttribute("errMsg", errMsg); dispatcher.forward(request, response); return; } }else { dispatcher = request.getRequestDispatcher(request.getContextPath()+"/login.jsp"); dispatcher.forward(request, response); return; }