2

第一次在这里发帖,我希望这是一个有效的问题。我一直在构建一个基本的 Java servlet,它从页面上的表单中接受 3 个名称/值对,将它们设置为 1. 请求属性 2. 会话属性和 3. cookie 属性。然后将 Cookie 添加到响应中,然后转发视图 (AccountSettings.jsp)。AccountSettings 页面应该使用 request.getCookies() 将它们转储到数组中,然后从数组中读取值。每次我使用此表格时,所有这些都应该发生。

我的问题是 cookie 值仅在我第一次使用表单时正确,然后每次我再次使用表单时,cookie 都会显示页面加载时输入的最后一个值。但是,如果我刷新页面,cookie 值将正确显示。我尝试手动删除 Logout servlet 中的 cookie(setMaxAge(0) 然后重新添加到响应),但这只会在索引 1 处产生一个常量 ArrayOutOfBoundsException,所以我将这部分注释掉并单独留下 cookie。

显示页面后,我在 Chrome 中检查了与 localhost 关联的 cookie,并且值设置正确,因此在我看来,JSP 是在实际正确设置 cookie 之前绘制的。

任何有关如何解决此问题的帮助将不胜感激。这是我的代码。

小服务程序:

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

public Login() {

    super();

}

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

    login(request, response);

}


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

    login(request, response);

}


private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    // get a new or existing session
    HttpSession session = request.getSession();

    // Instantiate user and populate values
    User user = new User();
    user.setUser(request.getParameter("user"));
    user.setPass(request.getParameter("pass"));

    // Get last page
    String referringUrl = request.getParameter("referringPage");

    session.setAttribute("user", user.getUser());
    session.setAttribute("pass", user.getPass());
    session.setAttribute("lastPage", referringUrl);

    Cookie cookie1 = new Cookie("user", user.getUser());
    Cookie cookie2 = new Cookie("pass", user.getPass());
    Cookie cookie3 = new Cookie("lastPage", referringUrl);

    response.addCookie(cookie1);
    response.addCookie(cookie2);
    response.addCookie(cookie3);

    request.setAttribute("user", user.getUser());
    request.setAttribute("pass", user.getPass());
    request.setAttribute("lastPage", referringUrl);

    try{

        if (user.authorize()){

            session.setAttribute("name", user.getName());
            session.setAttribute("authorized", "1");

        }else{

            session.setAttribute("authorized", "0");

        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

    RequestDispatcher view = request.getRequestDispatcher("AccountSettings.jsp");
    view.forward(request, response);

    user.destroy();

}

}

看法:

<div id="content">
        <div class="padding">

            <%
                if (!loggedIn){
                    out.print(
                        "Oops! I'm not sure how you got here..."
                    );
                }else{
                    Cookie[] cookies = request.getCookies();

                    out.print(
                        "<h2>Account Settings</h2><br><br>" +
                        "<table>" +
                            "<tr>" +
                                "<th>Source</th>" +
                                "<th>Username</th>" +
                                "<th>Password</th>" +
                                "<th>Last Page Visted</th>" +
                            "</tr>" +
                            "<tr>" +
                                "<th>Request</th>" +
                                "<td>" + request.getAttribute("user") + "</td>" +
                                "<td>" + request.getAttribute("pass") + "</td>" +
                                "<td>" + request.getAttribute("lastPage") + "</td>" +
                            "</tr>" +
                            "<tr>" +
                                "<th>Session</th>" +
                                "<td>" + session.getAttribute("user") + "</td>" +
                                "<td>" + session.getAttribute("pass") + "</td>" +
                                "<td>" + session.getAttribute("lastPage") + "</td>" +
                            "</tr>" +
                            "<tr>" +
                                "<th>Cookies</th>" +
                                "<td>" + cookies[1].getValue() + "</td>" +
                                "<td>" + cookies[2].getValue() + "</td>" +
                                "<td>" + cookies[3].getValue() + "</td>" +
                            "</tr>" +
                        "</table>"
                    );

                }
            %>

        </div>
    </div>
4

1 回答 1

3

所以在我看来,JSP 是在 cookie 实际设置正确之前绘制的

这是正确的。您正在向响应中添加新的 cookie(因此它们仅在相同域和路径上的后续请求中可用),但您的 JSP 正在尝试从当前请求中读取 cookie。

您需要通过替换发送重定向而不是转发

RequestDispatcher view = request.getRequestDispatcher("AccountSettings.jsp");
view.forward(request, response);

经过

response.sendRedirect("AccountSettings.jsp");

或者将 cookie 值复制为请求属性,以便 JSP 可以将它们作为请求属性获取(您已经知道如何做到这一点)。


与具体问题无关,在 cookie 中传递密码是一个非常糟糕的主意。这是一个巨大的安全漏洞。对于您的具体功能要求,您最好将登录用户存储为会话属性。

于 2012-10-19T14:15:51.730 回答