注解配置:
设置dispatcherType属性
- REQUEST: 默认值 , 浏览器直接请求资源
- FORWARD: 转发访问资源
- INCLUDE: 包含访问资源
- ERROR: 错误跳转资源
- ASYNC: 异步访问资源
FilterDemo5
//浏览器直接请求index.jsp资源时, 该过滤器会被执行
@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo5 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("FilterDemo5...");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}
ServletDemo2
@WebServlet("/user/updateServlet")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("updateServlet....");
//转发到index.jsp
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}