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
  • 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
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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).
 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 64618
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
    Bookmark Topic Watch Topic
  • New Topic