1.过滤器实验
首先写两个页面之间能够互相跳转
Test1.html通过超链接跳转到test2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--<a href="/test0808/Test1">Test1</a>-->
<a href="test2.html">test2</a>
</body>
</html>
test2.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3>test2成功</h3>
</body>
</html>
注意:不需要和java包写的过滤器搞什么请求转发,geturl什么乱七八糟的,
下面这个类会直接在后端拦截的
import javax.servlet.*;
import java.io.IOException;
public class HelloFilter implements Filter {
//初始化
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("拦截到一次请求");
}
@Override
public void destroy() {
System.out.println("过滤器被销毁");
}
}
执行结果:
过滤器的放行
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("拦截到一次请求");
filterChain.doFilter(servletRequest,servletResponse);
}