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

Servlet: Initialization parameters

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Shekhar Tayade
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic