views:

22

answers:

2

I need to modify the web start up service to query some database objects and put it into the session when the server(JBOSS 5) starts. Any idea what is the best way to do it? I am using struts2.1.6 + spring 2.5.6 + JPA + Hibernate.

Thanks.

A: 

I'm not familiar with JBoss, but in a typical web application you could implement the javax.servlet.ServletContextListener interface (overriding the contextInitialized method), then specify this class using a <listener> element in the deployment descriptor web.xml.

Another option that is preferable in some cases is to postpone the query until the first time your application actually requires it (i.e. "lazy" initialization).

Todd Owen
A: 

You need to put some data in session while statup(in session)... But session is created for each user. So what you can do is ServletContextListener interface and in sessionCreated method you can load data.

If data is common for all user then you can then u can create singleton class for That and load data from that object (In this way you don't need to query database again and again)

Below code might help

<listener>
        <listener-class>MyServlet</listener-class>
</listener>

This listener will be registered in web.xml and you will override sessionCreated() method to load your data

VinAy