Changes to Servlet and Filter Registration APIs in Servlet 3.0

Changes to Servlet and Filter Registration APIs in Servlet 3.0

Changes to Servlet and Filter Registration APIs in Servlet 3.0

Right after the Public Review draft of the Servlet 3.0 specification had been handed off to the JCP for publication, the Servlet EG changed the signature of the addServlet and addFilter APIs on javax.servlet.ServletContext. These APIs are new in Servlet 3.0, and allow servlets and filters to be registered at runtime, without having to be declared in a web.xml deployment descriptor.

It was felt that the previous method signatures:

  addServlet(String servletName, String description, String className, Map initParameters, int loadOnStartup, boolean isAsyncSupported)

and

  addFilter(String filterName, String description, String className, Map initParameters, boolean isAsyncSupported)

had too many parameters, and there was concern that these methods would have to be overloaded every time a new parameter was added in the future.

Therefore, the Servlet EG decided to change addServlet to take just servletName and className parameters, and to change addFilter to take just filterName and className parameters, and to have these methods return ServletRegistration and FilterRegistration objects, respectively, on which any further configuration would occur.

For example, the following ServletContextListener registers one servlet and one filter when invoked at its contextInitialized method, and then further configures the registered servlet and filter through their respective ServletRegistration and FilterRegistration objects:

  public void contextInitialized(ServletContextEvent sce) {
      ServletContext sc = sce.getServletContext();

      /\*
       \* Register servlet
       \*/
      ServletRegistration sr = sc.addServlet("NewServlet", "test.NewServlet");
      sr.setInitParameter("servletInitName", "servletInitValue");
      sc.addServletMapping("NewServlet", new String[] {"/newServlet"});

      /\*
       \* Register filter that is going to intercept any requests to the
       \* servlet
       \*/
      FilterRegistration fr = sc.addFilter("NewFilter", "test.NewFilter");
      fr.setInitParameter("filterInitName", "filterInitValue");
      sc.addFilterMappingForServletNames("NewFilter",
          EnumSet.of(DispatcherType.REQUEST), true, "NewServlet");
  }

While these changes are not reflected in the Public Review draft of the Servlet 3.0 specification, they have already been implemented in the trunk version of GlassFish v3, which is going to be the reference implementation of the Servlet 3.0 specification.

Please send any questions or comments to the GlassFish webtier alias, or post them to the GlassFish webtier forum.

Comments:

my applet is singed!

Posted by joe on December 11, 2008 at 10:31 AM PST #

Have you seen Li?

Posted by Gigi on December 11, 2008 at 12:14 PM PST #

Una macchina video per vedere
tutti i linguaggi della vita
internet sotto sempre
nuovi e sofisticati
on-demand.

Posted by Ciro Alliney on December 11, 2008 at 03:51 PM PST #

[Trackback] awesome! did you have a look at the servlet 3.0 API? I did not yet, but was amazed to see something like this: public void contextInitialized(ServletContextEvent sce) { ServletContext sc = sce....

Posted by Confluence: codesmell on December 11, 2008 at 06:48 PM PST #

[Trackback] awesome! did you have a look at the servlet 3.0 API PD? I did not yet, but was amazed to see something like this: public void contextInitialized(ServletContextEvent sce) { ServletContext sc = sce....

Posted by Confluence: codesmell on December 11, 2008 at 07:02 PM PST #

just a quick question: is it really necessary to pass the Servlet´s name to the addServletMapping method? might be just me, but i cannot think of a valid usecase where i add a mapping for Servlet through the registration object of another.

in that case the Servlet´s is not necessary and forcing the developer to repeat it is a source of mistake, right?

Posted by uwe schaefer on December 11, 2008 at 07:09 PM PST #

Servlet 3.0 has many changes and notably the use of annotations.

Posted by Krishna on December 23, 2008 at 11:45 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

jluehe

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today