java.lang.IllegalStateException: Cannot create a s

2019-01-15 23:08发布

问题:

I've got this really strange request for an invalid URL on:

Invalid URL: /cgi-bin/1st.cgi

This is causing the following error:

 SEVERE: Exception initializing page context
java.lang.IllegalStateException: Cannot create a session after the response has been committed
        at org.apache.catalina.connector.Request.doGetSession(Request.java:2381)
        at org.apache.catalina.connector.Request.getSession(Request.java:2098)
        at org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:833)
        at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:216)
        at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:544)
...

How can I solve this issue?

回答1:

What's happening is the response stream has been closed. Either you already sent a redirect, or committed some other type of response. I recommend getting your session object before you perform any response, and save the response until there is nothing left to do with the HttpServletRequest or HttpServletResponse objects.

A good practice is to return from your Servlet right away after performing a redirect, that way you can avoid some of these problems.



回答2:

I had this problem because of multiple tag <h:outputScript target="head">