zoukankan      html  css  js  c++  java
  • Filter 中空指针错误

    Filter 是过滤器,凡是通过servlet  JSP 的请求需要filter 进行过滤或者拦截操作,保证数据的合法或者逻辑正确性

    但是写第一个filter 配置完成后,发现jsp 文件进不去了,直接是500服务器错误,指示空指针错误

    最后发现了不是jsp 文件写错,而是filter 中没有判空

    比如我在登陆成功前提下在session 域中添加了一个字段值,但是没有登陆的情况下session 没有该字段值,取该值判断应该先判断非空,在进行操作,就不会产生空指针错误了

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
            response.setContentType("text/html;charset=utf-8");
    
            String contextPath = request.getContextPath();
            String requestURI = request.getRequestURI();
    
            HttpSession session = request.getSession(false);
            //思路,放行首页和登陆页||登陆后所有页面
            //放行首页和登陆页
            if (requestURI.equals("/index.jsp")||requestURI.equals("/")||requestURI.equals("/login.jsp")||requestURI.equals("/person")
            ){
                chain.doFilter(req,resp);
                return;
            }
            //放行登陆后状态
            if (session!=null){
                String checked = (String) session.getAttribute("checked");// null 问题
                if (checked!=null){
                    chain.doFilter(req,resp);
                    return;
                }
    }
            //不放行,返回首页,要求登陆
            response.getWriter().println("请先登陆");
            response.setHeader("Refresh","2;url="+contextPath+"/login.jsp");
    
    
    
    
        }
    

      

  • 相关阅读:
    mysql联合索引命中条件
    Shiro知识初探(更新中)
    Java中使用MongoTemplate进行分批处理数据
    Java中String时间范围比较
    使用ReentrantLock
    使用Condition
    python的坑--你知道吗?
    python基础--函数全解析(1)
    CSS基本语法及页面引用
    HTML学习汇总
  • 原文地址:https://www.cnblogs.com/doudou2018/p/9863619.html
Copyright © 2011-2022 走看看