?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 { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected 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 { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected 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 { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected 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 { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected 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:1show:2show:3show:4show:5show:6show:7
作用域总结
HttpServletRequest:一次请求,请求响应之前有效HttpSession:一次会话开始,浏览器不关闭或不超时之前有效ServletContext:服务器启动开始,服务器停止之前有效? 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞
?收藏
⭐️评论
?