您的位置:首页 > 其它

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());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet
相关文章推荐