Different behaviour on servlet w/o servlet-mapping and init parameters
I was playing around and found, that the init-parameter of a servlet is always null if there is no servlet mapping. I did not define a servlet mapping because I used the servlet only for for (named) dispatching (client usage should not be allowed).
web.xml:
<?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>InitParamServlet</servlet-name>
<servlet-class>test.InitParamServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>value1</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>InitParamServlet</servlet-name>
<url-pattern>*.initparam</url-pattern>
</servlet-mapping>
</web-app>
InitParamServlet.java:
package test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass InitParamServletextends HttpServlet{
privatestaticfinallong serialVersionUID = 1;
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.write(getServletName());
pw.write(": param1=" + getInitParameter("param1"));
}
}
The URL http://localhost:8080/xxx/servlet/test.InitParamServlet
returnsorg.apache.catalina.INVOKER.test.InitParamServlet: param1=null
(on Tomcat), and the call to the same servlet with URL http://localhost:8080/xxx/test.initparam
returnsInitParamServlet: param1=value1 instead (the correct init parameter value)!
The same happens with jetty. This looks strange for me; I would expect the same behaviour for the servlet (independent from servlet-mapping-tag in the web.xml).

