简体   繁体   English

如何将页面调用的内容保存到jsp / java中的文件中?

[英]How to save content of a page call into a file in jsp/java?

In jsp/java how can you call a page that outputs a xml file as a result and save its result (xml type) into a xml file on server. 在jsp / java中,如何调用一个页面,该页面输出一个xml文件作为结果并将其结果(xml类型)保存到服务器上的xml文件中。 Both files (the file that produces the xml and the file that we want to save/overwrite) live on the same server. 这两个文件(生成xml的文件和我们要保存/覆盖的文件)都位于同一服务器上。

Basically I want to update my test.xml every now and then by calling generate.jsp that outputs a xml type result. 基本上,我想立即更新我的test.xml ,然后调用输出一个xml类型结果的generate.jsp

Thank you. 谢谢。

  1. Register a filter that adds a wrapper to your response. 注册一个为您的响应添加包装的过滤器。 That is, it returns to the chain a new HttpServletResponse objects, extending the original HttpServletResponse , and returning your custom OutputStream and PrintWriter instead of the original ones. 也就是说,它向链中返回一个新的HttpServletResponse对象,扩展了原始HttpServletResponse ,并返回自定义的OutputStreamPrintWriter而不是原始的。
  2. Your OutputStream and PrintWriter calls the original OutputStream and PrintWriter , but also write to a your file (using a new FileOutputStream ) 您的OutputStreamPrintWriter调用原始的OutputStreamPrintWriter ,但也写入文件(使用新的FileOutputStream

If the request is idempotent, then just use java.net.URL to get an InputStream of the JSP output. 如果请求是幂等的,则只需使用java.net.URL即可获取JSP输出的InputStream。 Eg 例如

InputStream input = new URL("http://example.com/context/page.jsp").openStream();

If the request is not idempotent, then you need to replace the PrintWriter of the response with a custom implementation which copies the output into some buffer/builder. 如果请求不是幂等的,则需要用自定义实现替换响应的PrintWriter ,该实现将输出复制到某个缓冲区/构建器中。 I've posted a code example here before: Capture generated dynamic content at server side 我之前在这里发布了代码示例: 在服务器端捕获生成的动态内容

Once having the output, just write it to disk the usual java.io way, assuming that JSP's are already in XHTML format. 有了输出后,假设JSP已经是XHTML格式,只需以通常的java.io方式将其写入磁盘。

Why don't you use a real template engine like FreeMarker? 为什么不使用像FreeMarker这样的真实模板引擎? That would be easier. 那会容易些。

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

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