从单个 Web 应用程序中的 servlet 检索 JSP 内容(在 Tomcat 下)

发布于 2024-10-31 21:07:49 字数 1566 浏览 1 评论 0原文

首先,我想为问题描述不那么清楚而道歉。假设我们有一些用于电子邮件发送的工作机制,并且我们还有一些基于JSP处理引擎的电子邮件模板机制。比方说,这些模板可以像 一样直接访问http://localhost/app/templates/template1.jsp?k1=v1&k2=v2 并始终返回一个 HTML,该 HTML 可以插入到后端代码中特定用户的电子邮件中。简单地说,简写代码可能如下所示:

Map<String, String> parameters = new HashMap<String, String>() {{ ..puts.. }};
String message = readJsp("/templates/template1.jsp", parameters); // servlet-context?
sendEMail(recipient, subject, message)

目前我找到了两种实现 readJsp 方法的解决方案。第一种方法是从 servlet 向放置 servlet 和 JSP 模板的 Web 服务器发出 HTTP 请求。好吧,我不喜欢这个......

真正有效的第二更好的方法是从 servlet 上下文获取请求调度程序并调用它的 include() 使用响应包装器的方法(此处描述了此解决方法)。此方法工作完美,但如果我无法获取 HttpServletRequestHttpServletResponse 实例,我不知道如何使用此方法(让我们考虑我的 sendEMail() 方法可以从 servlet service 方法[在这里很容易获取 HttpServletRequest/Response 实例]和作为 Web 的一部分运行的线程调用应用[其中 HttpServletRequest/Response 实例从不已知,甚至无法传递给线程])。我可以建议我可以以某种方式自己创建 HttpServletRequest/HttpServletResponse 实例,但我不知道如何以及是否可以使用该 readJsp() 方法基于第二种方法。

因此,问题基本上是关于从不属于 servlet 但在同一 Web 应用程序中运行的线程获取 JSP 内容的第三种(甚至更好)方法。或者是否可以从头开始创建有效的 HttpServletRequest/HttpServletResponse 实例,以使第二种方法在 servlet service()< 中工作/代码>方法?

有什么想法吗?提前致谢。

First of all, I wanna apologize for not so clear description of the question. Let's consider we have some working mechanism for e-mail sending, and we also have some e-mail templates mechanism based on JSP processing engine. Let's say, these templates may be directly accessed like http://localhost/app/templates/template1.jsp?k1=v1&k2=v2 and always return an HTML that can be inserted into an e-mail for a certain user in the back-end code. Simply saying, the short-hand code might look like:

Map<String, String> parameters = new HashMap<String, String>() {{ ..puts.. }};
String message = readJsp("/templates/template1.jsp", parameters); // servlet-context?
sendEMail(recipient, subject, message)

Currently I found two solutions for implementing the readJsp method. The first approach is to make a HTTP request from the servlet to the web-server where both servlet and JSP-template are put at. Well, I'm not a fan of this...

The second and much better way that really works is getting a request dispatcher from the servlet context and invoking its include() method with the response wrapper (this work around is described here). This method works perfect, but I don't know how I can use this approach if I cannot get a HttpServletRequest and HttpServletResponse instances (let's consider my sendEMail() method can be invoked both from servlet service method [it's easy to get the HttpServletRequest/Response instances here] and a thread that runs as a part of the web application [where HttpServletRequest/Response instance are never known and even cannot be passed to the thread]). I can suggest that I can create the HttpServletRequest/HttpServletResponse instances myself in a certain way, but I don't know how and whether it will be ok for that readJsp() method based on the second approach.

So, the question is basically about, perhaps, the 3rd (and even better) way of getting the JSP content from a thread that does not belong to a servlet but runs in the same web application. Or is it possible to create valid HttpServletRequest/HttpServletResponse instances from scratch to make the second approach work out of servlet service() method?

Any ideas? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末的微笑 2024-11-07 21:07:49

您可以尝试使用 Spring 的 MockHttpServletRequest/Response 但据我所知,Tomcat 的 ServletDispatcher.include() 不适用于这些。

如果您无权访问请求/响应对,我建议不要使用 JSP。而是使用 VelocityFreemarker 用于模板化您的电子邮件。

You could try using Spring's MockHttpServletRequest/Response but from what I have heard, Tomcat's ServletDispatcher.include() does not work with those.

I would recommend not using JSP in the case where you don't have access to a request/response pair. Instead use Velocity or Freemarker to template your emails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文