首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

字符集过滤类的 doFilter 方法的空值错误

2012-02-02 
字符集过滤类的 doFilter 方法的空值异常我的tomcat服务器运行后,日志信息里记录着程序一直在抛出这个异常

字符集过滤类的 doFilter 方法的空值异常
我的tomcat服务器运行后,日志信息里记录着程序一直在抛出这个异常:
2007-03-09   00:54:11   StandardWrapperValve[action]:   Servlet.service()   for   servlet   action   threw   exception
java.lang.NullPointerException
at   org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:372)
at   org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229)
at   org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at   org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at   javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at   filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:28)
at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:186)
at   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at   org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at   org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at   org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at   org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at   org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at   org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at   org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at   org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at   org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at   org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at   org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at   java.lang.Thread.run(Thread.java:595)

根据堆栈看,好象是说我的   字符集过滤类的   doFilter   方法的空值异常  
但是这个类我实在是找不的参数传递的都是   容器对象,涉及到tomcat更底层支持类,我找不出解决错误的方法,请大家帮帮忙。小弟不胜感激!
SetCharacterEncodingFilter.java

package   filters;

import   java.io.IOException;
import   javax.servlet.Filter;
import   javax.servlet.FilterChain;
import   javax.servlet.FilterConfig;
import   javax.servlet.ServletException;
import   javax.servlet.ServletRequest;
import   javax.servlet.ServletResponse;

public   class   SetCharacterEncodingFilter   implements   Filter{


protected   String   encoding   =   null   ;
protected   FilterConfig   filterConfig   =   null   ;
protected   boolean   ignore   =   true   ;

public   void   destroy(){
this.encoding   =   null   ;
this.filterConfig   =   null   ;
}

public   void   doFilter(   ServletRequest   request   ,   ServletResponse   response   ,   FilterChain   chain)
throws   IOException   ,   ServletException{
if   (ignore   ||   (request.getCharacterEncoding()   ==   null)){
String   encoding   =   selectEncoding(request);
if   (encoding   !=   null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request   ,   response);
}

public   void   init(FilterConfig   filterConfig)   throws   ServletException{
this.filterConfig   =   filterConfig   ;
this.encoding   =   filterConfig.getInitParameter( "encoding ");
String   value   =   filterConfig.getInitParameter( "ignore ");
if   (value   ==   null)
this.ignore   =   true   ;
else   if   (value.equalsIgnoreCase( "true "))
this.ignore   =   true   ;  
else   if   (value.equalsIgnoreCase( "yes "))
this.ignore   =   true   ;
else  
this.ignore   =   false   ;
}

protected   String   selectEncoding(   ServletRequest   request){
return   this.encoding;
}

}  

java类的第28行:
chain.doFilter(request   ,   response);  
感觉应该是说chain   这个对象是空的。但不知道怎么解决。请大家给点意见。



[解决办法]

//假设request.getCharacterEncoding() == null成立 执行以下代码
看还是不是你想要的结果
public void doFilter( ServletRequest request , ServletResponse response , FilterChain chain)
throws IOException , ServletException{
if (ignore || (request.getCharacterEncoding() == null)){
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request , response);
}

[解决办法]
if (encoding != null)
request.setCharacterEncoding(encoding);
}


你这里面传进来的是什么编码啊?
filterConfig.getInitParameter( "encoding ");

热点排行