ITeye博客

自定义Filter过滤器,过滤所有jsp页面

ycjunhua · 2009-03-11 21:40 · java · HTML · XML · JSP · Web · Servlet · Markdown

转载地址: http://www.cnblogs.com/love2009/archive/2009/02/24/1397102.html 开发中,常常需要对指定的请求格式进行过滤。 比如,Struts2的应用中,往往要过滤掉 *.jsp的请求 ※http://127.0.0.1:8080/Sample/login.action这样的请求被允许。 ※http://127.0.0.1:8080/Sample/login.jsp这样的请求应该被过滤,并指定跳转到某个页面或执行某个Action等等 Struts2中的过滤器负责过滤所有的*.action,然后进行处理。 同样可以自定义一个Filter类,来单独负责某种格式请求的处理。 实现javax.servlet.Filter接口 FilterClass.java 1 import javax.servlet.*; 2 import javax.servlet.http.HttpServletResponse; 3 4 import java.io.*; 5 /** 6 * 过滤所有 「*.jsp」的请求 7 * 通过实现servlet中的Filter接口 8 */ 9 public class FilterClass implements Filter { 10 11 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, 12 FilterChain chain) throws IOException, ServletException { 13 // Sample---所有的*.jsp的请求,跳转到Action 14 HttpServletResponse response = (HttpServletResponse) servletResponse; 15 response.sendRedirect("login.action"); 16 } 17 18 public void destroy() { 19 // TODO Auto-generated method stub 20 21 } 22 23 public void init(FilterConfig arg0) throws ServletException { 24 // TODO Auto-generated method stub 25 26 } 27 } 在web.xml配置文件中,添加Filter过滤 Code sample test.FilterClass sample *.jsp 这样,所有的*.jsp请求都会被过滤,跳转到login.action

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。