aspose file tools*
The moose likes Servlets and the fly likes How to access a servlet context from a JavaBean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to access a servlet context from a JavaBean?" Watch "How to access a servlet context from a JavaBean?" New topic
Author

How to access a servlet context from a JavaBean?

Joao Bonina
Greenhorn

Joined: Dec 21, 2002
Posts: 2
Hello everybody!
I'm writing my first Tomcat web application and I'm establishing a connection to the database and putting it in the controller servlet's context.
Next, I'll have some JavaBeans that want to access the database thru the default connection.
Is it possible for a Javabean to access the connection object stored in the controller servlet's context?
Thanx in advance!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

The servlet context is available from the servlet or JSP page that is currently executing. In order to have it available within a JavaBean, you'll have to pass the reference to the servlet context to the bean.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joao Bonina
Greenhorn

Joined: Dec 21, 2002
Posts: 2
Oh, I see... You know, that's what I was trying to avoid, passing the connection as a parameter to the bean. Guess I'll have to rethink this all up!
Thank you for your help!
David Brafford
Ranch Hand

Joined: Feb 11, 2001
Posts: 91
There are some other ways - for example, putting
the ServletContext in the Application Context
or ( less desirably ), the session.
The bean could then lookup the Servlet context
in the Application ( or session ) context.
Another way is passing in the ServletContext
in the Bean Constructor ( a parameter, but done
only once ).
Food for thought,
Dave Brafford
---------------------------------
Considering the Certified Java Programmer Exam?
Get JCertify!
Baldwin, Eckel, Green, and more
http://www.enterprisedeveloper.com/jcertify
The best investment in your career you will make all year
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access a servlet context from a JavaBean?