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

      

  • 相关阅读:
    JSONObject对象和JSONArray对象的使用
    toString和getString的区别
    Pytorch 实验中非常有效的代码段
    TeXworks 高效使用指南
    linux 设置新用户
    ICML-21 待读的 Paper
    2019年12月19日记戴老师的讲座
    matplotlib 调整图片的 font size
    IJCAI 投稿 Type-3 字体
    matplotlib 中 figsize 和 dpi 之间的关系
  • 原文地址:https://www.cnblogs.com/doudou2018/p/9863619.html
Copyright © 2011-2022 走看看