ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
![](https://box.kancloud.cn/6b5b25add82de8fcba8c9514fcf01720_519x353.png) ServletTest1.java ~~~ package zyw.servlet; 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 java.io.IOException; @WebServlet(name = "ServletTest1",urlPatterns = "/test1") public class ServletTest1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data="xiaoming"; this.getServletContext().setAttribute("name",data); } } ~~~ ServletTest2.java ~~~ package zyw.servlet; 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 java.io.IOException; @WebServlet(name = "ServletTest2",urlPatterns = "/test2") public class ServletTest2 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name =(String) this.getServletContext().getAttribute("name"); System.out.println(name); } } ~~~ 先访问ServletTest1.java,再访问ServletTest2.java,结果 ![](https://box.kancloud.cn/3025fdb3005b3c2a7a23ad10b0897bfb_229x123.png)