Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Unable to get init parameters in servlet

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change this in yr web.xml:

 
R K Parulekar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Ben & Vince
It's working now !
Ravi..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic