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.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Servlet 3.0 Overview"

  1. Pankaj says:

    You missed one important feature, Async support.

Leave a Reply


nine − = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books