2

I want to redirect the url after user input name and password, but there are something wrong. some people say that add return after forward(request,response), and this method doesn't work.

    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;
    }
usar adman
  • 23
  • 5

1 Answers1

0

Instead create a String variable, set your destination based on your condition and finally forward it.

RequestDispatcher dispatcher;
String username = request.getParameter("username");
String passwd = request.getParameter("passwd");
String destination = "";//new targe variable
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");
                destination=request.getContextPath()+"/personalpage.jsp";//set your target
            }else {
                errMsg = "invalid password";
            }
        }else {
            errMsg="user not exist";
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    if(errMsg != null && !errMsg.equals("")){
        destination=request.getContextPath()+"/login.jsp";//set your target
        request.setAttribute("errMsg", errMsg);
    }
}else {
    destination=request.getRequestDispatcher(request.getContextPath()+"/login.jsp");//set 
}

dispatcher = request.getRequestDispatcher(destination);
dispatcher.forward(request, response);
Sas
  • 2,473
  • 6
  • 30
  • 47
  • Thank you for your answer, I've found the cause of the error. The reason is that I call the `forward` twice(before this piece of code). So this piece of code are correct.... – usar adman Nov 30 '14 at 16:41