filter拦截


xml

1
2
3
4
5
6
7
8
9
10
11
12
<!--拦截请求-->
<filter>
<filter-name>ManagerFilter</filter-name>
<filter-class>com.white.filter.MangerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ManagerFilter</filter-name>
<url-pattern>/pages/*</url-pattern>
<url-pattern>/clientByueServlet</url-pattern>
<url-pattern>/byueServlet</url-pattern>
</filter-mapping>
</web-app>

filter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.white.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


public class MangerFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
Object user = httpServletRequest.getSession().getAttribute("username");
if (user == null){
httpServletRequest.getRequestDispatcher("index.jsp").forward(servletRequest,servletResponse);
System.out.println("已拦截请求");
}else{
filterChain.doFilter(servletRequest,servletResponse);
}

}

@Override
public void destroy() {

}
}

emm报告会

一言难尽