简体   繁体   English

嗨,我正在为登录表单做下面的Java代码,它没有将我重定向到employee.jsp或customer jsp页面

[英]Hi, I am doing the java code found below, for a login form.It is not redirecting me to the employee.jsp or customer jsp page

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

    try {
        user user = new user();
        user.setUsername(request.getParameter("username"));
        user.setPassword(request.getParameter("password"));
        user = loginBean.Login(user);
        if (user.getMessage() == null || user.getMessage().equals("")) {
            request.setAttribute("message", user.getMessage());
        }
        if (user.getRole().equals("Employee")) {
            request.getRequestDispatcher("/employee.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("/customer.jsp").forward(request, response);
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }

}

What you are trying to do is after a successful login redirect the user to employee.jsp or customer.jsp depending on their role. 您想要做的是在成功登录后根据用户角色将用户重定向到employee.jsp或customer.jsp。

You currently forward the request on to employee.jsp or customer.jsp along with all the parameters (username, password and message and anything else). 当前,您将请求连同所有参数(用户名,密码和消息以及其他任何内容)一起转发到employee.jsp或customer.jsp。

What you need to do is redirect the user to the new page like this: 您需要做的是将用户重定向到新页面,如下所示:

if (user.getRole().equals("Employee")) {
    response.sendRedirect("/employee.jsp");
} else {
    response.sendRedirect("/customer.jsp");
}

Make Sure that u had these pages in your project directory . 确保您的项目目录中有这些页面。 If they are not there you get error message. 如果它们不存在,您会收到错误消息。 your code is right 您的代码是正确的

 if (user.getRole().equals("Employee")) {
response.sendRedirect("/employee.jsp");} else {
response.sendRedirect("/customer.jsp");}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM