File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet Init Parameter's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Init Parameter Watch "Servlet Init Parameter New topic

Servlet Init Parameter's

Chad Dalton

Joined: Feb 19, 2003
Posts: 20
I have a servlet that loads on startup (<load-on-startup>1</load-on-startup> . It takes 1 parameter, a file name. It's the name of a file on the file system that contains a list of properties for the servlet. When I start the server I see that the servlet reads the init-parameter name/value pair correctly and reads the properties file and populates the member variables of the servlet. However, when I navigate to the servlet the init is called again and this time the properties filename is empty so it fails on unable to open null. How can I configure it so the value in web.xml will be used for every instance of the servlet? I would like NOT to make it a context level parameter.
Server: Tomcat 4.1.27, JDK 1.3 (unable to upgrade, being hosted).
Ken Boyd
Ranch Hand

Joined: Dec 10, 2003
Posts: 329
In previous project I had property file on server (contains database URL & Password etc..) I was retrieving values from file using following code.

What you are doing is put file name in servlet init parameter like this
<param-value>"<actual nameof file>"</param-value>
Now in your init you get just name of file & read it anywhere else.
Hope I understand & this help.
SCJP 1.4

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I vaguely recall that init() being run twice on the same servlet was the result of "invoker servlet" evilness.

Are you calling the servlet through a path like: "/servlet/foo" ?
Chad Dalton

Joined: Feb 19, 2003
Posts: 20
Originally posted by Mike Curwen:

Are you calling the servlet through a path like: "/servlet/foo" ?

Yes, and that was partically my problem. In the <name> portion of the <servlet> parmeter of my web.xml I had, once I woke up and removed the servlet. part everything worked fine .
I agree. Here's the link:
subject: Servlet Init Parameter's