首先要知道servletContext与servletConfig
servletContext是web应用级别,是jvm进程级别;servletConfig是servlet服务级别,是线程级别。
定义:
ServletConfig:Servlet的配置对象,容器在初始化Servlet时通过它传递信息给Servlet。
ServletContext:上下文对象,提供了一系列方法供Servlet与Web容器交互。
创建时机:
ServletConfig:在容器初始化Servlet的时候,并为其提供上下文初始化参数的名/值对的引用。
ServletContext:容器启动的时候,并为其提供Servlet初始化参数的名/值对的引用。
一个web应用一般对应一个WEB-INF文件夹:
web.xml就是web应用级别,在web.xml中配置多个servlet:
<web-app>
<servlet>
<servlet-name>AcceptLink</servlet-name>
<servlet-class>labmanagement.AcceptLink</servlet-class>
<init-param>
<param-name>xmlLocations</param-name>
<param-value>one.xml,two.xml</param-value>
</init-param>
</servlet>
</servlet>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>labmanagement.HelloWorld</servlet-class>
<init-param>
<param-name>xmlLocations</param-name>
<param-value>one.xml,two.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>AcceptLink</servlet-name>
<url-pattern>/AcceptLink</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
ServletConfig对象:
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
首先,需要创建私有变量:private ServletConfig config = null;
其次,要重写init方法,传入config,令this.config = config;从而获得ServletConfig对象
最后,就可以获得<init-parm>中的配置信息了
//获取初始化参数
String value1 =this.config.getInitParameter("x1");
//获得配置文档中<init-param>标签下name对应的value
String vlaue2 =this.config.getInitParameter("x2");
//2.获取所有的初始化参数(用Enumeration接收)
Enumeration e =this.config.getInitParameterNames();
while(e.hasMoreElements()){
String name =(String) e.nextElement();
String value= this.config.getInitParameter(name);
System.out.println(name+ "=" + value);
}
在开发中ServletConfig的作用有如下三个:
1)获得字符集编码
String charset =this.config.getInitParameter("charset");
2)获得数据库连接信息
String url =this.config.getInitParameter("url");
String username =this.config.getInitParameter("username");
String password =this.config.getInitParameter("password");
3)获得配置文件
String configFile =this.config.getInitParameter("config");
ServletContext对象:
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
1)ServletContext对象应用1:多个web组件之间使用它实现数据共享
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
在serlvet中,可以使用如下语句来设置数据共享
ServletContext context =this.getServletContext(); //servletContext域对象
context.setAttribute("data","共享数据"); //向域中存了一个data属性
在另一个servlet中,可以使用如下语句来获取域中的data属性
ServletContext context =this.getServletContext();
String value = (String)context.getAttribute("data"); //获取域中的data属性
System.out.println(value);
2)通过servletContext对象获取到整个web应用的配置信息
String url =this.getServletContext().getInitParameter("url");
String username =this.getServletContext().getInitParameter("username");
String password =this.getServletContext().getInitParameter("password");
3)通过servletContext对象实现servlet转发
由于servlet中的java数据不易设置样式,所以serlvet可以将java数据转发到JSP页面中进行处理
this.getServletContext().setAttribute("data","serlvet数据转发");
RequestDispatcher rd =this.getServletContext().getRequestDispatcher("/viewdata.jsp");
rd.forward(request,response);
4)通过servletContext对象读取资源文件
在实际开发中,用作资源文件的文件类型,通常是:xml、properties,而读取xml文件必然要进行xml文档的解析,所以以下例子只对properties文件进行读取(在一个web工程中,只要涉及到写地址,建议最好以/开头)
在web工程中,我们一般来说,是不能采用传统方式读取配置文件的,因为相对的是jvm的启动目录(tomcat的bin目录),所以我们要使用web绝对目录来获取配置文件的地址
读取资源文件的三种方式:
第一种:使用ServletContext的getResourceAsStream方法:返回资源文件的读取字节流
InputStream in =this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = newProperties();
prop.load(in);
String url =prop.getProperty("url");
第二种:使用ServletContext的getRealPath方法,获得文件的完整绝对路径path,再使用字节流读取path下的文件
String path =this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
String filename =path.substring(path.lastIndexOf("\\")+1);
//相比第一种方法的好处是:除了可以获取数据,还可以获取资源文件的名称
FileInputStream in = newFileInputStream(path);
Properties prop = newProperties();
prop.load(in);
String url =prop.getProperty("url");
第三种:使用ServletContext的getResource方法,获得一个url对象,调用该类的openStream方法返回一个字节流,读取数据
URL url =this.getServletContext().getResource("/WEB-INF/classes/db.properties");
InputStream in =url.openStream();
Properties prop = newProperties();
prop.load(in);
String url1 =prop.getProperty("url");
5)web工程中,不同位置的资源文件的读取方式
一、当资源文件在包下面时
InputStream in =this.getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties");
System.out.println(in);
in =this.getServletContext().getResourceAsStream("/WEB-INF/db.properties");
System.out.println(in);
三、资源文件在web工程中
in =this.getServletContext().getResourceAsStream("/db.properties");
System.out.println(in);
******************************************************************************************************************
SpringMVC加载WEB-INF中web.xml之后,web容器会创建一个servletContext对象,这个对象会加载web.xml中配置的信息。其中包含servlet信息,webApplicationContext类信息,和重要的ContextLoaderListener类信息。
web.xml中配置如下:
<context-param>
<param-name>contextClass</param-name>
<param-value>com.web.controller.env.MyWebApplicationContext</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>authStatusServlet</servlet-name>
<servlet-class>com.ids.client.AuthStatusServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>authStatusServlet</servlet-name>
<url-pattern>/authStatus</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>popupLoginSuccessServlet</servlet-name>
<servlet-class>com.ids.client.PopupLoginSuccessServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>popupLoginSuccessServlet</servlet-name>
<url-pattern>/popupLoginSuccess</url-pattern>
</servlet-mapping>
接着根据webApplicationContext信息创建webApplicationContext的监听和初始化。
ContextLoaderListener就是ServletContextListener是监听servlet前期工作,比如创建beanFactory,加载servlet加载需要的bean。
web.xml中注册监听器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
initWebApplicationContext如下:
initWebApplicationContext方法结束!!!
webApplicationContext:
Class<?> contextClass determineContextClass(ServletContextservletContext )方法如下:
到此只是讲述了spring中,servletContext->servletContextListernner->webApplicationContext.
说到这里,对于springMVC中除了很基础的web.xml(web应用基础配置)还有两个很重要的和spring相关的配置文件,分别是applicationcontext.xml和spring-servlet.xml。这两个配置文件在web.xml中都要声明,而且要使用applicationcontext.xml,servletContextListernner必须声明,servletContextListernner的其中很重要的工作就是加载applicationcontext.xml,并且创建BeanFactory.加载applicationcontext.xml(用来声明全局的bean对象)中全局的bean对象到webApplicationContext中。
webApplicationContext中包含了applicationcontext信息,servlet信息等。然后webApplicationContext加载其含有的servlet信息,并对servlet内部的spring-servlet进行解析。
web.xml中applicationcontext.xml和spring-servlet.xml配置:
<!-- 初始化Spring容器并加载Spring配置文件applicationContext.xml,默认在WebRoot/WEB-INF目录下面 ,
可以通过<context-param>的contextConfigLocation属性指定路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param- value>classpath:com/lince/config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载DispatcherServlet并加载其的配置文件spring3-servlet.xml,默认也是在WEB-INF目录下 -->
<servlet>
<servlet-name>spring3</servlet-name> <!-- 默认找到的是spring3-servlet.xml -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/lince/config/spring3-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
到此还没有加载servlet.下面继续讲
spring中的servlet是DispacherServlet不过也是继承httpServlet,然后对init,post,get等方法进行了封装。
首先我们写自己servlet然后在web.xml中声明,很明显我们的selfServlet要集成httpServlet
spring 的核心就是DispacherServlet,然后对init,post,get,put等方法进行封装,其中init也是spring实现的核心关键方法。
servletContext(加载web.xml)->加载和创建servletContextListernner->加载web.xml中webApplicationContext类信息
创建webApplicationContext->加载servlet信息->调用init()方法->创建servletConfig信息->创建servlet
spring中init方法:
到此init方法结束!!!
这段代码主要创建servletConfig和initServletBean
servletConfig 创建对应ServletConfigPropertyValue():
initServletBean:
这里又再次调用了initWebApplicationContext 是对之前的WebApplicationContext 的创建或者补充,并对servlet使用的实例进行初始化:
spring-servlet中声明了servlet的controller和service等扫描信息,记录了拦截器链的拦截路径信息(mapping),记录servlet Mappling信息和对应controller的Mapping信息。
onrefresh()中对servlet的Mapping和根据controller建立拦截链,创建对应的handler。
本文详细介绍了Servlet与SpringMVC的初始化过程,包括servletContext与servletConfig的区别及作用,web.xml配置,以及SpringMVC如何加载web.xml并创建webApplicationContext。
3473

被折叠的 条评论
为什么被折叠?



