• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to access a servlet context from a JavaBean?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joao Bonina
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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'm not sure if I approve of this interruption. But this tiny ad checks out:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic