我在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);
有人对此有想法吗?
最佳答案
改成
String h = httpRequest.getHeader("X-Requested-With");
boolean isAjax = (h == null?false:h.indexOf("XMLHttpRequest")>=0);
如果这是一个ajax请求,则应将
isAjax
评估为true
。