如何在禁用cookie时通过requestDispatcher维护会话?

时间:2015-07-06 11:48:04

标签: servlets session-cookies session-management

enter image description here

当我启用了Cookie并且我从loggedIn到ShoppingCart执行requestDispatcher.forward(req,resp)时,'会话ID'和'用户名'被转发/保存。

但是当禁用cookie时,会创建一个新的“会话ID”,并且“用户名”为空。

我的问题是如何在禁用cookie时维护会话。


登录表单

<html>
<body>
    <form method="POST" action="login.do">
        username:<input type="text" name="username" /> 
        <input type="submit" value="login"/>
    </form>
</body>
</html>


loggedIn.jsp

<html>
<body>
Session Id : <%out.print(session.getId());%><br>
Logged In User: <%out.print(session.getAttribute("username"));%><br>

<form action="shopping.do" method="POST">
    <input type="submit" value="start shopping"/>
</form>

</body>
</html>


shoppingCart.jsp

<html>
<body>
Session Id : <%out.print(session.getId());%><br>
Logged In User: <%out.print(session.getAttribute("username"));%><br>
<h1>Shopping Cart</h1>
</body>
</html>


的web.xml

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <!--  LOGIN CONTROLLER -->
    <servlet>
        <servlet-name>LoginController</servlet-name>
        <servlet-class>com.example.controller.LoginController</servlet-class>       
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginController</servlet-name>
        <url-pattern>/login.do</url-pattern>
    </servlet-mapping>

    <!--  SHOPPING CONTROLLER -->
    <servlet>
        <servlet-name>ShoppingController</servlet-name>
        <servlet-class>com.example.controller.ShoppingController</servlet-class>        
    </servlet>
    <servlet-mapping>
        <servlet-name>ShoppingController</servlet-name>
        <url-pattern>/shopping.do</url-pattern>
    </servlet-mapping>

</web-app>


的LoginController

    package com.example.controller;

    import java.io.IOException;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    public class LoginController extends HttpServlet {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setContentType("text/html");

            HttpSession session = req.getSession();

            if(req.getParameter("username")!=null && !req.getParameter("username").isEmpty()){
                session.setAttribute("username",req.getParameter("username"));
            }

            String URL = ("loggedIn.jsp");
            String encodedURL=resp.encodeRedirectURL(URL);
            System.out.println(encodedURL);
            RequestDispatcher view =req.getRequestDispatcher(encodedURL);
            view.forward(req,resp);

        }

    }


ShoppingController

package com.example.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShoppingController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html");

        String URL = ("shoppingCart.jsp");
        RequestDispatcher view =req.getRequestDispatcher(URL);
        view.forward(req,resp);

    }

}

1 个答案:

答案 0 :(得分:1)

对于页面上的普通网址,您将使用response.encodeURL()。对于重定向,您应该使用response.encodeRedirectURL()

有关两者之间差异的其他信息,请参阅this SO post