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");
    
    
    
    
        }
    

      

  • 相关阅读:
    luogu P3376 【模板】网络最大流
    cogs 774. [USACO Open09] 捉迷藏
    1002. A+B for Polynomials (25) (浮点数判0)
    1001. A+B Format (20) (%0nd)
    7-28 搜索树判断(25 分)
    7-27 家谱处理(30 分)
    7-26 Windows消息队列(25 分)(堆排序)
    7-25 朋友圈(25 分)(并查集)
    7-24 树种统计(25 分)(二叉排序的应用)
    7-23 还原二叉树(25 分)
  • 原文地址:https://www.cnblogs.com/doudou2018/p/9863619.html
Copyright © 2011-2022 走看看