• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ServletContext

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Servlets forum...
[ January 09, 2003: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Author
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Pat Wallwork
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic