GeeCON Prague 2014*
The moose likes Servlets and the fly likes ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext" Watch "ServletContext" New topic
Author

ServletContext

Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Hi all,
I'm having trouble reading a context-param from my web.xml file. I'm trying to do this from a helper class...not a servlet. Each time a servlet calls a method in this helper class (DatabaseUtils) I want it to retrieve the database connection string. I've tried many different versions of
getServletContext().getInitParameter("connectionString");
and cannot make it work. I get the error that it can't identify method getServletContext() so I tried variations of
ServletConfig config = getServletContext();
and this will not work. I even defined an init method for the class like this:

which compiles but my String con never gets the connectionString. Anyways, I'm not sure that defining an init method for a helper class is a good idea.
I'd really appreciate some help on this. I've wasted a lot of time on it and the whole reason behind this is so I can work on an application on my local machine and tranfer it back and forth between the live server and my local one without ever having to change things like database connection strings, absolute URL's etc.
Thanks in advance,
-Pat
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Moving to the Servlets forum...
[ January 09, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Mark Howard
Ranch Hand

Joined: Feb 14, 2001
Posts: 285
Hi Pat
You are overriding the init(ServletConfig config) method of the GenericServlet class.
This is ok, but you must add a call to super.init(config) so that GenericServlet is aware of the config object.
Better idea would be to override the GenericServlet's no-parameter init() method, which is largely provided for overriding purposes. Behind the scenes, the init(ServletConfig config) method actually calls the no-parameter init() method, which you can then override and implement at will.
Alternatively, you might also consider using creating a listener class that implements the ServletContextListener interface to retrieve your DB connection string when the Context is initialized.
Hope this helps
Mark.
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Thanks for your response Mark! Maybe I should have started off stating that I don't quite understand what is going on as I'm using a class that isn't a servlet.
This class - DatabaseUtils - contains some common methods I use, one of them being makeDatabaseConnection()...which returns a Connection object. I don't have an init method for it as I only make instances of it when I need to use a method from it. I call it with it's only constructor which contains no arguments.
Where I'm confused is in the fact that I'm not sure if I should define an init() method for this class, or if I should make a second constuctor that I pass the Config argument to it. Basically, all I want to do is be able to obtain access to the Context so I can read the init-parameter from my web.xml file.
I mentioned this to a friend today and he suggested I approach this from a different angle and use something called the JDBC realm in the server.xml file so I can define my connection to the database.
My only goal is to set the connection string somewhere permanently so I can read it out from my DatabaseUtils class, and not have to change it when I move between servers. Right now, I have the string hardcoded into the makeDatabaseConnection() method so when I make changes to this class on my own machine and then upload it to the live server I have to change the connection string.
I'm open to suggestions on this and appreciate the help :-)
Thanks,
-Pat
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
It's a little hard to tell what's going on from the short snippet you gave, but try this.
In your servlet:

Passing just the connection string instead of the whole ServletConfig object reduces the coupling between your helper and the servlet. E.g. you can write unit tests for your helper without having to build a mock ServletConfig object.
David
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Hi David,
This still isn't working. I get the error that the method getServletConfig() cannot be found.
It seems to me that I just can't access the Servlet Context from a helper class unless I define an init() method in it or actually pass the Config object to the constructor of the helper class as an argument. I don't want to start doing the latter as I'd have to go and change a bunch of pages to make that work
-Pat
[ January 13, 2003: Message edited by: Pat Wallwork ]
Sam Dalton
Author
Ranch Hand

Joined: Jul 26, 2001
Posts: 170
Hi Pat,
You will not be able to use the method getServletConfig() from a helper class. This method is defined within the GenericServlet base class, and as you helper does not implement this (and neither should it) you will not be able to use it. You could pass the ServletConfig to the helper eg:

but this would seem like a bad idea to me. You should question why you are doing this in a helper class and consider erfactoring it to be within a servlet.
Cheers
Sam


<a href="http://www.samjdalton.com" target="_blank" rel="nofollow">Sam Dalton</a>,<br />Co-author of [http://www.amazon.com/exec/obidos/tg/detail/-/1590592255/qid=1068633302//ref=sr_8_xs_ap_i0_xgl14/104-4904002-9274339?v=glance&s=books&n=507846]Professional JSP 2.0[/URL] (October 2003)<br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/1861007701/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional SCWCD Certification</a><br />Co-author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100561X/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional Java Servlets 2.3</a>
Pat Wallwork
Ranch Hand

Joined: Sep 23, 2001
Posts: 72
Thanks Sam,
That is what I wanted to know! I was coming to the conclusion that the only way to do it would be to pass the ServletConfig in an argument to the helper class which I didn't want to do.
In case you didn't read the above posts, the only reason I want access to it is so I can retrieve the <init-param> from my web.xml. As this has become a much bigger headache than I thought it would be I am looking for an alternative.
Thanks for the input everyone
-Pat
 
GeeCON Prague 2014
 
subject: ServletContext