自定义Filter禁止用户直接访问JSP页面

时间:2021-11-24 17:12:43

自定义Filter:

 1 package gsxy.gswbm.util;
 2 import java.io.IOException;
 3 
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 public class MyFilterJsp implements Filter{
11     public void init(FilterConfig config) throws ServletException{} //初始化过滤器
12     public void destroy(){}//过滤器的生命周期结束
13 
14     public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
15         throws IOException, ServletException{
16         request.getRequestDispatcher("/login.html").forward(request, response);
17     }
18 }

web.xml配置:

1 <filter>
2     <filter-name>MyFilterJsp</filter-name>
3     <filter-class>gsxy.gswbm.util.MyFilterJsp</filter-class>
4 </filter>
5 <filter-mapping>
6     <filter-name>MyFilterJsp</filter-name>
7     <url-pattern>*.jsp</url-pattern>
8 </filter-mapping>