i would like to initialize (in Struts2) a property(data loading from a file) only once and make available that property for entire struts 2 application.
how can i achieve that? do i need override struts 2 dispatcher?
Regards
Raju
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You could create a
ServletContextListenerdefined inweb.xmlthat opens your property file and sets the desired value to theServletContextvia:The
ServletContexthas application-wide scope.Update:
You can create a new class that implements
ServletContextListener(here is its JavaDoc: ServletContextListener), which requires that you definecontextInitialized()andcontextDestroyed()methods.The method
contextInitialized()is called right before your servlet begins accepting requests. In yourcontextInitialized()method, you would include thegetServletContext().setAttribute("dataKey", dataValue)call.In order to register your listener, you will need to add a listener definition in your
web.xmlfile:You’ll need to replace CLASS_PATH.CLASS_NAME in the above XML with the class path and name of the context listener class you just created.