Servlet学习笔记—ServletContext的详解
2017-10-28 13:32
477 查看
一、ServletContext概述
WEB容器在启动时(也就是Tomcat启动的时候),它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
二、ServletContext参数配置
在web.xml文件中加入节点<context-param>:<!-- 配置ServletContext --> <context-param> <param-name>context-name</param-name> <param-value>context-value</param-value> </context-param> <context-param> <param-name>context-name1</param-name> <param-value>context-value1</param-value> </context-param>
整个web.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- =====================分割线===================== --> <!-- 配置ServletContext --> <context-param> <param-name>context-name</param-name> <param-value>context-value</param-value> </context-param> <context-param> <param-name>context-name1</param-name> <param-value>context-value1</param-value> </context-param> <!-- =====================分割线===================== --> <!-- 第一步:配置servlet信息 --> <servlet> <!-- 设置servlet的名字 --> <servlet-name>httpServlet1</servlet-name> <!-- 具体的servlet的类,要填上类的全路径 --> <servlet-class>com.pl.servlet.HttpServletDemo1</servlet-class> <!-- 设置servlet的初始化的时机在服务器启动的时候 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 第二步:配置servlet映射信息 --> <servlet-mapping> <!-- 指定要映射的servlet的名字 --> <servlet-name>httpServlet1</servlet-name> <!-- servlet的具体映射路径 --> <url-pattern>/httpserv1</url-pattern> </servlet-mapping> <!-- =====================分割线===================== --> <!-- 第一步:配置servlet信息 --> <servlet> <!-- 设置servlet的名字 --> <servlet-name>httpServlet2</servlet-name> <!-- 具体的servlet的类,要填上类的全路径 --> <servlet-class>com.pl.servlet.HttpServletDemo2</servlet-class> <!-- 设置servlet的初始化的时机在服务器启动的时候 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 第二步:配置servlet映射信息 --> <servlet-mapping> <!-- 指定要映射的servlet的名字 --> <servlet-name>httpServlet2</servlet-name> <!-- servlet的具体映射路径 --> <url-pattern>/httpserv2</url-pattern> </servlet-mapping> </web-app>
三、ServletContext参数获取
方法一、通过ServletConfig对象获取ServletContext对象
第一个servlet获取ServletContext参数:public class HttpServletDemo1 extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { //获取ServletContext对象 ServletContext sc = config.getServletContext(); //获得ServletContext级别的全局的容器参数 String contextValue = sc.getInitParameter("context-name"); System.out.println("HttpServletDemo1获取ServletContext参数值:"+contextValue); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet方法被调用了"); resp.getOutputStream().write("doGet方法被调用了".getBytes()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost方法被调用了"); resp.getOutputStream().write("doPost方法被调用了".getBytes()); } }
第二个servlet获取ServletContext参数(与第一个完全一样的方法):
public class HttpServletDemo2 extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { //获取ServletContext对象 ServletContext sc = config.getServletContext(); //获得ServletContext级别的全局的容器参数 String contextValue = sc.getInitParameter("context-name1"); System.out.println("HttpServletDemo2获取ServletContext参数值:"+contextValue); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet方法被调用了"); resp.getOutputStream().write("doGet方法被调用了".getBytes()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost方法被调用了"); resp.getOutputStream().write("doPost方法被调用了".getBytes()); } }
方法二、直接通过调用getServletContext()方法获取ServletContext对象
示例:public class HttpServletDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext sc = getServletContext(); } }
四、应用场景示例(统计站点pv)
第一个Servlet:public class HttpServletDemo1 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext sc = getServletContext(); Integer pvcount = (Integer) sc.getAttribute("pvcount"); if (pvcount == null) { sc.setAttribute("pvcount", 1); String content = "<font color='red' size=20>" + 1 + "</font>"; resp.getOutputStream().write(content.getBytes()); } else { sc.setAttribute("pvcount", ++pvcount); String content = "<font color='red' size=20>" + pvcount + "</font>"; resp.getOutputStream().write(content.getBytes()); } } }
第二个Servlet:
public class HttpServletDemo2 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext sc = getServletContext(); Integer pvcount = (Integer) sc.getAttribute("pvcount"); if (pvcount == null) { sc.setAttribute("pvcount", 1); String content = "<font color='red' size=20>" + 1 + "</font>"; resp.getOutputStream().write(content.getBytes()); } else { sc.setAttribute("pvcount", ++pvcount); String content = "<font color='red' size=20>" + pvcount + "</font>"; resp.getOutputStream().write(content.getBytes()); } } }
相关文章推荐
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- Servlet知识详解(一)ServletContext对象 和 ServletConfig对象学习笔记
- javaweb学习笔记:Servlet的运行过程详解
- Servlet学习笔记(八):过滤器Filter详解
- 学习笔记(二)HttpServlet、ServletContext、ServletConfig
- Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext
- servlet学习笔记8——ServletContext的讲解和使用
- Servlet学习笔记(3)——ServletContext
- JavaWeb学习笔记----Servlet的ServletConfig对象和ServletContext对象的使用
- javaweb基础学习(一)<ServletConfig与ServletContext对象详解>
- servlet学习笔记8——ServletContext的讲解和使用
- 韩顺平servlet学习笔记05-servletContext
- servlet学习笔记8——ServletContext的讲解和使用
- 学习笔记--【转】Parameter与Attribute的区别&servletContext与ServletConfig区别
- [原创]java WEB学习笔记47:Servlet 监听器简介, ServletContext(Application 对象), HttpSession (Session 对象), HttpServletRequest (request 对象) 监听器,利用listener理解 三个对象的生命周期
- Servlet学习笔记(七):Session详解
- Servlet学习笔记(七):Session详解
- Servlet学习总结(7)----ServletContext详解
- Servlet学习笔记—response详解
- 重温Servlet学习笔记--servletContext对象