java - How to load once (when Tomcat starts) some data (HashMap) to Tomcat's cache? -
I am working on some Java EE application which is to find some data in the HashMap. The problem is that I want to load this Hashmap only once - when Tomcat starts and I do not know how to do it. Can you give me some suggestions?
Assuming that you can only do this for a web application when you load this container Want to load the HashMap settings for your app (hereafter reads the in its Example: You can get this map servletException protected by zero doGet (HttpServletRequest request, HttpServletResponse response), IOException {// throws in the servlets like Oh, and one important thing: do not forget to add this listener to your web application, then you In your web.xml
file). To do this, you can create a class that implements ServletContextListener
contextInitialized
method to create the HashMap you are interested in, and Add as attribute for ServletContext
. Can not just be an ServletContext
example for a web application and this example is available for all servlets / so they can get that property later with the map set before you JSP.
class ContextListenerImpl implies ServletContextListener {@Override Public Zero contextDestroyed (ServletContextEvent SCE) {// can be empty for now} Public Zero contextInitialized @Override (ServletContextEvent SCE) {circactorct sc = sce.getServletContext (); // ... Here you can create and initialize your hashmem, when the map is ready, then add it as a feature for the property sc.setAttribute ("mySpecialMap", map); }}
.. map & lt; You, type & gt; Map = (map & lt; your, type & gt;) getServletContext () .getAttribute ("mySpecialMap"); // ...}
Lt; Listener & gt; Will add & lt; Audience category & gt; Full.name.of.ContextListenerImpl & lt; / Listener Category & gt; & Lt; / Listener & gt;
web.xml
file.
Comments
Post a Comment