Enterprise Java

Servlet 3.0 Overview

Servlet 3.0 – has a bunch of ground breaking features that will ultimately change how developers will code and write JEE Web Applications – some might say that its conventions defy most of our understanding about how things “should” work in theory but thats one of the beauty of innovation and continuous improvement; challenging the convention and give a better and efficient solution.

Topics covered in this blog post:
 
 
 
 

  1. Ease of Development – what did change?
  2. Dynamic registration of Servlets and Filters
  3. Pluggability
  4. Resources in bundled jar files – A new feature called Web fragments.

Ease of Development

This is one of the subjective topics in world of JEE Development – its not about drag and drop all the time, its about how you can further refine your way of development that complies to standards, optimal and strictly checked across conventions.

Declarative style of programming through annotation – In JEE6, the web.xml is now optional and all POJOS can now be tagged thru use of Annotations. You can declare Servlets, Filter, Listeners, Init Params, etc – almost everything that is under the web.xml can now be configured thru use of annotations.

  1. @WebServlet – Define a Servlet
  2. @WebFilter – Define a Filter
  3. @WebListener – Define a Listener
  4. @WebInitParam – Define init params
  5. @MultipartConfig – Define file upload properties
  6. @ServletSecurity – Define security constraints

Note: Although, this can be done, developers can still use the web.xml to override these values.

I attached an example here for you to test.

Dynamic Registration of Servlets and Filters

Another feature available is Dynamic Registration – Performed during ServletContext initialisation:

public class TestServletContextListener
             implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        ServletContext servletContext = sce.getServletContext();

        ServletRegistration.Dynamic dynamic =
                   servletContext.addServlet("DynamicServlet","com.mycom.MyServlet"); 
        dynamic.addMapping("/dynamicServlet"); // URL mapping
        dynamic.setAsyncSupported(true);
    }
}

and of course, you can look up and hook the dynamic servlet / filter

ServletRegistration declared = ServletContext.getServletRegistration("DeclaredServlet");
declared.addMapping("/declaredServlet");
declared.setInitParameter("param", "value");

Download the maven project here.

Pluggability

Enable use of 3rd party framework without boiler plate configuration in deployment descriptors. Modularize web.xml to allow frameworks to be self-contained within their own JAR file and Programmatic configuration APIs Use of annotations.

The motivation behind pluggability is to decrease the complications of putting all configuration in one single sile (web.xml) if a specific framework is needed. For example, if a developer needs Spring support for an existing Web Application – the 1st thing to do is put in the servlet/ listener for Spring as part of it support – it can get ugly when in the future, a new EE technology arises as you need to put everything again on the web.xml file.

Now with JEE6 – you can create a sub-project with a web-fragment.xml that will mimic a section on the main web.xml – this allows further improvements (or pluggable functional requirements) to be independently created and isolated. Below is an example of a web-fragment.xml

<web-fragment>
  <servlet>
    <servlet-name>welcome</servlet-name>
    <servlet-class>com.mycom.WelcomeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>welcome</servlet-name>
      <url-pattern>/Welcome</url-pattern>
  </servlet-mapping>
  ...
</web-fragment>

It is closely / very similar to web.xml.

Download the Sample web-fragment project here.

Resources in bundled jar files

  • Static and JavaServerTM Pages (JSP) resources no longer confined to web application’s document root
  • May be placed inside WEB-INF/lib/ [*.jar]/META-INF/resources
  • Container must honor this new location when processing HTTP requests and calls to ServletContext#getResource or ServletContext#getResourceAsStream  methods
  • Resources in document root take precedence over those in bundled JAR files, however.

javaee6_servlet3-0_basics__1_-pdf__page_29_of_32_

Given the web-fragments and the new resources bundle jar support – functional requirements that requires end to end processing can now be developed separately from the main parent application.

The new and improve Servlet 3.0 brings a lot of effective tools that will allow developers to create more quality and robust applications with the platform.  Annotation / Declarative programming – takes the descriptors to new heights as Servlets, Filters and Listeners can now be done on the Java Code level. Dynamic Registrations of Servlets, Filters, Listeners etc can be used to create JIT pojos that will handle specific business cases, DI and CDI support allows developers simplify the paradigm by simply letting the container take care of how the objects are made and the best of them all, web-fragments and support for resource bundle jar gives a different separation to the development as it allows isolation of functional specific – web project, leveraging JEE existing technologies without having to deal with a lot of configuration thru the parent web application.
 

Reference: Servlet 3.0 from our JCG partner Alvin Reyes at the Alvin “Jay” Reyes Blog blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pankaj
10 years ago

You missed one important feature, Async support.

Back to top button