🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
![](https://box.kancloud.cn/54751cda7be938b48326e64d2352ba9f_458x364.png) SaveSessionServlet.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 = "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(); session.setAttribute("goods","cup"); } } ~~~ 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,再执行GetSessionServlet.java ![](https://box.kancloud.cn/132a84d386b0fd95d0ad95d4c135303d_543x109.png)