我已经开发了一个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“...或者,有没有其他策略可以在不使用restlet的情况下工作?也许对我的web.xml文件做了一些修改?
编辑(2009-08-14):
我下面发布的答案在App-Engine和Restlet上不起作用。但是,如果我不包含Restlet,或者允许Restlet有一个url-pattern "/*“,那么它确实可以工作。
理想的做法是拥有Router的一个子类,使我能够做到这一点:
router.attach("/contact", "/contact.jsp");谢谢!
编辑(2009-08-17):
我很惊讶,自从我发布了一个赏金后,我还没有收到任何回复。如果我的问题不清楚,请有人评论并让我知道?
编辑(2009-08-17):
有趣的观察。当使用下面的“富卖家”描述的方法时,它在部署在Google App-Engine上而不是本地时可以工作。此外,如果我在Google App-Engine上调用http://mydomain.com/contact.jsp,它会绕过Restlet,直接转到JSP。但是,在本地,Restlet接管了工作。也就是说,http://localhost:8080/contact.jsp不会转到JSP,而是转到Restlet。部署的应用程序引擎应用程序对URL的响应是否与本地应用程序不同?
发布于 2010-09-17 08:36:42
“我想通过Restlet在URL请求时返回一个JSP文件”--我的理解是JSP被转换为servlet。由于Servlet与Restlet是正交的,因此不确定如何通过Restlet返回JSP文件。
假设除了Restlet之外,您还需要一种使用JSP的方法,最好的实现方法是将您的Restlet映射到根路径,如/rest,而不是/*,并照常使用.jsp。
发布于 2009-08-14 07:57:22
Restlet目前不直接支持JSP。它们很难在servlet容器之外处理。
有一个关于这个问题的discussion on Nabble,您可能会发现它很有用,目前看起来您需要将重定向返回到在web.xml中正常映射的JSP,或者破解它以处理JSP并返回作为表示的流。
线程中日期为"Apr 23,2009;03:02 do“的响应描述了如何进行黑客攻击:
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-14 03:55:23
看起来像是一个简单的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设置为"/*“,它就不能工作了
https://stackoverflow.com/questions/1275904
复制相似问题