如何将页面调用的内容保存到jsp/java中的文件中?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果请求是幂等的,则只需使用
java.net.URL
获取 JSP 输出的 InputStream。例如,如果请求不是幂等的,那么您需要替换
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.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 sideOnce having the output, just write it to disk the usual
java.io
way, assuming that JSP's are already in XHTML format.HttpServletResponse
对象,扩展原始HttpServletResponse
,并返回您的自定义OutputStream
和PrintWriter< /code> 而不是原来的。
OutputStream
和PrintWriter
调用原始的OutputStream
和PrintWriter
,但也写入您的文件(使用新的文件输出流)HttpServletResponse
objects, extending the originalHttpServletResponse
, and returning your customOutputStream
andPrintWriter
instead of the original ones.OutputStream
andPrintWriter
calls the originalOutputStream
andPrintWriter
, but also write to a your file (using a newFileOutputStream
)为什么不使用像 FreeMarker 这样真正的模板引擎呢?那会更容易。
Why don't you use a real template engine like FreeMarker? That would be easier.