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

发布于 2024-08-17 06:22:33 字数 208 浏览 2 评论 0原文

在jsp/java中,如何调用输出xml文件作为结果的页面并将其结果(xml类型)保存到服务器上的xml文件中。这两个文件(生成 xml 的文件和我们要保存/覆盖的文件)都位于同一服务器上。

基本上我想通过调用输出 xml 类型结果的 generate.jsp 来更新我的 test.xml

谢谢。

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. Both files (the file that produces the xml and the file that we want to save/overwrite) live on the same server.

Basically I want to update my test.xml every now and then by calling generate.jsp that outputs a xml type result.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

度的依靠╰つ 2024-08-24 06:22:33

如果请求是幂等的,则只需使用 java.net.URL 获取 JSP 输出的 InputStream。例如,

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

如果请求不是幂等的,那么您需要替换 PrintWriter 具有自定义实现的响应,它将输出复制到某个缓冲区/构建器中。我之前在这里发布了一个代码示例: 捕获生成的动态内容在服务器端

一旦获得输出,只需以通常的 java.io 方式将其写入磁盘,假设 JSP 已经是 XHTML 格式。

If the request is idempotent, then just use java.net.URL to get an InputStream of the JSP output. E.g.

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. 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.

自此以后,行同陌路 2024-08-24 06:22:33
  1. 注册一个过滤器,将包装器添加到您的响应中。也就是说,它向链返回一个新的 HttpServletResponse 对象,扩展原始 HttpServletResponse,并返回您的自定义 OutputStreamPrintWriter< /code> 而不是原来的。
  2. 您的 OutputStreamPrintWriter 调用原始的 OutputStreamPrintWriter,但也写入您的文件(使用新的文件输出流)
  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.
  2. Your OutputStream and PrintWriter calls the original OutputStream and PrintWriter, but also write to a your file (using a new FileOutputStream)
腹黑女流氓 2024-08-24 06:22:33

为什么不使用像 FreeMarker 这样真正的模板引擎呢?那会更容易。

Why don't you use a real template engine like FreeMarker? That would be easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文