企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://box.kancloud.cn/44537ef796e6a49c6421fd9186276df6_468x371.png) web.xml中配置,session的计算是从停止操作session资源开始,包括获取,保存等 ~~~ <session-config> <session-timeout>10</session-timeout> </session-config> ~~~ ![](https://box.kancloud.cn/2d3a8580db14f37c948e973333d21a9c_634x286.png) ![](https://box.kancloud.cn/fad105802ebffea59697a176a4602937_465x129.png) 持久化:创建一个cookie覆盖之前服务器自动生成的JSESSIONID,设置持久化时间 GetSessionServlet.java ~~~ package zyw.session; import javax.servlet.ServletException; 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; @WebServlet(name = "GetSessionServlet",urlPatterns = "/getsession") public class GetSessionServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取session HttpSession session = request.getSession(); String goods = (String)session.getAttribute("goods");//Ctrl + Alt + v response.getWriter().write(goods+"");//加“”防止空指针 } } ~~~ SaveSessionServlet.java ~~~ package zyw.session; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; @WebServlet(name = "SaveSessionServlet",urlPatterns = "/savesession") public class SaveSessionServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); //创建一个cookie覆盖之前服务器自动生成的JSESSIONID Cookie cookie = new Cookie("JSESSIONID", session.getId()); cookie.setMaxAge(60*60); response.addCookie(cookie); session.setAttribute("goods","cup"); } } ~~~