过滤器Filter实现页面跳转

当我们想对不合法或不存或不符合要求的请求进行跳转时,可已选择Filter来实现,下面是简单的例子

只需要在doFilter中实现内部逻辑即可

//业务逻辑
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
request.getRequestDispatcher("/views/404.html").forward(request,response);
return;  

切记页面跳转,一定到return结束掉否则会出现

java.lang.IllegalStateException: Cannot forward after response has been committed
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:326)
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:316)

你可能感兴趣的:(java,Java基础)