服务器端获取Session对象依赖于客户端携带的Cookie中的JSESSIONID数据。如果用户把浏览器的隐私级别调到最高,这时浏览器是不会接受Cookie、这样导致永远在服务器端都拿不到的JSESSIONID信息。这样就导致服务器端的Session使用不了。
Java针对Cookie禁用,给出了解决方案,依然可以保证JSESSIONID的传输。
Java中给出了再所有的路径的后面拼接JSESSIONID信息。
1 在 Session1Servlet中,使用response.encodeURL(url) 对超链接路径拼接 session的唯一标识
[Java] 纯文本查看 复制代码
?
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 | @WebServlet ( "/sessionDemo5" ) public class SessionDemo5 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); System.out.println(session); System.out.println(session.getId()); response.setContentType( "text/html;charset=utf-8" ); String path = response.encodeURL( "/day16/sessionDemo6" ); System.out.println(path); response.getWriter().print( "<a href='" + path + "'>点击</a>" ); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this .doPost(request, response); } } |
在response对象中的提供的encodeURL方法它只能对页面上的超链接或者是form表单中的action中的路径进行重写(拼接JSESSIONID)。
如果我们使用的重定向技术,这时必须使用encodeRedirectURL完成