简体   繁体   English

从Java代码调用jsp并获取输出

[英]invoke jsp from java code and get output

I have a JSP page that show data in some formatted way. 我有一个JSP页面,它以某种格式化的方式显示数据。 the browser can call spring showInfo.do and it is forward to that JSP. 浏览器可以调用spring showInfo.do并将其转发到该JSP。 ie

public showInfo(HttpServletRequest request, HttpServletResponse response) {
RequestDispatcher rd = getServletContext().getRequestDispatcher("info.jsp");
dispatcher.forward(request,response);
}

The output of the JSP is html. JSP的输出是html。

Now I want to save this JSP output manually from my java server side code (not in a servlet context), something like this: 现在,我想从Java服务器端代码中手动保存此JSP输出(不在servlet上下文中),如下所示:

void saveInfo() {
params.setParameter("info1", "data");
String responseStr = Invoke("info.jsp", params);
//save responseStr to disk
}

I want to be able to save the html page on disk from a service and make it look the same as a user can see it from a browser. 我希望能够从服务将html页面保存在磁盘上,并使它与用户从浏览器中看到的外观相同。 So if the server is offline a user can double click on the saved html file and see in his browser the last info. 因此,如果服务器处于脱机状态,则用户可以双击保存的html文件并在其浏览器中查看最后的信息。 Any idea how this can be done? 知道如何做到这一点吗?

Oups. 哎呀 The servlet specification requires the servlet container to be able to execute a JSP file. Servlet规范要求Servlet容器能够执行JSP文件。 This is commonly done by converting the JSP to plain Java and the generating a servlet class file. 通常,这是通过将JSP转换为纯Java并生成servlet类文件来完成的。

If you are outside of a servlet container you must: * either fully implement a JSP execution environment, for example by using sources from a servlet container like Tomcat * or rely on a servlet container to convert the JSP file to a .java or .class servlet and then use the Servlet interface methods on it 如果您不在servlet容器之外,则必须:*完全实现JSP执行环境,例如通过使用servlet容器(例如Tomcat)之类的源*或依靠servlet容器将JSP文件转换为.java或.class servlet,然后在其上使用Servlet接口方法

Alternatively, you could try to use a headless browser to capture the output of the application. 另外,您可以尝试使用无头浏览器捕获应用程序的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM