| PageContext: 当我们访问不同的servlet时,可以获取该对象,当访问其他的servlet,即切换不同的页面时,就会销毁该对象 |
| ServletRequest:当我们发送不同的请求时,可以获取该对象,访问其他请求时,则该对象销毁 |
| HttpSession: 当我们打开某个网站时,只要该会话没有过期,则可以随时从该对象中获取数据 |
| ServletContext: 当tomcat服务器启动后该对象就一直存在,且所有的servlet都可以从该作用域中获取值 |
-
构建一个web项目,导入依赖



-
之后查看导入的jar包

-
web.xml中添加如下
| <context-param> |
| <param-name>url</param-name> |
| <param-value>xdclass.net</param-value> |
| </context-param> |
| |
| <context-param> |
| <param-name>username</param-name> |
| <param-value>jack</param-value> |
| </context-param> |
- 编写UserServlet: 先获取web.xml中数据,再向servletContext中添加数据
| @WebServlet(name = "userServlet",urlPatterns = {"/user"}) |
| public class UserServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| resp.setContentType("text/html;charset=utf-8"); |
| PrintWriter printWriter = resp.getWriter(); |
| printWriter.write("<div> doGet </div>"); |
| |
| ServletContext servletContext = req.getServletContext(); |
| String url = servletContext.getInitParameter("url"); |
| System.out.println(url); |
| |
| servletContext.setAttribute("appInfo","xdclass.net info"); |
| } |
| |
| @Override |
| protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| resp.setContentType("text/html;charset=utf-8"); |
| PrintWriter printWriter = resp.getWriter(); |
| printWriter.write("<div> doPost </div>"); |
| } |
| |
| } |
- 编写TestServlet: 获取在UserServlet中向servletContext中添加的数据
| @WebServlet(name = "testServlet",urlPatterns = {"/test"}) |
| public class TestServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException { |
| resp.setContentType("text/html;charset=utf-8"); |
| PrintWriter printWriter = resp.getWriter(); |
| printWriter.write("<div> TestServlet doGet </div>"); |
| |
| ServletContext servletContext = req.getServletContext(); |
| String appInfo = (String)servletContext.getAttribute("appInfo"); |
| System.out.println("TestServlet appInfo="+appInfo); |
| } |
| |
| } |
- 测试先访问testServlet: servletContext中没有数据
| [2022-04-12 09:27:28,239] Artifact web02:war exploded: Artifact is deployed successfully |
| [2022-04-12 09:27:28,239] Artifact web02:war exploded: Deploy took 502 milliseconds |
| TestServlet appInfo=null |
- 再访问userServlet: 获取到web.xml中的数据
| 12-Apr-2022 21:27:37.215 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory 鎶妛eb 搴旂敤绋嬪簭閮ㄧ讲鍒扮洰褰� [C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager] |
| 12-Apr-2022 21:27:37.271 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Web搴旂敤绋嬪簭鐩綍[C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]鐨勯儴缃插凡鍦╗56]姣鍐呭畬鎴� |
| xdclass.net |
- 再访问testServlet: 获取到在userServlet中向servletContext中添加的数据

| 12-Apr-2022 21:27:37.215 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory 鎶妛eb 搴旂敤绋嬪簭閮ㄧ讲鍒扮洰褰� [C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager] |
| 12-Apr-2022 21:27:37.271 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Web搴旂敤绋嬪簭鐩綍[C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]鐨勯儴缃插凡鍦╗56]姣鍐呭畬鎴� |
| xdclass.net |
| TestServlet appInfo=xdclass.net info |
JSP本身就是⼀种Servlet, JSP在第⼀次被访问的时候会被编译为HttpJspPage类,是HttpServlet的⼀个⼦类
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术