登录后的URL映射

时间:2013-03-19 22:42:04

标签: java jsp servlets

我有一个带有URL映射“/ Login”的登录servlet,它管理用户输入和登录过程。但是,当用户登录时,网站将定向到URL:

http://localhost:8080/pilot_1/Login

而不是

http://localhost:8080/pilot_1/checklistallitem

值得一提的是,第一个URL工作正常,它显示了所有数据,但我不确定为什么URL没有按照需要显示。这是Login Servlet的doPost方法。

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

    String username = req.getParameter("j_username");
    String password = req.getParameter("j_password");

    if (users.containsKey(username)){
        if ( users.get(username).equals(password)){
            req.getSession().setAttribute("active_window", "Checklist");
            req.getSession().setAttribute("current_team", "allteams");

            getServletContext().getRequestDispatcher("/checklistallteam").forward(req, resp);

        }else{
            JOptionPane.showMessageDialog(null, "Invalid ID or Password");
        }
    }else{
        JOptionPane.showMessageDialog(null, "Invalid ID or Password");
    }
}

1 个答案:

答案 0 :(得分:0)

这是重定向和转发之间的区别。当您使用调度程序转发请求时,解析和处理在服务器端发生,然后最终响应将返回给调用客户端。另一方面,当您发出重定向时,会有一个对客户端的中间响应,基本上告诉它 - “调用此其他URL来完成请求”。作为副作用,客户端将知道新资源的URL,并将更新位置栏以反映它。

由于转发完全在服务器端处理,因此客户端位置栏中的URL不会更改。