1. 程式人生 > >Servlet初始配置 監聽器和過濾器

Servlet初始配置 監聽器和過濾器

https cal 認證 缺省 Coding wrapper oot comm 是否

ServletContext:application範圍內的參數

此所設定的參

來源: http://note.sdo.com/my

數,在JSP網頁中可以使用下列方法來取得:

${initParam.param0},

<%=application.getInitParameter("param0") %><br/>

若在Servlet可以使用下列方法來獲得:

String param_name=getServletContext().getInitParameter("param0")

=this.getServletConfig().getServletContext().getInitParameter("param0")

=application.getInitParameter("param0");

使用上下文初始化參數:

1 2 3 4 5 6 7 8 <context-param> <param-name>param_name</param-name> <param-value>avalible during application</param-value> </context-param> <context-param>
<param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-configuration/*.xml</param-value> </context-param>
<wiz_tmp_tag class="wiz-block-scroll">

部署在同一容器中的多個Web項目,要配置不同的webAppRootKey,web.xml文件中最好定義webAppRootKey參數,如果不定義,將會缺省為“webapp.root”

1 2 3 4 5 6 7 8 9 <context-param> <param-name>webAppRootKey</param-name> <param-value> business.root </param-value> </context-param> <!—public-base應用路徑 --> <context-param> <param-name>webAppRootKey</param-name> <param-value> pubbase.root</param-value> </context-param>

在web.xml中該Servlet的定義標記中,比如:

servlet範圍內的參數,只能在servlet的init()方法中取得(或者通過註解配置在對應的類上:@WebServlet("/MyServlet")

System.out.println(this.getInitParameter("param1")=this.getServletConfig().getInitParameter(name));

1 2 3 4 5 6 7 8 <servlet> <servlet-name>TimeServlet</servlet-name> <servlet-class>com.allanlxf.servlet.basic.TimeServlet</servlet-class> <init-param> <param-name>param1</param-name> <param-value>avalible in servlet init()</param-value> </init-param> <load-on-startup>0</load-on-startup>//標記容器是否在啟動的時候就加載這個servlet,執行init()方法,初始化參數</servlet>

配置監聽器:(啟動Web容器時,自動裝配ApplicationContext、log4j的配置信息。)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-configuration/*.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <!-- Spring的log4j監聽器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 與CAS Single Sign Out Filter配合,註銷登錄信息 --> <listener> <listener-class>com.yonyou.mcloud.cas.client.session.SingleSignOutHttpSessionListener</listener-class> </listener>



配置過濾器:

Filter可認為是Servle的一種“加強版”,主要用於對用戶請求request進行預處理,也可以對Response進行後處理,是個典型的處理鏈。使用Filter的完整流程是:Filter對用戶請求進行預處理,接著將請求HttpServletRequest交給Servlet進行處理並生成響應,最後Filter再對服務器響應HttpServletResponse進行後處理。Filter與Servlet具有完全相同的生命周期,且Filter也可以通過<init-param>來配置初始化參數,獲取Filter的初始化參數則使用FilterConfig的getInitParameter()。

換種說法,Servlet裏有request和response兩個對象,Filter能夠在一個request到達Servlet之前預處理request,也可以在離開Servlet時處理response,Filter其實是一個Servlet鏈。以下是Filter的一些常見應用場合,創建一個過濾器,實現Filter接口。重新init(), doFilter(), destroy().

註解的方式配置:@WebFilter(filterName="FirstFilter",urlPatterns={"/index.jsp"},initParams={WWebInitParam(name="mood",value="awake")})

urlPatterns:哪些URL可以過濾,默認“/*”

initParams: 初始化哪些參數。



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 <!--****************************過濾器配置*********************************--> <!-- 字符集過濾器 --> <filter> <filter-name>CharacterEncodingFilter</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> <filter-name>CAS Single Sign Out Filter</filter-name> <filter-class>com.yonyou.mcloud.cas.client.session.SingleSignOutFilter</filter-class> </filter> <!-- 認證過濾器 --> <filter> <filter-name>CAS Authentication Filter</filter-name> <filter-class>com.yonyou.mcloud.cas.client.authentication.ExpandAuthenticationFilter</filter-class> <init-param> <param-name>casServerLoginUrl</param-name> <param-value>https://dev.yonyou.com:443/sso-server/login</param-value> </init-param> <init-param> <!--這裏的server是服務端的IP --> <param-name>serverName</param-name> <param-value>http://10.1.215.40:80</param-value> </init-param> </filter> <!-- 驗證ST/PT過濾器 --> <filter> <filter-name>CAS Validation Filter</filter-name> <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> <init-param> <param-name>casServerUrlPrefix</param-name> <param-value>https://dev.yonyou.com:443/sso-server</param-value> </init-param> <init-param> <param-name>serverName</param-name> <param-value>http://10.1.215.40:80</param-value> </init-param> <init-param> <param-name>proxyCallbackUrl</param-name> <param-value>https://dev.yonyou.com:443/business/proxyCallback</param-value> </init-param> <init-param> <param-name>proxyReceptorUrl</param-name> <param-value>/proxyCallback</param-value> </init-param> <init-param> <param-name>proxyGrantingTicketStorageClass</param-name> <param-value>com.yonyou.mcloud.cas.client.proxy.MemcachedBackedProxyGrantingTicketStorageImpl</param-value> </init-param> <!-- 解決中文問題 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter> <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> </filter> <filter> <filter-name>CAS Assertion Thread Local Filter</filter-name> <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> </filter> <filter> <filter-name>NoCache Filter</filter-name> <filter-class>com.yonyou.mcloud.cas.client.authentication.NoCacheFilter</filter-class> </filter> <!--****************************映射關系配置********************************--> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>NoCache Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CAS Single Sign Out Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CAS Validation Filter</filter-name> <url-pattern>/proxyCallback</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CAS Authentication Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CAS Validation Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>CAS Assertion Thread Local Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

配置失效時間:

1 2 3 <session-config> <session-timeout>120</session-timeout> </session-config>
1 2 3 <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></span>

Servlet初始配置 監聽器和過濾器