我开发了一个 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 模式,它确实有效
理想的情况是拥有允许我执行此操作的 Router 的子类:
router.attach("/contact", "/contact.jsp");
谢谢!
编辑(2009-08-17):
我很惊讶自从我发布赏金后我没有收到任何回复。如果我的问题/问题不清楚,有人会发表评论并让我知道吗?
编辑(2009-08-17):
有趣的观察。当使用下面“Rich Seller”描述的方法时,它在部署在 Google App-Engine 上而不是本地时有效。另外,如果我调用 http://mydomain.com/contact.jsp在 Google App-Engine 上,它绕过 ReSTLet 并直接进入 JSP。但是,在本地,ReSTLet 接管了工作。即 http://localhost:8080/contact.jsp不上JSP,上ReSTLet。已部署的应用引擎应用程序对 URL 的响应是否与其本地应用程序不同?
最佳答案
ReSTLet 目前不直接支持 JSP。它们很难在 servlet 容器之外处理。
有一个 discussion on 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 的源代码稍后发布。
关于java - 配置 ReSTLet 以在 Google App Engine 上返回 JSP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1275904/