when developing web application using JSP and servlet this is the biggest question in my mind that where to put the CODING OF DATABASE CONNECTION...
In servlet, if we put the connection code inside the service() (or doXXX ()) method then for every request a connection is established between the application and the database; and this leads to consumption of time and resources so this is not a good option...
And if we put the connection code inside the init() method (by initializing the instanse variable) then only one connection is made and all the user's requests can use that same connection for communication with the database in service() (or doXXX ()) method; and when we think about simultanious requests, this may lead to synchronization problem because all the requests are using the same connection...
So what is the elegant solution of this problem ?