File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 | Flagged Topics | Hot Topics | Zero Replies
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: 606

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: 61774
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get context-param from web.xml in struts app