Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to get init parameters in servlet

 
R K Parulekar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change this in yr web.xml:

 
R K Parulekar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Ben & Vince
It's working now !
Ravi..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic