How can I create a servlet that will initialize connections to different systems, in a way that other servlets can use them?

Alex Chaffee

If you want to share data between servlets, you can use Attributes. See the documentation on ServletContext for more information.

If you want to open up, for example, several JDBC connections that will be shared among several servlets, you can define a "behind the scenes" initialization servlet that has an init() method that opens the connections and adds them as Attributes. You then configure your servlet engine to initialize that servlet on startup (rather than on request). That way as soon as the engine boots, your init servlet will run and open the connections. (This servlet may never be called on its own! You can leave the service method undefined, or provide status or debugging information.)

Also, naturally, you should remember to close the connections in the servlet's destroy method.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact