简体   繁体   中英

Capture JSP output, save to file

I'm tasked with trying creating a site which will create custom HTML templates. The customizations are based-up customized upon user input. I'm currently using tomcat / JSP for the rest of the front end. What I want to do is create the HTML templates in JSP to output conditional HTML code, but instead of display this HTML output to the user, I'd like to save this code to several files (which will then be zipped up and delivered to the user, along with images, css files, js files). I need to know how to create a fake container that will execute a JSP file (process includes and evaluate variables).

I read about using server filters to intercept the output of JSP code, but I am not sure this will work because a) I need to create several HTML files and b) I need to display different content to the user ( ie here's ur zip file, download it) not the output of the JSP being processed.

Thanks!

Here is an idea. Create a servlet to accept the input from the user, from the servlet use java.net.HttpURLConnection to pass the input to the JSP page and to get the result.

URL urlPage = new URL(url);
HttpURLConnection conn = (HttpURLConnection)urlPage.openConnection();
conn.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

//then loop through lines of webpage with br.readLine();

Then in your servlet you can zip all the files you pull and return it to the user

You could have a servlet that calls to the JSP by opening a URLConnection and then reading in the output into a temp file or a buffer. It would then zip it and write the zip out.

I'm really not sure I understand your question, but I will take a stab at it. Why not create a servlet and have it call your jsp file using the following:


    RequestDispatcher d = getServletContext().getRequestDispatcher(jspName);
    d.forward(request, response);

Have the jsp show the front end with the form / user inputted values. When the form is completed, submit the form. At which point the jsp will return context to the servlet where you can parse the values, determine which files to create and return to the user. Once you have determined what you want to display to the user, throw it into another jsp and pass the context to that jsp.

You can do this in 2 ways. You can create a different jsp for the different filetypes you wish to return and simply call the proper one. Or You can pass whatever structures / values you want into your jsp by embedding them in the request and then getting them within your jsp. You can use this fact to create a single jsp that based on passed in parameters(embedded within the request) will display the proper information.

If I completely missed the point of your question, feel free to delete this response.

Edit: Either way you will have to embed your values within the request.

Why not use wget? You could load the pages into a real container and then retrieve the generated pages from the container using wget.

--input-file=FILE read URLs from file.

Using this option in wget will take a list of URLs from a file and download them for you just as the text would be displayed in the view-src of the browser.

Also wget is available for Windows and Linux.

You can also try using some of the many light template engines which were designed specifically for such job. That's what I did when I had similar task and never looked back. Check out Velocity for example.

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