简体   繁体   English

捕获JSP输出,保存到文件

[英]Capture JSP output, save to file

I'm tasked with trying creating a site which will create custom HTML templates. 我的任务是尝试创建一个将创建自定义HTML模板的网站。 The customizations are based-up customized upon user input. 定制基于用户输入进行定制。 I'm currently using tomcat / JSP for the rest of the front end. 我目前在其余的前端使用tomcat / JSP。 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). 我想做的是在JSP中创建HTML模板以输出条件HTML代码,但是我不想将此HTML输出显示给用户,而是希望将此代码保存到多个文件中(然后将其压缩并传递到用户以及图片,css文件,js文件)。 I need to know how to create a fake container that will execute a JSP file (process includes and evaluate variables). 我需要知道如何创建一个将执行JSP文件的虚假容器(过程包括并评估变量)。

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. 我读过有关使用服务器过滤器拦截JSP代码输出的信息,但我不确定这是否可行,因为a)我需要创建多个HTML文件,b)我需要向用户显示不同的内容(即,这是您的zip文件) (下载),而不是正在处理的JSP的输出。

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. 创建一个Servlet来接受用户的输入,从Servlet使用java.net.HttpURLConnection将输入传递到JSP页面并获得结果。

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 然后在您的servlet中,您可以压缩所有拉出的文件并将其返回给用户

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. 您可以通过打开URLConnection,然后将输出读入临时文件或缓冲区来使Servlet调用JSP。 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: 为什么不创建一个servlet并使用以下命令调用您的jsp文件:


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

Have the jsp show the front end with the form / user inputted values. 让jsp用表格/用户输入的值显示前端。 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. 此时,jsp将把上下文返回到Servlet,您可以在其中解析值,确定要创建的文件并返回给用户。 Once you have determined what you want to display to the user, throw it into another jsp and pass the context to that jsp. 确定要显示给用户的内容后,将其放入另一个jsp中,并将上下文传递给该jsp。

You can do this in 2 ways. 您可以通过2种方式执行此操作。 You can create a different jsp for the different filetypes you wish to return and simply call the proper one. 您可以为要返回的不同文件类型创建一个不同的jsp,只需调用适当的文件即可。 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. 或者,您可以将所需的任何结构/值嵌入到jsp中,然后将它们放入jsp中,从而将它们传递给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. 您可以使用此事实来创建单个jsp,该jsp基于传入的参数(嵌入在请求中)将显示正确的信息。

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? 为什么不使用wget? You could load the pages into a real container and then retrieve the generated pages from the container using wget. 您可以将页面加载到实际容器中,然后使用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. 在wget中使用此选项将获取文件中的URL列表,并为您下载它们,就像文本将显示在浏览器的view-src中一样。

Also wget is available for Windows and Linux. wget也可用于Windows和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. 例如查看速度

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

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