一、servletContext概述
servletContext对象是Servlet三大域对象之一,每个Web应用程序都拥有一个ServletContext对象,该对象是Web应用程序的全局对象或者上下文。Tomcat服务器在启动时,会自动创建一个ServletContext对象,在关闭时,会自动销毁这个ServletContext对象。每个Web应用程序只拥有一个ServletContext对象,ServletContext对象可以在整个Web应用*享数据资源。
- 生命周期很长
- 每个web应用都有一个唯一的servletContext对象.
- 在每个应用加载的时候,服务器就会创建servletContext对象。
- ServletContext对象是一个域对象(领域)
- 获得servletContext的方式
在自定义Servlet中有以下几种方式获取到ServletContext对象:
- 通过ServletConfig对象的this.getServletContext()方法获取。
- 采用request.getSession().getServletContext()方法获取。
- 通过继承GenericServlet类或HttpServlet类,调用GenericServlet类或HttpServlet类的getServletContext()方法获取。
import java.io.IOException; import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//演示获取servletContext对象
/**
* 有三种方式获取servletContext对象
* 1. 采用servletConfig对象获取
* 2. 采用servlet实例对象获取
* 3. 采用request对象获取
* @author Administrator
*
*/
public class ServletContext1 extends HttpServlet { ServletContext sc ; @Override
public void init(ServletConfig config) throws ServletException {
super.init(config) ;
sc = config.getServletContext() ;//1.
} public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//第二种方式
ServletContext sc1 = this.getServletContext() ;
System.out.println(sc);
System.out.println(sc1 == sc);
//第三种方式
ServletContext sc2 = request.getSession().getServletContext() ;
System.out.println(sc2 == sc);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
应用
1) 实现数据共享
2) 获取全局配置参数
3) 请求转发
a) 拿取请求转发器,然后转发
4) 获取资源文件
5) 用来获得文件的MIME的类型.
案例
下面我将通过例子说明:登录成功后,5秒后跳转到某个页面,在页面中显示您是第x次登录成功
- 创建一个VisitServlet用来获取访问次数,并存储在ServletContext对象中。
public class VisitServlet extends HttpServlet {
@Override
public void init() throws ServletException {
ServletContext context = getServletContext();
context.setAttribute("times", 0);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext(); int times = (Integer)context.getAttribute("times");
times ++;
context.setAttribute("times", times);
response.setHeader("Refresh", "5;url=/test/showTimeServlet");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
- 创建一个ShowTimeServlet用来显示访问次数。
public class ShowTimeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
//获取访问的次数
int times = (Integer)context.getAttribute("times"); response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>VisitServlet共被访问了"+times+"次</h1>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获得Servlet的web.xml配置信息
<context-param>
<param-name>username</param-name>
<param-value>苍老师</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>123</param-value>
</context-param>
获取web.xml配置信息中
import java.io.IOException;
import java.util.Enumeration; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 获取web应用的初始化信息
*/
public class ServletContextDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得ServletContext域对象
ServletContext context = this.getServletContext();
//获得单个数据(username)
String name = context.getInitParameter("username") ;
// System.out.println(name);
//获得所有数据
Enumeration enumeration = context.getInitParameterNames();
while(enumeration.hasMoreElements()){
String name = (String) enumeration.nextElement();
String value = context.getInitParameter(name);
System.out.println(name+":"+value);
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- 打开浏览器,在地址栏输入http://localhost:8080/test/visit,访问VisitServlet。
- 再新打开浏览器,在地址栏输入http://localhost:8080/test/show,显示访问次数。
二、servletConfig概述
在javax.servlet包中,定义了ServletConfig接口。Servlet容器使用ServletConfig对象在Servlet初始化时向其传递配置信息。
所谓的Serlvet配置信息,就是在Web应用程序中web.xml文件中配置有关Servlet的内容。
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletConfigTest</servlet-name>
<servlet-class>app.java.servlet.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/servlet/ServletConfigTest</url-pattern>
</servlet-mapping>
<init-param>
<param-name>name</param-name>
<param-value>yl</param-value>
</init-param>
<init-param>
<param-name>blog</param-name>
<param-value>http://www.longestory.com</param-value>
</init-param>
- getServletName()方法:获取web.xml文件中配置的Servlet名称。
public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException {
ServletConfig servletConfig = this.getServletConfig(); String servletName = servletConfig.getServletName();
System.out.println(servletName);
}
运行Web应用程序,在控制台中打印“ConfigServletTest”。
- getInitParameter(String name)方法:返回指定名称的初始化参数的值,如果参数不存在则返回null值。
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig(); String name = servletConfig.getInitParameter("name");
String blog = servletConfig.getInitParameter("blog");
System.out.println(name + "'s blog is " + blog);
}
运行Web应用程序,在控制台中打印“yl's blog is http://www.longestory.com”。
- getInitParameterNames()方法:返回Servlet配置的所有初始化参数名称的枚举集合. Enumeration是Iterator的前身,用法与Iterator一致。
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig(); Enumeration enumeration = servletConfig.getInitParameterNames();
while (enumeration.hasMoreElements()) {
String element = (String) enumeration.nextElement();
String value = servletConfig.getInitParameter(element);
System.out.println(element + ": " + value);
}
}
- ServletConfig有哪些实际作用呢?在struts 1 框架中就运行了ServletConfig内容。
<!-- Standard Action Servlet Configuration (with debugging) -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/struts-config.xml,
/WEB-INF/struts-config-Wildcard.xml
</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2017-05-12