Filter

过滤器 (Filter)
	1、概述:过滤器概述过滤器就像一个保安,可以对请求和响应进行拦截。
	2、编写过滤的步骤:
		1)编写一个类,实现javax.servlet.Filter接口,这样的类一般称之为过滤器类
			public class FilterDemo1 implements Filter {

				public void init(FilterConfig filterConfig) throws ServletException {
					System.out.println("FilerDemo1初始化了");
				}
			
				//对于每一次的用户访问,都会调用该方法。
				public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
					throws IOException, ServletException {

					System.out.println("FilterDemo1拦截前");
					chain.doFilter(request, response);//放行,让目标资源执行
					System.out.println("FilterDemo1拦截后");
				}

				public void destroy() {
					// TODO Auto-generated method stub
				}
			}
		2)在web.xml中进行配置,要拦截哪些资源。
			<filter>
				<filter-name>FilterDemo1</filter-name>
				<filter-class>cn.jxn.filter.FilterDemo1</filter-class>
			</filter>
			<filter-mapping>
				<filter-name>FilterDemo1</filter-name>
				<url-pattern>/*</url-pattern>
			</filter-mapping>
	3、过滤器的执行过程:
		1)过滤器只会被初始化一次,应用被加载时就完成了初始化。
		2)多个过滤器的拦截顺序是按照web.xml中filter-mapping元素的出现顺序进行拦截的。
	
	4、过滤器简单案例:
		1)界面输出中文及中文请求参数(POST方式有效)编码过滤器:SetCharacterEncodingFilter.java
			web.xml中的配置:
			<filter>
				<filter-name>SetCharacterEncodingFilter</filter-name>
				<filter-class>cn.jxn.filter.example.SetCharacterEncodingFilter</filter-class>
				<init-param>
					<param-name>encoding</param-name>
					<param-value>UTF-8</param-value>
				</init-param>
			  </filter>
			  <filter-mapping>
				<filter-name>SetCharacterEncodingFilter</filter-name>
				<url-pattern>/*</url-pattern>
			  </filter-mapping>
			
			//界面输出中文及中文请求参数(POST方式有效)编码过滤器
			public class SetCharacterEncodingFilter implements Filter {
			
				private FilterConfig filterConfig;
				
				public void destroy() {
					// TODO Auto-generated method stub
				}

				public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
					throws IOException, ServletException {

					String encodingValue = filterConfig.getInitParameter("encoding");//获取过滤器配置中的参数
					if(encodingValue==null)//如果没有配置该参数
						encodingValue="UTF-8";//给定默认值UTF-8
					request.setCharacterEncoding(encodingValue);//只对POST请求方式有用
					response.setContentType("text/html;charset="+encodingValue);
					chain.doFilter(request, response);
				}

				public void init(FilterConfig filterConfig) throws ServletException {
					this.filterConfig = filterConfig;
				}
			}
		2)控制动态资源(Servlet JSP)不要缓存的过滤器
			web.xml中的配置
			<filter>
				<filter-name>NoCacheFilter</filter-name>
				<filter-class>cn.jxn.filter.example.NoCacheFilter</filter-class>
			</filter>
			<filter-mapping>
				<filter-name>NoCacheFilter</filter-name>
				<url-pattern>*.jsp</url-pattern>
			</filter-mapping>
			<filter-mapping>
				<filter-name>NoCacheFilter</filter-name>
				<url-pattern>/servlet/*</url-pattern>
			</filter-mapping>
		
			// 控制动态资源(Servlet JSP)不要缓存的过滤器
			public class NoCacheFilter implements Filter {

				public void destroy() {
					// TODO Auto-generated method stub
				}

				public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
					throws IOException, ServletException {

					HttpServletRequest request = (HttpServletRequest)req;
					HttpServletResponse response = (HttpServletResponse)resp;
					response.setDateHeader("Expires", 0);
					response.setHeader("Cache-Control", "no-cache");
					response.setHeader("Pragma", "no-cache");
					chain.doFilter(request, response);
				}

				public void init(FilterConfig filterConfig) throws ServletException {
				
				}
			}
			3)控制html、css、js等静态资源的缓存时间的过滤器
				web.xml中的配置
				<filter>
					<filter-name>NeedCacheFilter</filter-name>
					<filter-class>cn.jxn.filter.example.NeedCacheFilter</filter-class>
					<init-param>
						<param-name>html</param-name>
						<param-value>1</param-value><!-- 单位是小时 -->
					</init-param>
					<init-param>
						<param-name>css</param-name>
						<param-value>2</param-value><!-- 单位是小时 -->
					</init-param>
					<init-param>
						<param-name>js</param-name>
						<param-value>3</param-value><!-- 单位是小时 -->
					</init-param>
				</filter>
				<filter-mapping>
					<filter-name>NeedCacheFilter</filter-name>
					<url-pattern>*.html</url-pattern>
				</filter-mapping>
				<filter-mapping>
					<filter-name>NeedCacheFilter</filter-name>
					<url-pattern>*.css</url-pattern>
				</filter-mapping>
				<filter-mapping>
					<filter-name>NeedCacheFilter</filter-name>
					<url-pattern>*.js</url-pattern>
				</filter-mapping>
			
			public class NeedCacheFilter implements Filter {
			
				private FilterConfig filterConfig;
				
				public void destroy() {
					// TODO Auto-generated method stub
				}

				public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) 
					throws IOException, ServletException {

					System.out.println("控制缓存时间的过滤器过滤了");
					HttpServletRequest request = (HttpServletRequest)req;
					HttpServletResponse response = (HttpServletResponse)resp;
					//获取html、css、js各自的缓存时间,如果没有,给个默认值是1小时
					int time = 1;
					String uri = request.getRequestURI();//   /day19/1.html
					String extendName = uri.substring(uri.lastIndexOf(".")+1);
					
					if("html".equals(extendName)){
						//访问的html资源
						String value = filterConfig.getInitParameter("html");
						if(value!=null){
							time = Integer.parseInt(value);
						}
					}
					if("css".equals(extendName)){
						//访问的css资源
						String value = filterConfig.getInitParameter("css");
						if(value!=null){
							time = Integer.parseInt(value);
						}
					}
					if("js".equals(extendName)){
						//访问的js资源
						String value = filterConfig.getInitParameter("js");
						if(value!=null){
							time = Integer.parseInt(value);
						}
					}
					response.setDateHeader("Expires", System.currentTimeMillis()+time*60*60*1000);
					chain.doFilter(request, response);
				}

				public void init(FilterConfig filterConfig) throws ServletException {
					this.filterConfig = filterConfig;
				}
			}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值