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?

"itemprop =" text ">

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 web.xml file). To do this, you can create a class that implements ServletContextListener

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

Example:

  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); }}  

You can get this map servletException protected by zero doGet (HttpServletRequest request, HttpServletResponse response), IOException {// throws in the servlets like

  .. map & lt; You, type & gt; Map = (map & lt; your, type & gt;) getServletContext () .getAttribute ("mySpecialMap"); // ...}  

Oh, and one important thing: do not forget to add this listener to your web application, then you

  Lt; Listener & gt; Will add & lt; Audience category & gt; Full.name.of.ContextListenerImpl & lt; / Listener Category & gt; & Lt; / Listener & gt;  

In your web.xml file.


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -