java 将 JSP 渲染为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1464353/
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
rendering JSP to a string
提问by Overdose
What is the way to render jsp file to a string? Is it possible without using frameworks like spring/struts/etc?..
将jsp文件渲染为字符串的方法是什么?是否可以不使用 spring/struts/etc 之类的框架?...
回答by Kalpak
If you want to stream any webpage may it be a JSP or any other web page, you can use the method below.
如果您想流式传输任何网页,可能是 JSP 或任何其他网页,您可以使用以下方法。
import java.io.*;
import java.net.*;
public class c {
public String getHTML(String urlToRead) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String args[])
{
c c = new c();
System.out.println(c.getHTML(args[0]));
}
}
回答by Alexey Sviridov
You may compile JSP Pages with any JSP Rendering engine, for example Apache Jasper, included in Apache Tomcat.
您可以使用包含在 Apache Tomcat 中的任何 JSP 渲染引擎(例如 Apache Jasper)来编译 JSP 页面。
回答by Aaron Digulla
Use MockRunner. This allows you to run JSPs in test cases, for example.
使用MockRunner。例如,这允许您在测试用例中运行 JSP。

