8

我开发了一个 Restlet 应用程序。我想通过 Restlet 返回一个关于 URL 请求的 JSP 文件。如何在不使用重定向的情况下实现这一目标?

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

因此,在 Restlet 中,我会:

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

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

编辑(2009-08-14):

我在下面发布的答案不适用于 App-Engine 和 Restlet。但是,如果我不包含 Restlet,或者允许 Restlet 具有“/*”的 url 模式,它确实有效

理想的做法是拥有一个允许我这样做的路由器子类:

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

谢谢!

编辑(2009-08-17):

我很惊讶自从我发布赏金以来我没有任何回应。如果我的问题/问题不清楚,有人会发表评论并告诉我吗?

编辑(2009-08-17):

有趣的观察。当使用下面“Rich Seller”描述的方法时,它在部署在 Google App-Engine 上而不是在本地时有效。此外,如果我在 Google App-Engine 上调用http://mydomain.com/contact.jsp,它会绕过 Restlet 并直接进入 JSP。但是,在本地,Restlet 接管了。即http://localhost:8080/contact.jsp不去JSP,去Restlet。已部署的应用程序引擎应用程序对 URL 的响应是否与其本地对应项不同?

4

3 回答 3

2

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

Nabble 上有一个关于这个问题的讨论,您可能会觉得这很有用,目前看起来您需要返回重定向到在 web.xml 中正常映射的 JSP,或者破解它以处理 JSP 并返回流作为代表。

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

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 的源代码稍后会发布几个条目。

于 2009-08-14T07:57:22.193 回答
1

看起来像一个简单的 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>

这在 App-Engine 中无需 Restlet 即可工作。但是一旦我包含了 Restlet,如果我将我的 Reslet url-pattern 设置为“/*”,它就不起作用

于 2009-08-14T03:55:23.460 回答
1

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

假设您正在寻求一种除 Restlet 之外还使用 JSP 的方法,这最好通过将您的 restlet 映射到根路径(例如 /rest 而不是 /*)并照常使用 .jsp 来实现。

于 2010-09-17T00:36:42.460 回答