淮安市建设工程施工图审查处网站/chatgpt 网站
💗wei_shuo的个人主页
💫wei_shuo的学习社区
🌐Hello World !
ServletContext对象
概述
- 全局对象,拥有作用域,对应Tomcat的Web应用
- Web服务器启动时,会为每一个Web应用程序创建一块共享的存储区域(ServletContext)
- ServletContext在Web服务器
启动时创建
,服务器关闭时销毁
获取ServletContext对象
- GenericServlet提供 getServletContext(); 方法 this.getServletContext();
- HttpServletRequest提供 getServletContext(); 方法
- HttpSession提供 getServletContext(); 方法
//获取ServletContext对象
ServletContext servletContext = this.getServletContext();//通过request对象,获取ServletContext对象
ServletContext servletContext1 = request.getServletContext();//通过request获取session对象,获取ServletContext对象
HttpSession session = request.getSession();
ServletContext servletContext2 = session.getServletContext();
ServletContext 作用
- 获取项目真实路径(获取当前项目在服务器发布的真实路径)
String realpath = servletContext.getRealPath("/");
//获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取项目真实路径System.out.println(servletContext.getRealPath("/"));/*输出结果D:\Eclipse\apache-tomcat-8.5.82-windows-x64\apache-tomcat-8.5.82\webapps\Servlet_Projects_war\*/
- 获取项目上下文路径(获取当前项目上下文路径:应用程序名称)
System.out.println(servletContext.getContextPath()); System.out.println(request.getContextPath());
//获取ServletContext对象 ServletContext servletContext = this.getServletContext(); //获取项目上下文路径 System.out.println(servletContext.getContextPath()); System.out.println(request.getContextPath()); /* 输出结果: /Servlet_Projects_war /Servlet_Projects_war */
全局容器
ServletContext拥有作用域,可以存储数据到全局容器中
- 存储数据: servletContext.setAttribute(“name”,“value”);
- 获取数据: servletContext.getAttribute(“name”);
- 移除数据: servletContext.removeAttribute(“name”);
ServletContextController类
@WebServlet(name = "ServletContextController", value = "/ctxController") public class ServletContextController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取项目真实路径System.out.println(servletContext.getRealPath("/"));//存储数据servletContext.setAttribute("context","info");} }
- ShowContextController类
@WebServlet(name = "ShowContextController", value = "/ShowController") public class ShowContextController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = this.getServletContext();//获取数据String context = (String) servletContext.getAttribute("context");System.out.println(context);/*输出结果:D:\Eclipse\apache-tomcat-8.5.82-windows-x64\apache-tomcat-8.5.82\webapps\Servlet_Projects_war\info*/} }
ServletContext 特点
- 唯一性:一个应用对应一个ServletContext
- 生命周期:只要容器不关闭或者应用不卸载,ServletContext就一直存在
ServletContext 应用场景
- CounterController类
@WebServlet(name = "CounterController", value = "/counterController") public class CounterController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = request.getServletContext();//获取计数器Integer counter = (Integer) servletContext.getAttribute("counter");//计数器初始化if (counter == null) {counter = 1;//counter存入计数器servletContext.setAttribute("counter", counter);} else {counter++;servletContext.setAttribute("counter", counter);}System.out.println("counter:" + counter);} }
- ShowCounterController类
@WebServlet(name = "ShowCounterController", value = "/ShowCounterController") public class ShowCounterController extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//获取ServletContext对象ServletContext servletContext = request.getServletContext();//获取计数器Integer counter = (Integer) servletContext.getAttribute("counter");//计数器初始化if (counter == null) {counter = 1;//counter存入计数器servletContext.setAttribute("counter", counter);} else {counter++;servletContext.setAttribute("counter", counter);}System.out.println("show:" + counter);} }
- 结果
counter:1 show:2 show:3 show:4 show:5 show:6 show:7
作用域总结
- HttpServletRequest:一次请求,请求响应之前有效
- HttpSession:一次会话开始,浏览器不关闭或不超时之前有效
- ServletContext:服务器启动开始,服务器停止之前有效
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍收藏
⭐️评论
📝