拦截路径配置
代码演示
package com.ithuima.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain Chain) throws IOException, ServletException {
//1.方向前对request数据进行处理
System.out.println("1FilterDemo...");
//放行
Chain.doFilter(request,response);
//2.方向后对response 数据进行处理
System.out.println("5.FilterDemo...");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
package com.ithuima.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo2 implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain Chain) throws IOException, ServletException {
//1.放行前对request数据进行处理
System.out.println("2FilterDemo...");
//放行
Chain.doFilter(request,response);
//2.放行后对response 数据进行处理
System.out.println("4.FilterDemo...");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
jsp文件
<html>
<body>
<h1>Hello jsp!</h1>
<%
System.out.println("3hello.jsp");
%>
</body>
</html>
<html>
<body>
<h2>Hello World!</h2>
<%
System.out.println("index.jsp");
%>
</body>
</html>