Configure Restlet to Return JSPs on Google App Engine?

血红的双手。 提交于 2019-12-04 04:58:36

"I would like to return a JSP file on a URL request through Restlet" - My understanding is JSP's are converted to servlets. Since Servlets are orthogonol to Restlets not sure how you can return JSP file through Restlet.

Assuming you are asking for a way to use JSP in addition to Restlet, This is best achieved by mapping your restlets to a rootpath such as /rest instead of /* and using the .jsp as usual.

Restlet doesn't currently support JSPs directly. They're difficult to handle outside of the servlet container.

There's a discussion on Nabble about this issue that you may find useful, at the moment it looks like you need to either return a redirect to the JSP mapped as normal in the web.xml, or hack it to process the JSP and return the stream as the representation.

The response dated "Apr 23, 2009; 03:02pm" in the thread describes how you could do the hack:

if (request instanceof HttpRequest &&
    ((HttpRequest) request).getHttpCall() instanceof ServletCall) {

    ServletCall httpCall = (ServletCall) ((HttpRequest) request).getHttpCall();

    // fetch the HTTP dispatcher
    RequestDispatcher dispatcher = httpCall.getRequest().getRequestDispatcher("representation.jsp");

    HttpServletRequest proxyReq = new HttpServletRequestWrapper(httpCall.getRequest());

    // Overload the http response stream to grab the JSP output into a dedicated proxy buffer
    // The BufferedServletResponseWrapper is a custom response wrapper that 'hijacks' the
    // output of the JSP engine and stores it on the side instead of forwarding it to the original
    // HTTP response.
    // This is needed to avoid having the JSP engine mess with the actual HTTP stream of the
    // current request, which must stay under the control of the restlet engine.
    BufferedServletResponseWrapper proxyResp = new BufferedServletResponseWrapper(httpCall.getResponse());

    // Add any objects to be encoded in the http request scope
    proxyReq.setAttribute("myobjects", someObjects);

    // Actual JSP encoding
    dispatcher.include(proxyReq, proxyResp);

    // Return the content of the proxy buffer
    Representation rep = new InputRepresentation(proxyResp.toInputStream(),someMediaType); 

The source for the BufferedServletResponseWrapper is posted a couple of entries later.

Looks like a simple web.xml configuration.

<servlet>
     <servlet-name>contactServlet</servlet-name>
     <jsp-file>/contact.jsp</jsp-file>
</servlet>

<servlet-mapping>
     <servlet-name>contactServlet</servlet-name>
     <url-pattern>/contact</url-pattern>
</servlet-mapping>

This works without Restlet in App-Engine. But once I include Restlet, it doesn't work if I set my Reslet url-pattern to "/*"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!