FilterChain doFilter方法中的java.lang.NullPointerException

时间:2014-03-12 12:19:42

标签: java ajax servlets filter struts

我在web.xml部署描述符中添加了一个过滤器。但是每当请求到达服务器时,过滤器都会抛出NullPointerException

java.lang.NullPointerException 
org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:427)
org.apache.commons.beanutils.PropertyUtilsBean.getIndexedProperty(PropertyUtilsBean.java:340) 
org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:664) 
org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715) 
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:884) 
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811) 
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298) 
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252) 
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:821) 
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254) 
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482) 
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

下面给出了发生异常的代码段。

public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)     throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;

    boolean isAjax = "XMLHttpRequest".equals(httpRequest.getHeader("x-requested-with"));


    HttpSession httpSession = httpRequest.getSession(false);

    if(isAjax){
        if(isValidRequest(httpRequest, httpSession)){
            chain.doFilter(request, response);
        }else{
            returnInvalidMessage(httpRequest, httpResponse);
        }
    }else{
        chain.doFilter(request, response); /* Here is the exception occures */
    }
}

以下行发生了异常

chain.doFilter(request, response);

有人对此有所了解吗?

2 个答案:

答案 0 :(得分:1)

更改为

 String h = httpRequest.getHeader("X-Requested-With");
 boolean isAjax = (h == null?false:h.indexOf("XMLHttpRequest")>=0);
如果这是ajax请求,则应将{p> isAjax评估为true

答案 1 :(得分:0)

如果您的项目在tomcat上运行,可能是您的项目jar问题。

  

jsp-api.jar和servlet-api.jar

与tomcat的jar冲突。所以只需删除两个jar