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

为什么我这个过滤器一点反应都没有呢?

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>TestFilter</title>
</head>
<body>
<h1>这是main.jsp</h1>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>TestFilter</title>
</head>
<body>
tttttttttttttttttttttttttttttt
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <filter>
        <filter-name>FirstFilter</filter-name>
        <filter-class>com.filter.FirstFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>FirstFilter</filter-name>
        <url-pattern>/main.jsp</url-pattern>
    </filter-mapping>
</web-app>
public class FirstFilter implements javax.servlet.Filter{


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

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("start.....................doFirstFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //重定向
//        response.sendRedirect(request.getContextPath()+"/index.jsp");
        //转发
        request.getRequestDispatcher("index.jsp").forward(servletRequest,servletResponse);
//        filterChain.doFilter(servletRequest,servletResponse);
        System.out.println("end.......................doFirstFilter");
    }

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


想通过过滤器跳转到到index.jsp中,转发和重定向都试过了,还是一点反应都没

提问者:moummm 2017-08-30 00:51

个回答

  • 慕粉3680658
    2017-08-30 22:30:31

    dofilter方法中你已设定了传入参数为servletRequest,但你用的是自定义的request 我觉得问题可能是这里,我也是新手