直接从 Java 执行 JSP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1075827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute JSP directly from Java
提问by
I need to execute a JSP. But I need to directly from Java, without using Tomcat or any other servlet container. Compiling JSPs would be a good thing too, but not necessary. I think maybe org.apache.jasper package is good for doing this, but I can't find any good example or tutorial online.
我需要执行一个JSP。但我需要直接从 Java,而不使用 Tomcat 或任何其他 servlet 容器。编译 JSP 也是一件好事,但不是必需的。我认为 org.apache.jasper 包可能适合这样做,但我在网上找不到任何好的示例或教程。
I need something as:
我需要一些东西:
Class compiledJSP = compileJSP(new File("helloWorld.jsp"));
String html = executeJSP(compiledJSP, httpServletRequest, httpServletResponse, ...);
html --> "Hello World, John!"
Thanks!
谢谢!
回答by ChssPly76
If you need to capture JSP's output as string it's reasonably straightforward (although far from ideal from the design point of view) from withinServlet Container:
1. Extend javax.servlet.HttpServletResponseWrapper and override getWriter() / getOutputStream() methods to provide your own buffered versions (e.g. StringWriter)
2. Invoke "include()" method of RequestDisparcher, wrapping original response in your own.
3. Grab buffer's content.
如果您需要将 JSP 的输出捕获为字符串,那么在Servlet 容器中相当简单(尽管从设计的角度来看远非理想):
1. 扩展 javax.servlet.HttpServletResponseWrapper 并覆盖 getWriter() / getOutputStream() 方法以提供您的自己的缓冲版本(例如StringWriter)
2. 调用RequestDisparcher 的“include()”方法,将原始响应包装在您自己的。
3. 抓取缓冲区的内容。
Now if you need to do the same thing outsideServlet Container, you really need to ask yourself "why?". Perhaps you should be using a template engine (FreeMarker / Velocity / StringTemplate / etc...) instead of JSPs? If your JSPs are well-formed XML files and are not using any java code inserts it may be reasonably trivial to convert them to FreeMarker templates (FreeMarker supports custom JSP tag libraries) which would greatlysimplify your task.
现在如果你需要在Servlet Container之外做同样的事情,你真的需要问自己“为什么?”。也许您应该使用模板引擎(FreeMarker / Velocity / StringTemplate / 等...)而不是 JSP?如果您的 JSP 是格式良好的 XML 文件并且没有使用任何 Java 代码插入,那么将它们转换为 FreeMarker 模板(FreeMarker 支持自定义 JSP 标记库)可能相当简单,这将大大简化您的任务。
Nevertheless, if it's an absolute hard requirement your most straightforward options are:
1. Run an external Servlet Container and let it handle JSP interpretation. Your program would submit HTTP requests to it and capture the output.
2. Same as above, but you can run embedded Servlet Container (e.g. Jetty).
然而,如果这是一个绝对的硬性要求,您最直接的选择是:
1. 运行一个外部 Servlet 容器并让它处理 JSP 解释。您的程序将向它提交 HTTP 请求并捕获输出。
2. 同上,但可以运行嵌入式 Servlet Container(例如 Jetty)。
If your JSPs are available at build-time you can precompile them via Jasper as suggested in other answers.
如果您的 JSP 在构建时可用,您可以按照其他答案中的建议通过 Jasper 预编译它们。
I would stronglyadvice against trying to implement your own servlet container - you'll be walking into a world of hurt.
我强烈建议不要尝试实现自己的 servlet 容器——你会走进一个受伤的世界。
回答by Brian
You will need a container. A JSP is an abstraction on Servlet. Servlets have a dependency on a life cycle provided by a container.You need a container to provide the life cycle.
您将需要一个容器。JSP 是对 Servlet 的抽象。Servlet 依赖于容器提供的生命周期。您需要一个容器来提供生命周期。
回答by Pablojim
This is possible without a servlet container. There are two steps to it.
这在没有 servlet 容器的情况下是可能的。有两个步骤。
The first is to generate the source code. If you look at the source code of the jspc ant task it is possible to do it directly in code. Or you could just invoke the ant task.
首先是生成源代码。如果您查看 jspc ant 任务的源代码,则可以直接在代码中进行。或者您可以只调用 ant 任务。
The code that is generated is just a Servlet and it is possible to invoke the methods on a servlet outside of a container:
生成的代码只是一个 Servlet,可以在容器外调用 servlet 上的方法:
Just instantiate it and then call doGet(request, response). I'm not sure exactly what you need this for but your life will be made easier using spring mock objects for the http request and response.
只需实例化它,然后调用 doGet(request, response)。我不确定您到底需要什么,但是使用 spring 模拟对象来处理 http 请求和响应,您的生活会变得更轻松。
This will populate the Response object. you can then get the output with:
这将填充 Response 对象。然后你可以得到输出:
res.getContentAsString();
See an example here:
在此处查看示例:
http://ytreyvus.blogspot.com/2007/03/private-void-cloneplaneffectrelationshi.html
http://ytreyvus.blogspot.com/2007/03/private-void-cloneplaneffectrelationshi.html
回答by Aaron Digulla
Try MockRunnerto execute it. You'll need to compile the JSP first, though. I'm using Maven 2 for this (and the JSP Compiler plugin)
尝试MockRunner来执行它。不过,您需要先编译 JSP。我为此使用了 Maven 2(和JSP 编译器插件)

