小谈-—ServletConfig对象和servletContext对象
Posted 爆发的小宇宙00
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小谈-—ServletConfig对象和servletContext对象相关的知识,希望对你有一定的参考价值。
一、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()方法获取。
1 import java.io.IOException; 2 3 import javax.servlet.ServletConfig; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 //演示获取servletContext对象 10 /** 11 * 有三种方式获取servletContext对象 12 * 1. 采用servletConfig对象获取 13 * 2. 采用servlet实例对象获取 14 * 3. 采用request对象获取 15 * @author Administrator 16 * 17 */ 18 public class ServletContext1 extends HttpServlet { 19 20 ServletContext sc ; 21 22 @Override 23 public void init(ServletConfig config) throws ServletException { 24 super.init(config) ; 25 sc = config.getServletContext() ;//1. 26 } 27 28 public void doGet(HttpServletRequest request, HttpServletResponse response) 29 throws ServletException, IOException { 30 //第二种方式 31 ServletContext sc1 = this.getServletContext() ; 32 System.out.println(sc); 33 System.out.println(sc1 == sc); 34 //第三种方式 35 ServletContext sc2 = request.getSession().getServletContext() ; 36 System.out.println(sc2 == sc); 37 } 38 39 public void doPost(HttpServletRequest request, HttpServletResponse response) 40 throws ServletException, IOException { 41 doGet(request, response); 42 } 43 }
应用
1) 实现数据共享
2) 获取全局配置参数
3) 请求转发
a) 拿取请求转发器,然后转发
4) 获取资源文件
5) 用来获得文件的MIME的类型.
案例
下面我将通过例子说明:登录成功后,5秒后跳转到某个页面,在页面中显示您是第x次登录成功
- 创建一个VisitServlet用来获取访问次数,并存储在ServletContext对象中。
1 public class VisitServlet extends HttpServlet { 2 @Override 3 public void init() throws ServletException { 4 ServletContext context = getServletContext(); 5 context.setAttribute("times", 0); 6 } 7 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 8 ServletContext context = getServletContext(); 9 10 int times = (Integer)context.getAttribute("times"); 11 times ++; 12 context.setAttribute("times", times);
response.setHeader("Refresh", "5;url=/test/showTimeServlet");
13 } 14 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 doGet(request, response); 16 } 17 }
- 创建一个ShowTimeServlet用来显示访问次数。
1 public class ShowTimeServlet extends HttpServlet { 2 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 ServletContext context = getServletContext(); 4 //获取访问的次数 5 int times = (Integer)context.getAttribute("times"); 6 7 response.setContentType("text/html;charset=utf-8"); 8 PrintWriter out = response.getWriter(); 9 out.println("<h1>VisitServlet共被访问了"+times+"次</h1>"); 10 } 11 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 12 doGet(request, response); 13 } 14 }
获得Servlet的web.xml配置信息
1 <context-param> 2 <param-name>username</param-name> 3 <param-value>苍老师</param-value> 4 </context-param> 5 <context-param> 6 <param-name>password</param-name> 7 <param-value>123</param-value> 8 </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
以上是关于小谈-—ServletConfig对象和servletContext对象的主要内容,如果未能解决你的问题,请参考以下文章
servlet中this.getServletContext(); this.getServletConfig().getServletContext(); 的区别