问答详情
源自:5-2 [Java Web] 过滤器的分类2

使用eclipse编写error,为什么跳转不到编写的error.jsp页面,仍然是404

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <display-name></display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <filter>
      <filter-name>FirstFilter</filter-name>
      <filter-class>com.imooc.filter.FirstFilter</filter-class>
  </filter><filter-mapping>
      <filter-name>FirstFilter</filter-name>
      <url-pattern>/index.jsp</url-pattern></filter-mapping>
  <!--  
  <filter-mapping>
      <filter-name>FirstFilter</filter-name>
      <url-pattern>/main.jsp</url-pattern>
      <dispatcher>FORWARD</dispatcher></filter-mapping>
  -->
  <!--  
  <filter>
      <filter-name>SecondFilter</filter-name>
      <filter-class>com.imooc.filter.SecondFilter</filter-class></filter>
  <filter-mapping>
      <filter-name>SecondFilter</filter-name>
      <url-pattern>/index.jsp</url-pattern></filter-mapping>
  -->
  <error-page>
    <error-code>404</error-code>
    <location>/error.jsp</location>
  </error-page>
       
</web-app>
package com.imooc.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstFilter implements Filter {

    @Override
    public void destroy() {
        System.out.println("destory-----FirstFilter");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        System.out.println("start---FirstFilter--doFilter");
//        chain.doFilter(request, response);
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse response2 = (HttpServletResponse) response;
        //重定向
        response2.sendRedirect(req.getContextPath()+"/main.jsp");
        //转发
//        req.getRequestDispatcher("main.jsp").forward(request, response);
//        req.getRequestDispatcher("main.jsp").include(request, response);
        System.out.println("end---FirstFilter--doFilter");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init-----FirstFilter");
    }

}

以上分别是web.xml和FirstFilter的代码,请大神告知下

提问者:慕雪1014944 2017-07-17 03:52

个回答

  • flygoxc
    2017-07-18 23:02:51
    已采纳

    需要配置isErrorPage

  • Silent_Walke
    2019-04-13 14:43:34

    试了一个下午,发现随便写一个简单的jsp页面是不可以转进去的,即使配置isELIgnored="true"%也不可以,上网查了一下,错误页面要大于50k,也就是error.jsp不能太小。