西安网站建设哪家专业,求推荐专门做借条的网站,广东东莞石龙,wordpress登录后才允许浏览背景 HttpSession默认使用Cookie存储Session ID#xff0c;如果在用户禁用浏览器Cookie的功能后#xff0c;仍打算运用HttpSession来进行会话管理#xff0c;那么可以搭配URL重写来实现。 实现方法 使用HttpServletResponse的encodeURL()方法协助产生URL。 服务器端调用r…背景 HttpSession默认使用Cookie存储Session ID如果在用户禁用浏览器Cookie的功能后仍打算运用HttpSession来进行会话管理那么可以搭配URL重写来实现。 实现方法 使用HttpServletResponse的encodeURL()方法协助产生URL。 服务器端调用request.getSession()时 如果容器能从HTTP请求中取得带有Session ID的Cookie则response.encodeURL()会将传入的URL原封不动地输出。 如果容器不能从HTTP请求中取得带有Session ID的Cookie时通常是浏览器禁用Cookie的情况response.encodeURL()会自动产生带有Session ID的URL。 注意 在浏览器第一次请求网站时容器并不知道浏览器是否禁用了Cookie所以容器的做法是Cookie与URL重写两种方式并用来往客户端发送session Id。 url重写实现HttpSession可以实现跨浏览器的会话管理。在HttpSession存活期间只要有人取得当次的Session ID在另一浏览器相同的URL附上Session ID就可以取得同一个HttpSession对象。 重定向 HttpServletResponse上的encodeRedirectURL()方法可以在要求浏览器重定向时在URL上携带Session ID。 Demo package com.test;import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;WebServlet(/url.session)
public class URLOverrideHttpSession extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {response.setContentType(text/html;charsetUTF-8);PrintWriter out response.getWriter();/*** 实现url重写来实现session只需要两行代码* 1、获取session* 2、使用responce发送url** 注意这两行代码的顺序不可变*/HttpSession session request.getSession();out.println(a href response.encodeURL(url.session) 提交/a);out.close();}
} 测试方法 测试过程中注意观察地址栏URl 1、访问127.0.0.1:8080/url.session 2、点击“提交” 3、关闭浏览器的cookie功能再次测试转载于:https://www.cnblogs.com/Mike_Chang/p/10054557.html