• servlet过滤器


    servlet的过滤器解决了是否登录状态问题。

    1.创建一个AccessFilter类,基础代码

    package valid.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 AccessFilter implements Filter{
        public void destroy() {
            System.out.println("请求销毁");
        }
        @Override
        public void doFilter(ServletRequest res, ServletResponse rep,
                FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) res;
            HttpServletResponse response = (HttpServletResponse) rep;
            
            String requestURI = request.getRequestURI();
            String path = request.getContextPath();
            //如果请求路径等于首页ctl
            if(requestURI.startsWith(path + "/servlet/valServlet")){
                chain.doFilter(res, rep);
            }
            else{
                //这个时候,就说明需要判断权限了
                Object user = request.getSession().getAttribute("login");
                if(null == user){
                    response.sendRedirect("../index.jsp");
                }else{
                    chain.doFilter(res, rep);
                }
            }
            //放行
        }
        public void init(FilterConfig arg0) throws ServletException {
            System.out.println("请求初始化");
        }
    
    }

    2.web.xml配置

    <filter>
         <filter-name>acess</filter-name>
         <filter-class>valid.filter.AccessFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>acess</filter-name>
         <url-pattern>/servlet/*</url-pattern>
     </filter-mapping>

    所有servlet请求,都会先经过AccessFilter,只要没有登录状态,都将会被拦截,并跳转到首页

  • 相关阅读:
    改变GMF应用程序画布的布局
    Eclipse 3.2下载最多的国家和地区
    让输出的Plugin文件名里包含当前时间
    把SWT包装成Plugin需要修改的地方
    在程序里隐藏但利用Resource Navigator
    GMF应用程序设置背景图片
    给GMF应用程序添加自定义Action
    Graphical Modeling Framework简介
    GMF常见问题
    EReference的containment和container属性
  • 原文地址:https://www.cnblogs.com/angto64/p/5117066.html
Copyright © 2020-2023  润新知