Sunday, 24 April 2016

How to initialize the common connection before any Servlet / JSP will be accessed ?


Scenario
I need to initialize the common connection before any servlet / jsp will be accessed.

Solution
Make the connection in a context listener and save it.

Example
public class MyServletContextListener implements ServletContextListener {
  public void contextInitialized(ServletContextEvent event) {
     ServletContext sc = event.getServletContext();
     // Make connection and put connection object in servlet context
  }

  public void contextDestroyed(ServletContextEvent event) {
     // Clean up code here

  }

}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.