aspose file tools
The moose likes Servlets and the fly likes How to get context-param from web.xml in struts app Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply locked 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
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 576

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).


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Rakesh Rajmohan
Greenhorn

Joined: Jul 09, 2009
Posts: 22
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..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56543
    
  14

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.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to get context-param from web.xml in struts app
 
Similar Threads
How to get context-param from web.xml in struts app
Listeners Doubt?
Shared Object, Synchronization, Servlet Context
ServletContext Null pointer Exception
servletConfig and servletContext