Fork me on GitHub

servlet_4

 

 

过滤器入门
	过滤器的概念及执行基本流程
	过滤器的使用场景
	过滤器的实现及基本配置


过滤器链
	过滤器链的配置
		使用注解的方式无法保证过滤器链的执行顺序,所以只能使用web.xml的配置
		按照出现在web.xml的filter-mapping顺序依次经过过滤器
		
异步过滤器
	与异步servlet类似
	
	异步过滤器的配置
	注解方式:设置@webfilter的属性asyncsupport属性为true
	web.xml方式:

与请求分派协作
	dispatcherTypes=DispatcherType.INCLUDE
	

 

 

包装器
	包装器的概念
	包装器类的使用
	
	在servlet api中提供了4个包装器类
	与装饰器的设计模式相类似
	允许将任何方法都包装到servletrequest...之中
	
	javax.servlet.ServletRequestWrapper,在其后加wrapper

 

 



一、servlet
package com.fgy; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/servlet6") public class Servlet6 extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println(request.getParameter("content")); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } =============================================== 二、过滤器 package com.fgy; import java.io.IOException; import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; @WebFilter(urlPatterns="*") public class Filter2 implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(new MyWrapper((HttpServletRequest) request), response); } public void init(FilterConfig fConfig) throws ServletException { } } ============================================= 三、包装器 进行修改,实现将请求参数的值1替换为one package com.fgy; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; public class MyWrapper extends HttpServletRequestWrapper { private HttpServletRequest request; public MyWrapper(HttpServletRequest request) { super(request); this.request = request; } @Override public String getParameter(String name) { String value = request.getParameter(name); return value.replaceAll("1", "one"); } }

 

posted on 2018-05-27 18:36  阳光-源泉  阅读(263)  评论(0编辑  收藏  举报

导航