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: Initialization parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet: Initialization parameters" Watch "Servlet: Initialization parameters" New topic

Servlet: Initialization parameters

Shekhar Tayade

Joined: Apr 11, 2001
Posts: 9
I am running Tomcat 3.1 and as per the documentation I have put the initialization parameters in web.xml in webapps\root\web-inf folder. I still cann't get these parameters using the getInitParameter() method of ServletConfig. I have made sure that the name and case of the parameter I am using is correct. I am using following code in a servlet to access the parameter:
public void init(ServletConfig config ) throws ServletException{
parameter1 = config.getInitParameter("param1");
I always get the value of "parameter1" variable as null.
Any help is highly appreciated.

William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
My first guess is that something about your web.xml file does not agree with the way your servlet is being invoked. If you are not starting the servlet using the name in web.xml then you won't be able to see the init parameters.
What does your web.xml entry for this servlet look like?

Shekhar Tayade

Joined: Apr 11, 2001
Posts: 9
Hi William,
Thanks! I was using the wrong name of the servlet in web.xml. It really helps to talk to somebody when you know there is something obviously wrong but you cann't see it.
Thanks again.
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
By the way, the web.xml deployment descriptor also allows you to provide initialization parameters that are not tied to any particular servlet:
In the example above, you could retrieve the JDBC driver class name in either servlet1 or servlet2 as follows:
ServletContext context = getServletContext();
String className = context.getInitParameter("JDBC.DRIVER");
or in a JSP:
<% String className =
application.getInitParameter("JDBC.DRIVER"); %>

Phil Hanna
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
I agree. Here's the link:
subject: Servlet: Initialization parameters
It's not a secret anymore!