javaWeb项目之简析配置文件

javaWeb项目配置文件:

web.xml解析:(大小写敏感,标签不嵌套) context-param > listener  >  fileter  > servlet

  1. 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
  2. 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
  3. 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
  4. 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
  5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
  6. 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。

<?xml version="1.0" encoding="UTF-8"?>   必须,描述xml版本和文件的字符编码

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">  定义标签头

<display-name>Archetype Created Web Application</display-name>:xml编辑器显示的名称

<context-param>标签:可在项目启动之前做一些操作(打开数据库,加载配置文档,声明应用内初始化参数等  Application)

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-ehcache.xml,classpath:spring-hibernate.xml,classpath:spring-druid.xml</param-value>
</context-param>

<filter>标签:设置过滤器,指定web容器的过滤器(自定义过滤器须实现 javax.servlet.Filter init(),doFilter()和destory()方法,init方法是在WEB应用启动就会调用,doFilter则是在访问filter-mapping映射到的url时会调用实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等,可根据用户访问方式配置调用(REQUEST,INCLUDE,FORWARD,ERROR)

<!-- 配置字符集过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<!-- 配置项目的编码mapping (用户直接访问的时候调用)-->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

<listener>标签:捕捉到服务器的启动和停止,在启动和停止触发里面的方法做相应的操作,通过监听器,可自动激发一些操作,如监听在线用户数量


<!-- 配置文件加载监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<servlet>标签:将servlet类与访问的映射路径关联起来

<!-- 配置spring mvc -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

配置session-config,配置session失效时间

<session-config>
<session-timeout>120</session-timeout>
</session-config>

更多详情见:最全web.xml配置

Spring.xml:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章