标签:
在编写servlet过程中,需要用到 ServletConfig、ServletContext对象,对这两种对象的介绍如下:
ServletContext对象:servlet容器在启动时会加载web应用,并为每个web应用创建唯一的servlet context对象,可以把ServletContext看成是一个Web应用的服务器端组件的共享内存,在ServletContext中可以存放共享数据。ServletContext对象是真正的一个全局对象,凡是web容器中的Servlet都可以访问。
servletConfig对象:用于封装servlet的配置信息。从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对servlet自身有效,一个servlet的ServletConfig对象不能被另一个servlet访问。
在servlet中如何获取ServletContext对象和ServletConfig对象,请看下面代码:
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServlet;
-
- public class Servlet1 extends HttpServlet{
-
- @Override
- public void init() throws ServletException
- {
- ServletConfig config = this.getServletConfig();
- System.out.println("Servlet1 config=="+config);
- System.out.println("Servlet1 间接获取context==" +config.getServletContext() );
- System.out.println("Servlet1 直接获取context==" + this.getServletContext());
- System.out.println("Servlet1 param_value==" + config.getInitParameter("servlet1_param_name"));
- System.out.println("context_value==" + this.getServletContext().getInitParameter("context_param"));
- System.out.println("*********************************");
- }
-
- @Override
- public void service(ServletRequest req, ServletResponse resp)
- throws ServletException{
-
- }
-
- @Override
- public void destroy() {
-
- }
- }
Servlet2的代码:
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import javax.servlet.http.HttpServlet;
-
- public class Servlet2 extends HttpServlet {
- @Override
- public void init() throws ServletException
- {
- ServletConfig config = this.getServletConfig();
- System.out.println("Servlet2 config=="+config);
- System.out.println("Servlet2 间接获取context==" +config.getServletContext());
- System.out.println("Servlet2 直接获取context==" + this.getServletContext());
- System.out.println("Servlet2 param_value==" + config.getInitParameter("servlet2_param_name"));
- System.out.println("context_value==" + this.getServletContext().getInitParameter("context_param"));
- System.out.println("*********************************");
- }
-
- @Override
- public void service(ServletRequest req, ServletResponse resp)
- throws ServletException{
-
- }
-
- @Override
- public void destroy() {
-
- }
- }
web.xml文件的配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <servlet>
- <servlet-name>servlet_1</servlet-name>
- <servlet-class>com.test.servlet.Servlet1</servlet-class>
- <init-param>
- <param-name>servlet1_param_name</param-name>
- <param-value>value1</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet>
- <servlet-name>servlet_2</servlet-name>
- <servlet-class>com.test.servlet.Servlet2</servlet-class>
- <init-param>
- <param-name>servlet2_param_name</param-name>
- <param-value>value2</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <context-param>
- <param-name>context_param</param-name>
- <param-value>value3</param-value>
- </context-param>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
部署到tomcat中,然后启动服务,在控制台打印出如下信息:
Servlet2 config==org.apache.catalina.core.StandardWrapperFacade@13d422d
Servlet2 间接获取context==org.apache.catalina.core.ApplicationContextFacade@1123c5f
Servlet2 直接获取context==org.apache.catalina.core.ApplicationContextFacade@1123c5f
Servlet2 param_value==value2
context_value==value3
*********************************
Servlet1 config==org.apache.catalina.core.StandardWrapperFacade@14683c0
Servlet1 间接获取context==org.apache.catalina.core.ApplicationContextFacade@1123c5f
Servlet1 直接获取context==org.apache.catalina.core.ApplicationContextFacade@1123c5f
Servlet1 param_value==value1
context_value==value3
*********************************
通过控制台打印的信息可以得出如下结论:
1、在web.xml配置文件中,对没个servlet的配置里,有一
项<load-on-startup></load-on-startup>,它的含义是:标记容器是否在启动的时候就加载这个
servlet。当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;当是一个负数时或者没有指定时,则指示容器在该servlet被
选择时才加载。正数的值越小,启动该servlet的优先级越高。
servlet_2虽然配置在servlet_1后面,但是它的load-on-startup为0,启动的优先级高于servlet_1,所以servlet_2先启动。
2、获取ServletContext的两种方式,直接获取和间接获取,得到的对象都是同一个。同时在servlet1和servlet2中取得的ServletContext对象都是同一个对象,说明整个web应用都只有一个唯一的ServletContext实例;
3、servlet1与servlet2的serveletConfig对象是不一样的,说明ServletConfig对象的作用范围仅在servlet中。
mvc-servlet---servletContext与servletConfig2
标签:
原文地址:http://www.cnblogs.com/yang--yang/p/4469991.html