• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why getInitParameter( ) return null?

 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi;

I have a very simple servlet called DateFormatServlet, which gets initialization parameters set up as <init-param> elements in the web.xml.

But the result is null, and enumeration is also null.
I am using apache tomcat 5.5.17.

Can anybody tell me why? Thanks in advance.



Here's the my servlet file.
----------------------------------
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;
import java.util.*;

public class DateFormatServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
System.out.println ( "months: " + getServletConfig().getInitParameter("months") );

Enumeration e = getServletConfig().getInitParameterNames();
while (e.hasMoreElements())
{
String parmName = (String) e.nextElement();
System.out.println ( "---> " + parmName + " " + getServletConfig().getInitParameter(parmName) );
}
}
}

here's my web.xml file
---------------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<servlet>
<servlet-name>DateFormatServlet</servlet-name>
<servlet-class>coreservlets.DateFormatServlet</servlet-class>
<init-param>
<param-name>dateFormat</param-name>
<param-value>yyyy-MM-dd</param-value>
</init-param>
<init-param>
<param-name>months</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>wildness</param-name>
<param-value>exaggerated</param-value>
</init-param>
</servlet>

</web-app>
 
Lalitha Vydyula
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tiffiny,
First of all you forgot to include <servlet-mapping>. Once I included <servlet-mapping>, I called your DateFormatServlet and it working fine for me. I'm using tomcat 5.0.28. There's nothing wrong with your code.
[ February 09, 2007: Message edited by: Lalitha Vydyula ]
 
Tiffiny Yang
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lalitha !!

You're right, once I define following in the web.xml

<servlet-mapping>
<servlet-name>DateFormatServlet</servlet-name>
<url-pattern>/DateFormatServlet</url-pattern>
</servlet-mapping>

and it only works when invoked the servlet using
http://localhost:8080/servlet/DateFormatServlet

It does not work if invoked with http://localhost:8080/servlet/coreservlets.DateFormatServlet

I thought <servlet-mapping> is for URL mapping, why it has something to do with Servlet Initialization Parameters.
The example I got from the tutorial and books does not specify this, am i missing something..?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic