我正在尝试在Google App Engine Java上使用我们的servlet,请使用此处的方法读取JSP的结果:
Pass data from Java Servlet to JSP?

具体来说,我们的代码是

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

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

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


但是,由于我们在appengine-web.xml中关闭了会话

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


我们得到下面的错误。无论如何,servlet无需GAE尝试添加会话即可读取JSP?

    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)

最佳答案

如果检查堆栈跟踪,则RequestDispatcher转发将触发要求使用会话的代码。该错误消息清楚地告诉您您需要<sessions-enabled>true</sessions-enabled>。这可能是由于您从对getServletContext()的调用中获取RequestDispatcher的事实,如果您检查javadoc,则需要进行会话。

可能会有其他方法使RequestDispatcher转发到JSP,而不创建会话。尝试使用RequestDispatcherHttpServletRequest对象获取req.getRequestDispatcher()

启用会话,或者至少,如果以上建议没有帮助并且您仍然不想使用会话,则可以尝试使用RequestDispatcher.include(),它可能不需要session(?),尽管您再次将不得不测试。

09-11 08:53