码迷,mamicode.com
首页 > 其他好文 > 详细

Filter

时间:2015-05-31 00:09:17      阅读:168      评论:0      收藏:0      [点我收藏+]

标签: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;
			}
		}



Filter

标签:filter   过滤器   

原文地址:http://blog.csdn.net/wodewutai17quiet/article/details/46279589

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!