tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Roßbach ...@objektpark.de>
Subject Tomcat 7 ; Servlet API question - Spec 8.2.3 overwrite WebServlet annotation parameter
Date Wed, 28 Apr 2010 19:26:53 GMT
Hi,

I have read the servlet 3.0 spec and play around with some simple  
examples.

I setup a very simple annotated servlet:

==
package annotation;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(value = "/param", name= "param", initParams = {
         @WebInitParam(name = "foo", value = "Hello "),
         @WebInitParam(name = "bar", value = " World!") })
public class ParamServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
	    throws IOException, ServletException
	  {
	    PrintWriter out = res.getWriter();
	    out.println(getInitParameter("foo"));
	    out.println(getInitParameter("bar"));
	  }
}
===

Next the with parameter overwriting don't work with current trunk!

=== web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd

">
     <servlet>
         <servlet-name>param</servlet-name>
         <servlet-class>annotation.ParamServlet</servlet-class>
         <init-param>
             <param-name>foo</param-name>
             <param-value>hello</param-value>
         </init-param>
         <init-param>
             <param-name>bar</param-name>
             <param-value>peter</param-value>
         </init-param>
     </servlet>
     <servlet-mapping>
         <servlet-name>param</servlet-name>
         <url-pattern>/param1</url-pattern>
     </servlet-mapping>
</web-app>
====

I read the servlet 3.0 (November 2009 final version) chapter "8.2.3  
Assembling the descriptor from web.xml, web-
fragment.xml and annotations". At page 81 8.2.3.3.n) are describe  
following override rule:

n. Any metadata specified via an annotation that isn’t already present  
in the
descriptor will be used to augment the effective descriptor.
i. Configuration specified in the main web.xml or a web fragment takes
precedence over the configuration specified via annotations.
ii. For a servlet defined via the @WebServlet annotation, to override  
values
via the descriptor, the name of the servlet in the descriptor MUST  
match the
name of the servlet specified via the annotation (explicitly specified  
or the
default name, if one is not specified via the annotation).
iii. Init params for servlets and filters defined via annotations,  
will be
overridden in the descriptor if the name of the init param exactly  
matches
the name specified via the annotation. Init params are additive  
between the
annotations and descriptors.
iv. url-patterns, when specified in a descriptor for a given servlet  
name
overrides the url patterns specified via the annotation.
v. For a filter defined via the @WebFilter annotation, to override  
values via
the descriptor, the name of the filter in the descriptor MUST match  
the name
of the filter specified via the annotation (explicitly specified or  
the default
name, if one is not specified via the annotation).
vi. url-patterns to which a filter is applied, when specified in a  
descriptor
for a given filter name overrides the url patterns specified via the
annotation.
vii. DispatcherTypes to which a filter applies, when specified in a  
descriptor for
a given filter name overrides the DispatcherTypes specified via the
annotation.
viii. The following examples demonstrates some of the above rules -

I think this define that web.xml or a fragment definition can  
overwrite annotation defaults.
Also we must merge web.xml definition with annotation parameters.

OK!

But Tomcat 7 currently don't support this:

--- java/org/apache/catalina/startup/ContextConfig.java	(revision  
937984)
@@ -1890,17 +1890,34 @@

      protected void processAnnotationWebServlet(String className,
              AnnotationEntry ae, WebXml fragment) {
       if (fragment.getServlets().containsKey(className)) {
             // Skip this annotation. Entry in web.xml takes priority
             return;
       }

I am now start to correct the implementation.
Why the TCK don't show errors?

Peter


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message