企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://box.kancloud.cn/5d087e5eb418418736661505f4ab3a2a_465x285.png) ![](https://box.kancloud.cn/3501c0a6be4bb11c2da6cea76eb70c56_750x446.png) ![](https://box.kancloud.cn/8a2b987ccda767d0b7a117eb2282e440_470x195.png) Session对象的创建和获取 SessionServlet.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 = "SessionServlet",urlPatterns = "/session") public class SessionServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //第一次访问时没有session,服务器会自动创建一个session对象 //之后再次访问的时候已经存在了session对象,这个直接获取这个对象 HttpSession session=request.getSession(); String sessionId=session.getId(); response.getWriter().write("JESSIONID"+sessionId); } } ~~~ ![](https://box.kancloud.cn/0103f9b5ead0eb0fd2068f963a4a2daf_372x68.png)