以编程方式调用 JSP 解析器

发布于 2024-10-16 06:27:40 字数 443 浏览 2 评论 0原文

我想以编程方式调用 JSP 解析器。这意味着我希望能够在 Java 中“运行”jsp 页面并获取其输出,而不将其发送回客户端(我实际上想将输出保存到文件中)。我不想将请求“转发”到 JSP 页面。我希望能够在连续的几个 JSP 页面上执行此操作。

这样做的最佳方法是什么?

我发现了这个 问题,但 BalusC 并没有真正直接回答这个问题。

如果您想知道,我需要这样做是因为我想“预编译”JSP,以便在 Java servlet 容器之外的其他平台上使用。

编辑

我需要的不是 .class 文件,而是 HTML 输出。事实上,一旦生成,它将是静态的,但我有一些自定义 jsp 标签,我想利用 JSP 解析器来扩展它们。

I would like to invoke the JSP parser programmatically. That means that I want to be able, in Java, to 'run' a jsp page and get its output, without sending it back to the client (I actually want to save the output to a file). I don't want to 'forward' the request to the JSP page. I want to be able to do that on several JSP pages in a row.

What is the best way of doing this?

I have found this question, but BalusC doesn't really answer the question directly.

In case you are wondering, I need to do this is because I want to 'precompile' the JSPs for using on other platforms than a Java servlet container.

EDIT

What I need is not the .class file, but the HTML output. Indeed, that will be static once generated but I have some custom jsp tags and I want to leverage the JSP parser to expand them.

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

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

发布评论

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

评论(4

韶华倾负 2024-10-23 06:27:40

我不确定我是否理解这一切的意义。

JSP 被解析并预编译为 .class 文件。那时它们是 Java servlet。您需要一个 Servlet 引擎来执行它们。

如果您的目的是将生成的 HTTP 响应捕获为“预编译”响应,则表明没有动态内容,并且每次发送该特定请求时响应都是相同的。如果是这样的话,您得到的就是静态 HTML。

如果我是对的,这似乎是产生这样的事情的一个糟糕方法。

如果您希望将 JSP 预编译为 .class 文件,那么问题是不同的 Java EE 应用服务器使用不同的 JSP 预编译引擎。您无法使用 Tomcat 预编译 JSP 并在 WebLogic 上使用它们。

I'm not sure that I understand the point of all this.

JSPs are parsed and precompiled to .class files. They're Java servlets at that point. You need a servlet engine to execute them.

If your intent is to capture the generated HTTP response as the "precompiled" response, it would suggest that there's no dynamic content and the response is the same every time you send that particular request. If that's the case, what you've got is static HTML.

If I'm correct, this would seem to be a poor way to generate such a thing.

If your wish is to precompile JSPs to .class files, the problem is that different Java EE app servers use different JSP precompilation engines. You can't precompile JSPs using Tomcat and use them on WebLogic.

本宫微胖 2024-10-23 06:27:40

从 jsp 页面获取 html 输出的最佳方法是将其实际部署到真实的 Web 服务器,然后调用该页面并保存呈现的输出。

如果您想自动化其中的某些部分,您可能需要考虑使用通过真实界面进行测试的测试工具,例如 Selenium 或模拟浏览器,例如 HttpUnit

但这不仅仅只是调用 JSP 编译器。

The best way to get the html output from a jsp page is to actually deploy it to a real webserver and then call the page and save the rendered output.

If you want to automate some part of this, you might want to look into using a testing tool that exercises through the real interface, such as Selenium or that emulates the browser, such as HttpUnit.

But this is doing much more than just invoking the JSP compiler.

眼眸里的那抹悲凉 2024-10-23 06:27:40

Maybe it would be more practical to use template engines like http://freemarker.sourceforge.net/ or http://velocity.apache.org/

Freemarker even seems to support JSP Taglibs: http://freemarker.sourceforge.net/features.html

亽野灬性zι浪 2024-10-23 06:27:40

你的JSP是动态生成的。如果是这样,那么您就陷入了潜在的不利情况,其中您的 JSP 将被一次又一次地编译,从而导致性能问题。

但是,如果您有一个大型 JSP,其中包含准备显示所需的所有规则,则可以使用 HttpClient 调用您自己的 JSP,这将返回 HTML。这将确保您不依赖于应用程序服务器。如果您使用 JSP 解析器,您将依赖于供应商。

但是,如果您的 JSP 是动态构建的,那么您应该考虑可以在 Java 端生成 HTML 的选项。但如果它涉及基于规则的 HTML 创建,您最好使用 Java 创建它。您可以为此使用 Apache Jakarta ECS 库。

是的,JSP 并不适合这个目的。

Is your JSP dynamically generated. If so then you are stepping into a potential disadvantage situation wherein your JSP will be compiled again and again, leading to performance issues.

However if you could have a single large JSP with all the rules that you need to prepare your display, you could use HttpClient to make a call to your own JSP and that would return the HTML. This would ensure that you are not app-server dependent. If you use JSP Parser you are going to be vendor dependent.

But if your JSP is being dynamically constructed then you should look at options wherein your HTML can be generated on Java side. But if it involved rule based HTML creation, you are better off creating it in Java. You can use Apache Jakarta ECS library for this.

And yes JSPs are not meant for this purpose.

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