Forums Register Login

Servlet: Initialization parameters

+Pie Number of slices to send: Send
Hi,
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{
super.init(config);
parameter1 = config.getInitParameter("param1");
........
I always get the value of "parameter1" variable as null.
Any help is highly appreciated.
Thanks!
Shekhar.

+Pie Number of slices to send: Send
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?
Bill

+Pie Number of slices to send: Send
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.
Shekhar.
+Pie Number of slices to send: Send
By the way, the web.xml deployment descriptor also allows you to provide initialization parameters that are not tied to any particular servlet:
<web-app>
...
<context-param>
<param-name>JDBC.DRIVER</param-name>
<param-value>
org.enhydra.instantdb.jdbc.idbDriver
</param-value>
</context-param>
...
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>servlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>servlet2</servlet-class>
</servlet>
...
</web-app>
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
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 609 times.
Similar Threads
init(ServletConfig config) query
Servlets and ServletConfig problem
purpose of init method
Confusion is JSP initialization
what is ServletConfig whats its role in Servlets?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:15:08.