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

servlet_4

时间:2018-05-27 19:02:01      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:使用场景   ace   oid   exce   port   cte   ack   clu   分派   

 

 

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


过滤器链
	过滤器链的配置
		使用注解的方式无法保证过滤器链的执行顺序,所以只能使用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"); } }

 

servlet_4

标签:使用场景   ace   oid   exce   port   cte   ack   clu   分派   

原文地址:https://www.cnblogs.com/createyuan/p/9096877.html

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