wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to get init parameters in servlet" Watch "Unable to get init parameters in servlet" New topic
Author

Unable to get init parameters in servlet

R K Parulekar
Ranch Hand

Joined: May 29, 2005
Posts: 66
Hi
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
{
super.init(servletconfig);
context = servletconfig.getServletContext();
Enumeration en = context.getInitParameterNames();
System.out.println("Enum "+en);
while(en.hasMoreElements()){
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

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>test.MyServlet</servlet-class>
<init-param>
<param-name>file</param-name>
<param-value>test</param-value>

<param-name>dir</param-name>
<param-value>/var/lib/jakarta-tomcat-5.5.4/tmp/</param-value>
</init-param>
</servlet>


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




Thanks
Ravi


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

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.

Try:


By the way, I have both servlet and context init param demo apps on
http://simple.souther.us


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="http://vincehon.homeip.net:8000/VJW" target="_blank" rel="nofollow">http://vincehon.homeip.net:8000/VJW</a>
R K Parulekar
Ranch Hand

Joined: May 29, 2005
Posts: 66
Thank you so much Ben & Vince
It's working now !
Ravi..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to get init parameters in servlet
 
Similar Threads
Why this code to get Init Param in JSP not work?
web.xml and file uploading
environment variable referance in Web.xml
Servlet can't find intialisation paramters
init-param issues