当前位置:   article > 正文

Java 使用过滤器重定向跳转_过滤器重定向到首页

过滤器重定向到首页

实现Filter接口,重写doFilter方法

public class AuthorVerifyFilter implements Filter {

    private FilterConfig config;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
    }
    
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        //跳转至首页
        if(true){
            httpResponse.sendRedirect(httpRequest.getContextPath()+ "/index.action");
            return;
        }
        filterChain.doFilter(httpRequest,httpResponse);
    }

    @Override
    public void destroy() {
        this.config = null;
    }
}

  • 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

在web.xml中配置filter

<filter>
	<filter-name>AuthorVerifyFilter</filter-name>
	<filter-class>com.module.filter.AuthorVerifyFilter</filter-class>
	<init-param>
		<param-name>loginPage</param-name>
		<param-value>/login.action</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>AuthorVerifyFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AI编程探险家/article/detail/61351
推荐阅读
相关标签
  

闽ICP备14008679号