I need a sanity check on my strategy to best fulfill the requirements of a project. Basically I have an xml file that will need to be parsed periodically and html output displayed (xslt is not an option).
我需要对我的策略进行健全检查,以最好地满足项目的要求。基本上我有一个xml文件,需要定期解析并显示html输出(xslt不是一个选项)。
My thought is that I can use a single .jsp page to check an application variable that stores the last parsed date to know if it's 'time' (x days have passed) to re-generate the file. If it is, I can parse the xml via jsp and write the results to a static html file which is then dynamically included into the same jsp for rendering, if not I skip the regeneration and simply include the html file. including the html file rather than just rendering the display is to save the response / processing time for subsequent visits.
我的想法是,我可以使用单个.jsp页面来检查存储最后解析日期的应用程序变量,以了解它是否为'时间'(x天已过)以重新生成文件。如果是,我可以通过jsp解析xml并将结果写入静态html文件,然后将其动态地包含在同一个jsp中进行渲染,如果不是,我跳过重新生成并简单地包含html文件。包括html文件而不仅仅是渲染显示是为了保存后续访问的响应/处理时间。
My question is whether this is a smart way to go about things, my specific concerns are 1) will I be able to overwrite an html file that may or may not be in use by visitors and 2) will the dynamic include via jsp work this way without a page redirect.
我的问题是这是否是一种聪明的方式来解决问题,我的具体问题是:1)我是否能够覆盖访问者可能使用或未使用的html文件; 2)动态包括通过jsp工作这个没有页面重定向的方式。
Thanks in advance for any thoughts and enduring the long post -
提前感谢任何想法并忍受长篇大论 -
b
3 个解决方案
#1
I'd use a cache (eg. ehcache) to store the generated html, with the required ttl.
我使用缓存(例如ehcache)来存储生成的html,以及所需的ttl。
Rendering could be handled by a servlet (you could use a jsp, but cleaner in a servlet) which would lookup the html from the cache, and simply return it as the response. If the html was not in the cache, either because it had not been generated or it had expired then it would get generated and stored in the cache.
渲染可以由servlet处理(您可以使用jsp,但在servlet中使用更清晰),它将从缓存中查找html,并将其作为响应返回。如果html不在缓存中,或者因为它尚未生成或者已经过期,那么它将被生成并存储在缓存中。
The generation of the html from the xml could even be handled by a separate thread to avoid any delays to the user while it was being generated.
从xml生成html甚至可以由单独的线程处理,以避免在生成时对用户造成任何延迟。
#2
Doing the XML generation (translation) in a Servlet might make more sense. I say this because you'll most likely have a bunch of Java code. The Servlet is probably more appropriate than a JSP for a bunch of programmatic processing.
在Servlet中执行XML生成(转换)可能更有意义。我这样说是因为你很可能拥有一堆Java代码。对于一堆程序化处理,Servlet可能比JSP更合适。
#3
Why not simply include the JSP itself? If the JSP happens to "cache" the XML in a static file, then so be it, but just be conscious that there will be a race condition (unless specifically handled) if the JSP is called twice while it needs to regenerate HTML file.
为什么不简单地包含JSP本身?如果JSP碰巧将XML“缓存”在一个静态文件中,那么就这样吧,但只要意识到如果在需要重新生成HTML文件的同时调用JSP两次,就会出现竞争条件(除非特别处理)。
But, at a minimum, this way the "clients" of the JSP aren't exposed to its "caching" mechanism (which happens to be an HTML file).
但是,至少,这种方式JSP的“客户端”不会暴露于其“缓存”机制(恰好是HTML文件)。
You could, for example, just create a new HTML file every time, and store its name in the application context, or whatever, thus you never overwrite the file. You could cache it RAM, you could not cache it at all!
例如,您可以每次只创建一个新的HTML文件,并将其名称存储在应用程序上下文中,或者其他任何内容,因此您永远不会覆盖该文件。你可以缓存它RAM,你根本无法缓存它!
The point being that the client only know about the JSP, and don't care how or why it creates the HTML, which is as it should be.
关键是客户端只知道JSP,并不关心它是如何或为什么创建HTML,这应该是它应该是。
#1
I'd use a cache (eg. ehcache) to store the generated html, with the required ttl.
我使用缓存(例如ehcache)来存储生成的html,以及所需的ttl。
Rendering could be handled by a servlet (you could use a jsp, but cleaner in a servlet) which would lookup the html from the cache, and simply return it as the response. If the html was not in the cache, either because it had not been generated or it had expired then it would get generated and stored in the cache.
渲染可以由servlet处理(您可以使用jsp,但在servlet中使用更清晰),它将从缓存中查找html,并将其作为响应返回。如果html不在缓存中,或者因为它尚未生成或者已经过期,那么它将被生成并存储在缓存中。
The generation of the html from the xml could even be handled by a separate thread to avoid any delays to the user while it was being generated.
从xml生成html甚至可以由单独的线程处理,以避免在生成时对用户造成任何延迟。
#2
Doing the XML generation (translation) in a Servlet might make more sense. I say this because you'll most likely have a bunch of Java code. The Servlet is probably more appropriate than a JSP for a bunch of programmatic processing.
在Servlet中执行XML生成(转换)可能更有意义。我这样说是因为你很可能拥有一堆Java代码。对于一堆程序化处理,Servlet可能比JSP更合适。
#3
Why not simply include the JSP itself? If the JSP happens to "cache" the XML in a static file, then so be it, but just be conscious that there will be a race condition (unless specifically handled) if the JSP is called twice while it needs to regenerate HTML file.
为什么不简单地包含JSP本身?如果JSP碰巧将XML“缓存”在一个静态文件中,那么就这样吧,但只要意识到如果在需要重新生成HTML文件的同时调用JSP两次,就会出现竞争条件(除非特别处理)。
But, at a minimum, this way the "clients" of the JSP aren't exposed to its "caching" mechanism (which happens to be an HTML file).
但是,至少,这种方式JSP的“客户端”不会暴露于其“缓存”机制(恰好是HTML文件)。
You could, for example, just create a new HTML file every time, and store its name in the application context, or whatever, thus you never overwrite the file. You could cache it RAM, you could not cache it at all!
例如,您可以每次只创建一个新的HTML文件,并将其名称存储在应用程序上下文中,或者其他任何内容,因此您永远不会覆盖该文件。你可以缓存它RAM,你根本无法缓存它!
The point being that the client only know about the JSP, and don't care how or why it creates the HTML, which is as it should be.
关键是客户端只知道JSP,并不关心它是如何或为什么创建HTML,这应该是它应该是。