servlet过滤器为我的jsf应用程序抛出各种异常

时间:2014-04-10 11:58:19

标签: jsf tomcat servlets primefaces

你好堆栈溢出,

如果我尝试在jsf应用程序中使用servlet过滤器,则会出现以下异常。如果没有Servlet过滤器,应用程序将顺利运行。 我使用的是apache tomcat 7.0.4版。

   java.lang.NullPointerException
    at org.apache.tomcat.util.http.MimeHeaders.clear(MimeHeaders.java:151)
    at org.apache.coyote.Response.recycle(Response.java:536)
    at org.apache.coyote.http11.AbstractOutputBuffer.nextRequest(AbstractOutputBuffer.java:280)
    at org.apache.coyote.http11.InternalOutputBuffer.nextRequest(InternalOutputBuffer.java:144)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)




Exception in thread "http-bio-8084-exec-2" java.lang.NullPointerException
    at org.apache.tomcat.util.http.MimeHeaders.clear(MimeHeaders.java:151)
    at org.apache.coyote.Response.recycle(Response.java:536)
    at org.apache.coyote.http11.AbstractOutputBuffer.nextRequest(AbstractOutputBuffer.java:280)
    at org.apache.coyote.http11.InternalOutputBuffer.nextRequest(InternalOutputBuffer.java:144)
    at org.apache.coyote.http11.AbstractOutputBuffer.recycle(AbstractOutputBuffer.java:265)
    at org.apache.coyote.http11.InternalOutputBuffer.recycle(InternalOutputBuffer.java:131)
    at org.apache.coyote.http11.AbstractHttp11Processor.recycle(AbstractHttp11Processor.java:1709)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.release(Http11Protocol.java:140)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)







ClientAbortException:  java.net.SocketException: Socket closed
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:406)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
    at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:431)
    at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:419)
    at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91)
    at java.nio.channels.Channels$WritableByteChannelImpl.write(Channels.java:458)
    at com.sun.faces.application.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:343)
    at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
    at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:132)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.aircell.abs.acpu.maintenance.LoginFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
    Caused by: java.net.SocketException: Socket closed
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:116)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
    at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
    at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:119)
    at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
    at org.apache.coyote.Response.doWrite(Response.java:517)
    at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:401)
    ... 28 more




java.lang.IllegalStateException: Cannot change buffer size after data has been written
    at org.apache.catalina.connector.ResponseFacade.setBufferSize(ResponseFacade.java:254)
    at com.sun.faces.context.ExternalContextImpl.setResponseBufferSize(ExternalContextImpl.java:910)
    at com.sun.faces.application.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:328)
    at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153)
    at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:132)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.aircell.abs.acpu.maintenance.LoginFilter.doFilter(Unknown Source)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)





java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:363)
    at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
    at org.apache.coyote.http11.filters.ChunkedOutputFilter.end(ChunkedOutputFilter.java:167)
    at org.apache.coyote.http11.AbstractOutputBuffer.endRequest(AbstractOutputBuffer.java:310)
    at org.apache.coyote.http11.InternalOutputBuffer.endRequest(InternalOutputBuffer.java:157)
    at org.apache.coyote.http11.AbstractHttp11Processor.endRequest(AbstractHttp11Processor.java:1680)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1067)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

它还会导致应用程序以不正确的方式呈现。 (比如缺少css)和有时非常难看的渲染。

我的servletfilter代码:

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;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


  /**
 * Servlet Filter implementation class LoginFilter
 */
@WebFilter("/loginfilter")
public class LoginFilter implements Filter {

String value = null;
/**
 * Default constructor. 
 * 
 */
Object username;
HttpServletRequest req;
HttpServletResponse res;
HttpSession session;
BeanForLogin bean;
public LoginFilter() {
    // TODO Auto-generated constructor stub
    System.out.println("inside constructor");

}

/**
 * @see Filter#destroy()
 */
public void destroy() {
    // TODO Auto-generated method stub
}

/**
 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
    // TODO Auto-generated method stub
    // place your code here

    // pass the request along the filter chain
    //System.out.println("Inside login filter");
    req = (HttpServletRequest) request;
    res = (HttpServletResponse)response;        
    session = req.getSession(true);
    System.out.println("Session = " +session);
    username = session.getAttribute("username");
    res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        res.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        res.setDateHeader("Expires", 0); // Proxies.

    if((username == null) && (req.getServletPath().contains("/faces/")  (!req.getServletPath().contains("login_new.xhtml")))
    {

        System.out.println("inside if");
        System.out.println(req.getRequestURL() + "\n" + req.getServletPath());
        res.sendRedirect(req.getContextPath() + "/faces/login_new.xhtml");
        return;

    }
    else
    {
        System.out.println("inside else");
        chain.doFilter(req, res);
        return;
    }

}



/**
 * @see Filter#init(FilterConfig)
 */
public void init(FilterConfig fConfig) throws ServletException {
    // TODO Auto-generated method stub
}

}

问题是,有时页面按预期加载。但有时它是非常不合适的。确切地说,它是不一致的。

在这方面,任何帮助都表示赞赏!

提前致谢

0 个答案:

没有答案