• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
No problem buddy
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic