简体   繁体   中英

invoke jsp from java code and get output

I have a JSP page that show data in some formatted way. the browser can call spring showInfo.do and it is forward to that 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.

Now I want to save this JSP output manually from my java server side code (not in a servlet context), something like this:

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. So if the server is offline a user can double click on the saved html file and see in his browser the last info. Any idea how this can be done?

Oups. The servlet specification requires the servlet container to be able to execute a JSP file. This is commonly done by converting the JSP to plain Java and the generating a servlet class file.

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

Alternatively, you could try to use a headless browser to capture the output of the application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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