• 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
  • 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:
  • Quote
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh Rajmohan wrote: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"


You have to make the Servlet Context object visible to that Java class by passing it as a parameter.
 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From where should I pass this Servlet Context object reference to my dbconnection java class.. I think the visibility of a serveletContext will only be there in my Servlet class. But when I use struts my servlet class (ActionServlet) cannot be changed isn't it..?

If there are ways to change it do let me know. And also let me know if there is any other way to pass my ServletContext to my dbconnection class
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I'm not sure that saving it in Servlet context is really going to give you any advantage - for example if you wanted to extract you data access class and use it on a desktop application, you would need to rewrite.

You may be better using a properties file or an xml file to store the database credentials and url?

Sean
 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds good to me.. I'll try it out..

But this solution is because I use struts right..? If I use normal MVC-1 type, I can use servletcontext in my servlets and that's the best way isn't it
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rakesh,

You can use ServletContext ctx = getServlet().getServletContext() in your action class(not ServletAction class). Then you can pass it as parameter in your DB class. It works very well atleast for struts1.
 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great.. I think this will work.. thanks a lot dude..!
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem buddy
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rakesh Rajmohan wrote:From where should I pass this Servlet Context object reference to my dbconnection java class.. I think the visibility of a serveletContext will only be there in my Servlet class. But when I use struts my servlet class (ActionServlet) cannot be changed isn't it..?


You can pass any valid Java object to any Java method, no matter whether its ServletContext, Connection OR RMI Object
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic