"S. N. Prasanna" wrote:
> The problem here is, I am using a centralised DB Object which handles all
> the Database related stuff. It connects to DB in init() method of the
> Servlet and stays connected as long as the Servlet exists.
This is quite usual for server side applications. You would like to
centralize in one object the access to the database. This object will
handle the connection(s) to the database and will perhaps provide
"advanced" functionality such as caching. Of course there will be only
one object of this type in the system. So if you have only one servlet,
it is possible to instanciate it in the init() method and keep a
reference to the instance which will be used each time service() is
called. However this is not possible anymore if you have multiple
servlets or JSPs.
I am not sure if this can solve your problem, but what I usually do is
to create a Database class with a getInstance() static method. The first
time the method is called, it will create a new database object and
store it in a static variable. The next time, it will simply return the
stored instance. (I think this is a fairly standard implementation of
the singleton pattern.) This way, you can get access to a shared
database object in every JSP (and servlet) with a simple
Database.getInstance().
I've written a tiny example to illustrate this with a dummy database
class (it only increments a counter) and two JSPs (first.jsp and
second.jsp) which display the value of the counter. This is only to show
that an object can easily be shared between JSPs and this is of course
very useful for databases :-).
http://www.scdi.org/~avernet/try/jspshare/first.jsp
http://www.scdi.org/~avernet/try/jspshare/second.jsp
http://www.scdi.org/~avernet/try/jspshare/first.txt (source)
http://www.scdi.org/~avernet/try/jspshare/second.txt (source)
http://www.scdi.org/~avernet/try/jspshare/Database.java (source)
I hope this will help,
Alex
--
............................................
Alessandro Vernet - Software Engineer
Email - [EMAIL PROTECTED] or [EMAIL PROTECTED]
Home page - http://www.scdi.org/~avernet/
20030 Rodrigues Av. #7E - 95014 Cupertino CA
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".