代码之家  ›  专栏  ›  技术社区  ›  JP Richardson

在Google应用程序引擎上配置Restlet以返回JSP?

  •  8
  • JP Richardson  · 技术社区  · 15 年前

    假设我在mydomain.com上有一个文件“contact.jsp”,我希望人们能够在 http://mydomain.com/contact

    因此,在Restlet中,我将:

    router.attach("/contact", MyResource.class);
    

    但是如何返回“contact.jsp”页面呢?我知道重定向会起作用,但我不希望用户在中看到“.jsp” http://mydomain.com/contact.jsp “…或者有没有另一种策略可以在不使用restlet的情况下工作?可能是对我的web.xml文件进行了一些修改?

    编辑(2009-08-14):

    我在下面发布的答案在appengine和Restlet上不起作用。但是,如果我不包括Restlet,或者不允许Restlet的url模式为“/*”的话,它确实可以工作

    理想的情况是有一个路由器的子类,允许我这样做:

    router.attach("/contact", "/contact.jsp");
    

    谢谢

    编辑(2009-08-17):

    我很惊讶,自从我发了一份赏金后,一直没有收到任何回复。如果我的问题/问题不清楚,会有人评论并让我知道吗?

    编辑(2009-08-17):

    有趣的观察。当使用下面“富卖家”描述的方法时,它在部署到Google App Engine而不是本地时工作。另外,如果我打电话 在googleappengine上,它绕过Restlet直接进入JSP。但是,在本地,Restlet接管了一切。就是, http://localhost:8080/contact.jsp 不转到JSP,而是转到Restlet。部署的应用程序引擎应用程序对URL的响应是否与本地应用程序的响应不同?

    3 回复  |  直到 15 年前
        1
  •  2
  •   Rich Seller    15 年前

    Restlet目前不直接支持JSP。在servlet容器之外很难处理它们。

    有一个 discussion on Nabble 关于这个您可能会发现很有用的问题,现在看起来您需要返回一个重定向到web.xml中正常映射的JSP,或者对其进行破解以处理JSP并返回流作为表示。

    帖子中日期为“2009年4月23日;03:02pm”的回复描述了如何进行黑客攻击:

    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); 
    

    BufferedServletResponseWrapper的源在几个条目之后发布。

        2
  •  1
  •   JP Richardson    15 年前

    看起来像一个简单的web.xml配置。

    <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>
    

    这在没有应用程序引擎中的Restlet的情况下工作。但一旦我包含了Restlet,如果我将Reslet url模式设置为“/”,它就不起作用了

        3
  •  1
  •   Gmu    14 年前

    “我想通过Restlet返回URL请求上的JSP文件”——我的理解是JSP文件被转换为servlet。由于servlet与Restlet是正交的,所以不确定如何通过Restlet返回JSP文件。

    假设您正在寻求一种除了Restlet之外使用JSP的方法,那么最好将Restlet映射到根路径,例如/rest而不是/*并像往常一样使用.JSP。