This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Struts and the fly likes How to get context-param from web.xml in struts app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to get context-param from web.xml in struts app " Watch "How to get context-param from web.xml in struts app " New topic
Author

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

Rakesh Rajmohan
Greenhorn

Joined: Jul 09, 2009
Posts: 22
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Rakesh Rajmohan
Greenhorn

Joined: Jul 09, 2009
Posts: 22
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

Joined: Jul 15, 2009
Posts: 377

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


I love this place!
Rakesh Rajmohan
Greenhorn

Joined: Jul 09, 2009
Posts: 22
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

Joined: Aug 26, 2008
Posts: 263
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.

SCJP 1.5(97%) My Blog
Rakesh Rajmohan
Greenhorn

Joined: Jul 09, 2009
Posts: 22
Great.. I think this will work.. thanks a lot dude..!
shivendra tripathi
Ranch Hand

Joined: Aug 26, 2008
Posts: 263
No problem buddy
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to get context-param from web.xml in struts app
 
Similar Threads
servletConfig and servletContext
Listeners Doubt?
Shared Object, Synchronization, Servlet Context
How to get context-param from web.xml in struts app
ServletContext Null pointer Exception