过滤器Filter配置web.xml(SSM、SSH)

25 篇文章 0 订阅
5 篇文章 0 订阅

过滤器的作用:
过滤器是一个对象,可以传输请求或修改响应(它可以在请求到达Servlet/JSP之前对其进行预处理,而且能够在响应离开Servlet/JSP之后对其进行后处理)。所以如果你有几个Servlet/JSP需要执行同样的数据转换或页面处理的话,你就可以写一个过滤器类,然后在部署描述文件(web.xml)中把该过滤器与对应的Servlet/JSP联系起来。你可以一个过滤器以作用于一个或一组servlet,零个或多个过滤器能过滤一个或多个servlet。一个过滤器实现java.servlet.Filter接口并定义它的三个方法:

1. void init(FilterConfig config) throws ServletException:在过滤器执行service前被调用,以设置过滤器的配置对象。

2. void destroy();在过滤器执行service后被调用。

3. Void doFilter(ServletRequest req,ServletResponse res,FilterChain chain) throws IOException,ServletException;处理filter业务

一般部署这几个在描述文件(web.xml)中:

Spring的过滤器或Hibernate的过滤器:(因为使用SSH,Tomcat启动前必须配置这些过滤器和监听器,最重要的还有“applicationContext”路径)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- 配置spring的参数 有了《param-value》classpath:applicationContext.xml,在类中无需写固定xml文件 -->
    <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>  
        <!-- 配置spring的监听器 -->  
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>	
    <listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>
	<!-- 配置spring(自带)的过滤器 -->  
	<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>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
	<!-- 配置spring(自带)的hibernate过滤器 --> 
	<filter>
		<filter-name>hibernateFilter</filter-name>
		<filter-class>
			org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
		</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>hibernateFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置500和404错误跳转对应页面 -->
  <error-page>
  	<error-code>500</error-code>
  	<location>/error.jsp</location>
  </error-page>
   <error-page>
  	<error-code>404</error-code>
  	<location>/error.jsp</location>
  </error-page>
</web-app>

在Spring包里:

《==》Or 《==》 

提取链接:https://pan.baidu.com/s/1JpshKpqzTUDOVJsKZlXF9Q,提取码:j932 

org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

ssh中org.springframework.orm.hibernate4.support.OpenSessionInViewFilter的作用及配置【我的是hibernate3,但也差不多】 

Struts的过滤器:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!--
	apache译为服务器 dispatcher译为分配器
	
	过滤器的配置
	
	其中:
	    <filter-name>struts2</filter-name>标注的内容一般都是小写开头,这个内容起"线索/标记"作用
	    【标签filter-mapping通过<filter-name>struts2</filter-name>找到内容也为struts2的filter标签】
	    
		filter-mapping标签里的
		<url-pattern>/*</url-pattern>的"/*",说明过滤所有文件
	
	拦截器工作原理:
	StrutsPrepareAndExecuteFilter(其中初始化一个ActionProxy实例,并调用它的execute()方法)
	——》在ActionProxy类中拦截器方法会拦截并处理用户请求——》然后才到Action的execute()
	方法处理用户请求——》(在ActionProxy类中)Result,返回一个逻辑视图名,系统负责将该逻辑视图对应的资源显示给用户
	-->
	<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>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

在Struts的核心包里:

提取链接:https://pan.baidu.com/s/1Zq7sBBGTI1o8cY835LnOGQ,提取码:b4lz

了解:SSH异常——The Struts dispatcher cannot be found、Method: getClassInstance Line: 220 - com/opensy 【若Tomcat启动前未配置,会报什么异常,有三种】

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yvette_QIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值