Google App Engine Java servlet read JSP

I am trying to have our servlets on Google App Engine Java, read the result of a JSP using the method here: Pass data from Java Servlet to JSP?

Specifically our code is

ByteArrayOutputStream bufferStream = new ByteArrayOutputStream();
ResponseWrapper responseWrapper = new ResponseWrapper(response, bufferStream);

getServletContext().getRequestDispatcher(jspUrl).forward(request, responseWrapper);**

responseWrapper.flushBuffer();
return bufferStream.toString();

However since we have sessions turned off in appengine-web.xml

<sessions-enabled>false</sessions-enabled>

we get the error below. Is there anyway for a servlet to read a JSP without GAE trying to add a session?

    com.google.apphosting.vmruntime.VmApiProxyDelegate convertApiResponseRpcErrorToException: Security violation: invalid request id used!
W 2014-10-16 12:16:19.004
    org.gails.util.server.TLogUtil logException: IndexServlet.readJspPage() /index.html
  java.lang.RuntimeException: Session support is not enabled in appengine-web.xml.  To enable sessions, put <sessions-enabled>true</sessions-enabled> in that file.  Without it, getSession() is allowed, but manipulation of sessionattributes is not.  Session support is not enabled in appengine-web.xml.  To enable sessions, put <sessions-enabled>true</sessions-enabled> in that file.  Without it, getSession() is allowed, but manipulation of sessionattributes is not.
    java.lang.RuntimeException: Session support is not enabled in appengine-web.xml.  To enable sessions, put <sessions-enabled>true</sessions-enabled> in that file.  Without it, getSession() is allowed, but manipulation of sessionattributes is not.
    at com.google.apphosting.utils.jetty9.StubSessionManager$StubSession.throwException(StubSessionManager.java:86)
    at com.google.apphosting.utils.jetty9.StubSessionManager$StubSession.setAttribute(StubSessionManager.java:74)
    at org.eclipse.jetty.security.SecurityHandler$1.sessionCreated(SecurityHandler.java:335)
    at org.eclipse.jetty.server.session.AbstractSessionManager.addSession(AbstractSessionManager.java:686)
    at org.eclipse.jetty.server.session.AbstractSessionManager.newHttpSession(AbstractSessionManager.java:566)
    at org.eclipse.jetty.server.Request.getSession(Request.java:1406)
    at org.eclipse.jetty.server.Request.getSession(Request.java:1379)
    at org.apache.jasper.runtime.PageContextImpl.initialize(PageContextImpl.java:134)
    at org.apache.jasper.runtime.JspFactoryImpl.internalGetPageContext(JspFactoryImpl.java:109)
    at org.apache.jasper.runtime.JspFactoryImpl.getPageContext(JspFactoryImpl.java:60)
    at org.apache.jsp._005ftradeos_jsp._jspService(_005ftradeos_jsp.java:100)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:405)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:349)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.eclipse.jetty.servlet.JspPropertyGroupServlet.service(JspPropertyGroupServlet.java:130)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:769)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:595)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059)
    at com.google.apphosting.vmruntime.jetty9.VmRuntimeWebAppContext.doScope(VmRuntimeWebAppContext.java:366)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:191)
    at org.eclipse.jetty.server.Dispatcher.forward(Dispatcher.java:72)
    at org.gails.site.server.IndexServlet.readJspPage(IndexServlet.java:279)

Answer

If you check the stack trace, the RequestDispatcher forward is triggering code that requires the use of sessions. The error message clearly tells you that you need to have <sessions-enabled>true</sessions-enabled>. The origin of this may be the fact that you get the RequestDispatcher from a call to getServletContext(), which if you check the javadoc, requires a session.

There may be other ways to get a RequestDispatcher to forward to a JSP, that don’t create a session. Try to get the RequestDispatcher from the HttpServletRequest object with req.getRequestDispatcher().

Either enable sessions, or at the very least, if the above advice didn’t help and you still don’t want to use sessions, you could try to use RequestDispatcher.include(), which might not require sessions(?), although again you would have to test.

Leave a Reply

Your email address will not be published. Required fields are marked *