File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Unable to get init parameters in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to get init parameters in servlet" Watch "Unable to get init parameters in servlet" New topic

Unable to get init parameters in servlet

R K Parulekar
Ranch Hand

Joined: May 29, 2005
Posts: 66
Seek ur help..
Im unable to get the init param defined in web.xml

When servlets init is called , it does not print param names (pls see code below)
Also it prints 'null' for parameter file
If I change the order of parameters in web.xml,it always gives only last param and rest null

Init method and web.xml looks like

public void init(ServletConfig servletconfig)throws ServletException
context = servletconfig.getServletContext();
Enumeration en = context.getInitParameterNames();
System.out.println("Enum "+en);
System.out.println("Param names "+en.nextElement());
String dir = getInitParameter("dir");
String file = getInitParameter("file");

System.out.println("dir "+dir+" and file "+file);

Web.xml is as



O/p is as
dir /var/lib/jakarta-tomcat-5.5.4/download/ and file null


"A wish changes nothing. A decision changes everything." - Unknown
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

There are two types of init-params: context and servlet.

You're declaring them as servlet init params in your web.xml and then tring to read them as context init params in your servlet.


By the way, I have both servlet and context init param demo apps on

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Vince Hon
Ranch Hand

Joined: Feb 11, 2003
Posts: 117
change this in yr web.xml:

Vince Hon<br /> <br />SCJP 1.4 | SCWCD | SCBCD <br /><a href="" target="_blank" rel="nofollow"></a>
R K Parulekar
Ranch Hand

Joined: May 29, 2005
Posts: 66
Thank you so much Ben & Vince
It's working now !
It is sorta covered in the JavaRanch Style Guide.
subject: Unable to get init parameters in servlet
It's not a secret anymore!