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).

[3365 byte] By [Stevie-Ba] at [2007-10-3 0:36:51]
# 1

When you call the url

http://localhost:8080/xxx/servlet/test.InitParamServlet

you are actually calling the invoker servlet that is defined in the default conf/web.xml. If you look at this web.xml file you will see that the invoker is mapped with the pattern "/servlet/*" which maps to the above url. The request goes to this servlet which parses the url and then calls the InitParamServlet class.

When you call the url

http://localhost:8080/xxx/test.initparam

you are matching the url pattern you defined in the WEB-INF/web.xml file ("*.initparam"). This time the request goes to servlet instance that you defines in the servlet naming tags as "InitParamServlet" and since you defined init params they are populated.

Note that these urls will also match the "*.initparam" pattern

http://localhost:8080/xxx/XXXXX.initparam

http://localhost:8080/xxx/badpackage.initparam

http://localhost:8080/xxx/A.initparam

Most people would have simplely defined the second servlet mapping as "initparam" and accessed the servlet with the url

http://localhost:8080/xxx/initparam

tolmanka at 2007-7-14 17:30:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks for the clarification, but why does the servlet init parameter is handled different by the servlet (container)? I expect that this should not depend on the type of invoke.

With http://localhost:8080/xxx/servlet/test.InitParamServlet

the servlet init parameter 'param1' is null

With http://localhost:8080/xxx/test.initparam

the servlet init parameter 'param1' is value1 (what is the correct value)

Stevie-Ba at 2007-7-14 17:30:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
No explanation about the behavior? Not one?!
Stevie-Ba at 2007-7-14 17:30:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi,

These all happen for priority.

>> if you define same mapping in your web.xml as that of conf/web.xml then it gives the priority to your mapping.

>> wildcard has least priority.

now more prescribe:

if you define the different mapping for same servlet then you get different instance call for the same servlet.

e.g mapping /*.a param a

mapping /*.b param b

mapping /*.c param c

for same servlet.

Now you will get param 'a' for url that map to /*.a

you will get param 'b' for url that map to /*.b

I think it is fine to you.

bye............

Amit_Rohillaa at 2007-7-14 17:30:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...