复杂的Servlet关系......有可能吗?

时间:2013-09-12 14:08:15

标签: java jsp servlets parameter-passing

我想知道如何在2个servlet之间传递参数。但它有点不同......让我更具体一点。我有一个登录页面和DB中的用户及其密码。如果经理创建的用户没有密码。用户只输入用户名并单击“登录”,然后将出现creationpassword页面。然后,刚创建密码的用户将自动成功登录。 (这是事情。我不希望用户重新输入他的用户名和密码“刚刚创建”再次登录。)当他完成创建密码后,他可以被重定向到所需的页面。但是pageforwards servlet是第一个servlet。让我更清楚一点。

我的servlet是LoginServlet(由loginPage.jsp操作),CreatePassServlet(从createPass.jsp操作)。

当用户输入他的用户名时,LoginServlet检查他的密码(它是否存在),如果没有使用用户名转发给createPass.jsp页面,那么用户输入他的密码然后CreatePassServlet创建他的密码并存储在DB中然后返回他的LoginServlet的用户名和密码信息(指向他所需的页面)

这是我的课程......

LoginServlet

public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public LoginServlet() {
    super();
}

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    try {
        int pEmployeeId;
        String pName;
        String pSurname;
        String pUserName;
        String pUserType;
        String query;
        String home = "/login.jsp";
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        RequestDispatcher rd = request.getRequestDispatcher(home);
        mysqlCon con = new mysqlCon();
        LoginService ls = new LoginService();


        if((password==null) || (password=="")){

            Statement stmt = con.getConnection().createStatement();
            query = "SELECT password FROM employee WHERE username='"
                    + username + "';";
            stmt.executeQuery(query);
            ResultSet rs = stmt.getResultSet();
            if(rs.next()){

                String nextJSP = "createPassword.jsp";
                rd = request.getRequestDispatcher(nextJSP);
                rd.forward(request, response);      

                return;
            }

        }


        String getHashedPass = Md5HashPassword.Md5(password);

        Statement stmt = con.getConnection().createStatement();
        query = "SELECT employee_id, name, surname, usertype, username FROM employee WHERE username='"
                + username + "' AND password='" + getHashedPass + "';";
        stmt.executeQuery(query);
        ResultSet rs = stmt.getResultSet();

        if (rs.next()) {
            pEmployeeId = rs.getInt(1);
            pName = rs.getString(2);
            pSurname = rs.getString(3);
            pUserType = rs.getString(4);
            pUserName = rs.getString(5);

            if (ls.loginCheck(username, getHashedPass) != false) {
                Person tmp = new Person();

                tmp.setName(pName);
                tmp.setSurname(pSurname);
                tmp.setType(pUserType);
                tmp.setUsername(pUserName);
                tmp.setId((long) pEmployeeId);
                HttpSession session = request.getSession();
                session.setAttribute("person", tmp);

                if (pUserType.equals(UserType.MANAGER.toString())) {

                    String nextJSP = "managerHome.jsp";
                    rd = request.getRequestDispatcher(nextJSP);
                    rd.forward(request, response);
                }

                else if (pUserType.equals(UserType.ADMIN.toString())) {

                    String nextJSP = "adminHome.jsp";
                    rd = request.getRequestDispatcher(nextJSP);
                    rd.forward(request, response);

                }

                else if (pUserType.equals(UserType.USER.toString())) {

                    String nextJSP = "userHome.jsp";
                    rd = request.getRequestDispatcher(nextJSP);
                    rd.forward(request, response);
                } else {

                    String nextJSP = "login.jsp";
                    rd = request.getRequestDispatcher(nextJSP);
                    rd.forward(request, response);
                }

            }
        } else {
            request.setAttribute("errorMessage",
                    "Login Failed. Your username or password is incorrect. ");
            rd.forward(request, response);

        }

        con.getConnection().close();

    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

}

}

... CreatePassServlet

public class CreatePassServlet extends HttpServlet {
private static final long serialVersionUID = 1L;


public CreatePassServlet() {
    super();    
}




protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    mysqlCon con=new mysqlCon();
    String password=request.getParameter("password");
    String passwordcheck=request.getParameter("passwordcheck");

    if(password.equals(passwordcheck)){

        try {
            String query;
            String hashedPass = Md5HashPassword.Md5(password);      
            String username=request.getParameter("username");

            Statement stmt = con.getConnection().createStatement();
            query = "UPDATE employee SET password='"+ hashedPass +"' WHERE username='"+ username +"';";
            stmt.executeUpdate(query);

            request.setAttribute("username", username);
            ServletContext sc = getServletContext();  
            RequestDispatcher rd = sc.getRequestDispatcher("/LoginServlet");  
            rd.forward(request, response);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    else{
        RequestDispatcher rd=request.getRequestDispatcher("createPassword.jsp");
        request.setAttribute("errorMessage",
                "Creation failed. Your passwords did not match, please try again. ");
        rd.forward(request, response);
    }






}





protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

}

页面是正确的...当我运行它时,我得到了这个......

当我尝试支持loginservlet继续时,CreatePassServlet将Username重定向为NULL和我的密码。我想重点是我不能再将我的用户名传递回LoginServlet。然后我从loginServlet获取登录用户名 - 密码错误错误,因为username是NULL。

请帮帮我。提前谢谢......

2 个答案:

答案 0 :(得分:0)

我刚刚看到你可能没有在请求对象中设置hashedPass

您可以在许多地方保存信息。一个这样的地方就是你的httpsession。

session.setAttribute("newPassword", param);

在Servlet中,您有3个可以存储数据的范围。

  

申请

     

会话

     

请求

此外,JSP具有page范围来保存数据。

请参阅http://www.fcet.staffs.ac.uk/gdm1/general/java/web/servlets/scopes.asp以快速了解如何使用这些范围保存数据并传递给其他servlet。

答案 1 :(得分:0)

如果我正确理解了您的代码,当您在 CreatePassServlet之后执行LoginServlet 时,您必须使用request.getAttribute(“username”)来查找用户名.toString();

尝试类似:

        String username = (request.getAttribute("username")==null)?
request.getParameter("b"):request.getAttribute("username").toString();

而不是:

        String username = request.getParameter("username");