web.xml里<filter-mapping>中的<dispatcher>作用

本文详细介绍了Servlet 2.4版本中<dispatcher>元素的作用,包括REQUEST、FORWARD、INCLUDE和ERROR四种类型,以及如何在<filter-mapping>元素中使用这些类型来配置过滤器的匹配方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2.4版本的servlet规范在部属描述符中新增加了一个<dispatcher>元素,这个元素有四个可能的值:即REQUEST,FORWARD,INCLUDE和ERROR,可以在一个<filter-mapping>元素中加入任意数目的<dispatcher>,使得filter将会作用于直接从客户端过来的request,通过forward过来的request,通过include过来的request和通过<error-page>过来的request。如果没有指定任何< dispatcher >元素,默认值是REQUEST。可以通过下面几个例子来辅助理解。 
例1:

Xml代码   收藏代码
  1. <filter-mapping>   
  2. <filter-name>Logging Filter</filter-name>   
  3. <url-pattern>/products/*</url-pattern>   
  4. </filter-mapping>  

 

这种情况下,过滤器将会作用于直接从客户端发过来的以/products/…开始的请求。因为这里没有制定任何的< dispatcher >元素,默认值是REQUEST。 

例2:

Xml代码   收藏代码
  1. <filter-mapping>   
  2. <filter-name>Logging Filter</filter-name>   
  3. <servlet-name>ProductServlet</servlet-name>   
  4. <dispatcher>INCLUDE</dispatcher>   
  5. </filter-mapping>   

这种情况下,如果请求是通过request dispatcher的include方法传递过来的对ProductServlet的请求,则要经过这个过滤器的过滤。其它的诸如从客户端直接过来的对ProductServlet的请求等都不需要经过这个过滤器。 
指定filter的匹配方式有两种方法:直接指定url-pattern和指定servlet,后者相当于把指定的servlet对应的url-pattern作为filter的匹配模式 
filter的路径匹配和servlet是一样的,都遵循servlet规范中《SRV.11.2 Specification of Mappings》一节的说明 

例3:

Xml代码   收藏代码
  1. <filter-mapping>   
  2. <filter-name>Logging Filter</filter-name>   
  3. <url-pattern>/products/*</url-pattern>   
  4. <dispatcher>FORWARD</dispatcher>   
  5. <dispatcher>REQUEST</dispatcher>   
  6. </filter-mapping>   

 

在这种情况下,如果请求是以/products/…开头的,并且是通过request dispatcher的forward方法传递过来或者直接从客户端传递过来的,则必须经过这个过滤器。
参考资料:http://topic.youkuaiyun.com/t/20060322/23/4633313.html

<?xml version="1.0" encoding="UTF-8"?> <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_3_0.xsd" version="3.0"> <display-name></display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext*.xml classpath*:*Context.xml </param-value> </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>rms.root</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>com.hg.filter.UrlAuthListen</listener-class> </listener> <listener> <listener-class>com.hg.filter.WebConfigListener</listener-class> </listener> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter </filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>XssFilter</filter-name> <filter-class>com.hg.aop.XssFilter</filter-class> </filter> <filter-mapping> <filter-name>XssFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- dwr 配置 --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <description>How quickly do scriptSessions timeout?</description> <param-name>scriptSessionTimeout</param-name> <param-value>1800000</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <listener> <listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener</listener-class> </listener> <!-- dwr 配置 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>具体解释一下web.xml的配置
最新发布
07-08
<?xml version="1.0" encoding="UTF-8"?> <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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>web application framework</display-name> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/waf</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <session-config> <session-timeout>60</session-timeout> <cookie-config><name>JSESSIONID</name></cookie-config> </session-config> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring_context.xml ,classpath:quartz_context.xml ,classpath:spring_context_misc.xml ,classpath:bpms_context.xml, classpath:process_group.xml </param-value> </context-param> <listener> <listener-class>cn.ccccltd.waf.listener.SessionListener</listener-class> </listener> <listener> <listener-class>cn.ccccltd.waf.listener.CacheFileEvent</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class> </listener> <listener> <listener-class>cn.ccccltd.bpms.BpmsInitializer</listener-class> </listener> <listener> <listener-class>cn.ccccltd.bpms.ou.listener.BpmsSessionListener</listener-class> </listener> <filter> <filter-name>SigninFilter</filter-name> <filter-class>cn.ccccltd.waf.SigninFilter</filter-class> <init-param> <param-name>signinUrlPattern</param-name> <param-value>bo,dhtml,dhtm,shtml,shtm</param-value> </init-param> </filter> <filter-mapping> <filter-name>SigninFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- ****************************************************************************** --> <filter> <filter-name>xssFilter</filter-name> <filter-class>cn.ccccltd.waf.filter.XssFilter</filter-class> </filter> <filter-mapping> <filter-name>xssFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>SSOLoginFilter</filter-name> <filter-class>cn.ccccltd.waf.sso.controller.SSORequestController</filter-class> </filter> <filter-mapping> <filter-name>SSOLoginFilter</filter-name> <url-pattern>*.bo</url-pattern> </filter-mapping> <!--<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>--> <servlet> <servlet-name>appDispatcher</servlet-name> <servlet-class>cn.ccccltd.waf.AppDispatcher</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring_dispatcher_context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring MVC 请求映射 --> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.bo</url-pattern> </servlet-mapping> <servlet> <servlet-name>appLoader</servlet-name> <servlet-class>cn.ccccltd.waf.AppLoader</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet> <servlet-name>monitorLoader</servlet-name> <servlet-class>cn.ccccltd.waf.MonitorLoader</servlet-class> <load-on-startup>30</load-on-startup> </servlet> <servlet> <servlet-name>fileUpload</servlet-name> <servlet-class>cn.ccccltd.waf.tool.FileUpload</servlet-class> <load-on-startup>97</load-on-startup> </servlet> <servlet> <servlet-name>randomCode</servlet-name> <servlet-class>cn.ccccltd.waf.tool.RandomCode</servlet-class> <load-on-startup>99</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.dhtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.dhtm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.shtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>appDispatcher</servlet-name> <url-pattern>*.shtm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>fileUpload</servlet-name> <url-pattern>/waf/tool/fileUpload</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>randomCode</servlet-name> <url-pattern>/waf/tool/randomCode</url-pattern> </servlet-mapping> <servlet> <servlet-name>FileUploadServlet</servlet-name> <servlet-class>cn.ccccltd.comm.upload.FileUploadServlet</servlet-class> <init-param> <param-name>fileSizeLimit</param-name> <param-value>2000</param-value> </init-param> </servlet> <servlet> <servlet-name>DeleteFileServlet</servlet-name> <servlet-class>cn.ccccltd.comm.upload.DeleteFileServlet</servlet-class> </servlet> <servlet> <servlet-name>FileDownloadServlet</servlet-name> <servlet-class>cn.ccccltd.comm.upload.FileDownloadServlet</servlet-class> </servlet> <!-- 帆软报表 --> <!-- <servlet> <servlet-name>ReportServer</servlet-name> <servlet-class>com.fr.web.ReportServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ReportServer</servlet-name> <url-pattern>/ReportServer</url-pattern> </servlet-mapping> --> <servlet-mapping> <servlet-name>FileDownloadServlet</servlet-name> <url-pattern>/FileDownloadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>FileUploadServlet</servlet-name> <url-pattern>/FileUploadServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DeleteFileServlet</servlet-name> <url-pattern>/DeleteFileServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- error-page START --> <!-- 400 --> <error-page> <error-code>400</error-code> <location>/errorPage.jsp</location> </error-page> <!-- 403 --> <error-page> <error-code>403</error-code> <location>/errorPage.jsp</location> </error-page> <!-- 404 --> <error-page> <error-code>404</error-code> <location>/errorPage.jsp</location> </error-page> <!-- 500 --> <error-page> <error-code>500</error-code> <location>/errorPage.jsp</location> </error-page> <!-- error-page END --> </web-app>那你给我修改一下文件都在项目之中。
06-11
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>Web Struts</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>default.htm</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>根据这个代码改进这个代码<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值