web.xml配置总结

来源:互联网 发布:java 数组 length 编辑:程序博客网 时间:2024/04/29 14:50
一、关于webAppRootKey的定义

默认情况下webAppRootKey属性的值为webapp.root(就是说系统会把“web目录的路径”压入一个叫webapp.root的系统变量,也就是说我们可以在以后的properties文件设置中,使用${webapp.root.webtools}来代替“web目录的路径”,此例为tomcat/webapp/webtools)

因为一个tomcat下面可能部署了多个基于spring、log4j的应用,webapp.root会产生冲突错误(也就是说不知道webapp.root具体代表那个应用的路径了),所以我们可以为具体应用具体配置,而不使用默认值。

如下:

 <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>webapp.root.webtools</param-value>
 </context-param>

这样我们就把“web目录的路径”压入一个叫webapp.root.webtools的系统变量。

二、spring以及log4j的配置

<!-- Spring ApplicationContext配置文件的路径,可使用通配符。多个路径用逗号分隔。此参数用于后面的Spring-Context loader -->

 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:spring/*.xml</param-value>
 </context-param>

 <!--由Spring载入的Log4j配置文件位置-->
 <context-param>
     <param-name>log4jConfigLocation</param-name>
     <param-value>/WEB-INF/classes/log4j.properties</param-value>
 </context-param>

 <!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
 <context-param>
     <param-name>log4jRefreshInterval</param-name>
     <param-value>60000</param-value>
 </context-param>

//Log4jConfigListener会去log4j.propeties 读取配置文件;开一条watchdog线程每60秒扫描一下配置文件的变化;

 <listener>
     <listener-class>
         org.springframework.web.util.Log4jConfigListener
     </listener-class>
 </listener>

//根据spring的配置文件加载spring。

 <listener>
     <listener-class>
         org.springframework.web.context.ContextLoaderListener
     </listener-class>
 </listener>

三、Spring 刷新Introspector防止内存泄露

 <listener>
     <listener-class>
         org.springframework.web.util.IntrospectorCleanupListener
     </listener-class>
 </listener>

它主要负责处理由JavaBeans Introspector的使用而引起的缓冲泄露

spring中对它的描述如下:
    它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.在web.xml中注册这个listener.可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类。
    如果你使用了JavaBeans Introspector来分析应用中的类,Introspector 缓冲中会保留这些类的引用.结果在你的应用关闭的时候,这些类以及web 应用相关的class loader没有被垃圾回收.
    不幸的是,清除Introspector的唯一方式是刷新整个缓冲.这是因为我们没法判断哪些是属于你的应用的引用.所以删除被缓冲的introspection会导致把这台电脑上的所有应用的introspection都删掉.
    需要注意的是,spring 托管的bean不需要使用这个监听器.因为spring它自己的introspection所使用的缓冲在分析完一个类之后会被马上从javaBeans Introspector缓冲中清除掉.
    应用程序中的类从来不直接使用JavaBeans Introspector.所以他们一般不会导致内部查看资源泄露.但是一些类库和框架往往会产生这个问题.例如:Struts

  1. package  org.springframework.web.util;   
  2.   
  3.  import  java.beans.Introspector;   
  4.  import  javax.servlet.ServletContextEvent;   
  5.  import  javax.servlet.ServletContextListener;   
  6.   
  7.  public   class  IntrospectorCleanupListener  implements  ServletContextListener   {   
  8.      public   void  contextInitialized(ServletContextEvent event)   {   
  9.     }    
  10.     
  11.       public   void  contextDestroyed(ServletContextEvent event)   {   
  12.         Introspector.flushCaches();   
  13.     }    
  14. }   

四、编码问题的配置

1、字符编码过滤器(将通过此url的访问的字符编码都强制转换成UTF-8)

 <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>
    <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
    </init-param>
 </filter>

 <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

2、解决struts2中文乱码问题
 <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>
       org.apache.struts2.dispatcher.ActionContextCleanUp
    </filter-class>
 </filter>

 <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

3、再者建立数据库时要统一编码为UTF-8

4、jsp页面要统一编码 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>  

五、添加struts配置(将所有一下的url访问拦截下来转发给action)

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>
      org.apache.struts2.dispatcher.FilterDispatcher
   </filter-class>
   <init-param>
      <param-name>config</param-name>
      <param-value>
         struts-default.xml,struts-plugin.xml,struts/struts.xml

         //默认为classpath下或src下
      </param-value>
   </init-param>
</filter> 

<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>


原创粉丝点击