File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet: Initialization parameters" Watch "Servlet: Initialization parameters" New topic
Author

Servlet: Initialization parameters

Shekhar Tayade
Greenhorn

Joined: Apr 11, 2001
Posts: 9
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.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
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


Java Resources at www.wbrogden.com
Shekhar Tayade
Greenhorn

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.
Shekhar.
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:
<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


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet: Initialization parameters
 
Similar Threads
what is ServletConfig whats its role in Servlets?
Confusion is JSP initialization
Servlets and ServletConfig problem
purpose of init method
init(ServletConfig config) query