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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get context-param from web.xml in struts app

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hello,

I am developing a struts application, and I want to have my database credentials configured. So I had put it in my web.xml under <context-param>. I have a listener class which reads these parameters and sets it to the ServletContext.

I have another java class which will get the backend datbaase connection, here If I use "getServletContext().getAttribute("dbName");" I get an error "method getServletContext() is undefined for my class"

can anyone let me know how can I get the set ServletContext attributes in my "DbConnection" java files.

Thanks a lot in advance
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
The getServletContext() method belongs to the GenericServlet so unless you extend GenericServlet or HttpServlet you will not be able to use the method directly.

You could also get the servletContext using sessions - I think its session.getServletContext().

Of course your DBA class is unlikely to have access to either; so you would have pass the Connection String to the DBA class somehow - I not really sure of a good way in which this could be done. (I would generally end up using a CONSTANT to hold the database connection String).
 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hmmn I'll try and se if I can do it in nay way.. Will keep it posted if I get through..!

Thanks anyways Sam..
 
Sheriff
Posts: 67747
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:
  • Report post to moderator
Please do not cross-post the same question in multiple forums. It wastes people's time when multiple redundant conversations take place. Please read this for more information.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic