DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • NullPointerException in Java: Causes and Ways to Avoid It
  • Dependency Injection in Spring

Trending

  • The Future of Java and AI: Coding in 2025
  • Monolith: The Good, The Bad and The Ugly
  • How To Introduce a New API Quickly Using Quarkus and ChatGPT
  • How to Create a Successful API Ecosystem
  1. DZone
  2. Coding
  3. Frameworks
  4. An Overview of Servlet 3.0

An Overview of Servlet 3.0

By 
James Sugrue user avatar
James Sugrue
DZone Core CORE ·
Jan. 21, 09 · Interview
Likes (2)
Comment
Save
Tweet
Share
77.1K Views

Join the DZone community and get the full member experience.

Join For Free

JSR 315 (Servlet 3.0) is an update to the existing Servlet 2.5 specification. Servlet 3.0 is focussed on extensibility and web framework pluggability, aligning with the goals of Java EE 6. Ease of Development (EoD) will be supported using newer language features. A reference implementation is available in the GlassFish v3 nightly build. The public review contains:

  • Pluggability
  • EoD
  • Async Support
  • Security Enhancements
  • Miscellaneous Changes

In this article we will bring you up to speed with what's happening with the Servlet 3.0 specification and give more detail on what is included.

Note: this article corresponds to the public review of the specification. As it is not yet final some things may change.

The Expert Group

Rajiv Mordani from Sun Microsystems is the specification lead with an expert group comprised of many of the most recognisable names in the Java community:

  • Adobe Systems Inc.
  • Apache Software Foundation
  • BEA Systems
  • Ericsson AB
  • Google Inc.
  • Hunter, Jason
  • IBM
  • Icesoft Technologies Inc
  • NCsoft Corporation
  • Oracle
  • Pramati Technologies
  • Prasanna, Dhanji R.
  • SAP AG
  • Ship, Howard M. Lewis
  • Suleiman, Hani
  • Sun Microsystems, Inc.
  • Tmax Soft, Inc.
  • Walker, Joe
  • Wilkins, Gregory John

Pluggability

Due to the popularity of so many various web frameworks, Servlet 3.0 will make it easier to use and configure the developer's framework of choice. So if you want to add in Struts, or Spring Web Flow it will be easy to do so.

  • Methods to add Servlets and Filters

    If a ServletContextListener is registered and wants to add a Servlet or Filter, then at the time of context initialization the event that is fired to the Listener can add Servlets and Filters to the context. The methods are addServlet and addFilter. For more details look for the javadocs at the JCP site at  http://jcp.org/aboutJava/communityprocess/pr/jsr315/index.html.

  • Web fragments

    Instead of having just one monolithic web.xml that is used by the developer to declare servlets, filters and other configuration for using the framework, the framework can now include a web-fragment.xml with all it's configuration.

    A web-fragment is almost identical to the web.xml and can contain all the configuration needed by the framework in the META-INF directory of the framework's jar file. The container will use the information to assembe the descriptor for the application and the application needn't have to use any of the boilerplate configuration in their app for the framework. There are rules that are defined int the specification for conflict resolution, overriding and disabling fragment scanning. Along with the annotations which also can be in libraries the feature is very compelling not only for the developers that use frameworks but also for framework authors to be self sufficient in defining the configuration needed.

    The great benefit to this is that library providers can supply their own web.xml fragment for use in your webapp.

Ease of Development

Several new annotations have been defined for ease of development in Servlet 3.0, allowing you to write a Servlet without requiring a descriptor. These annotations reside in the javax.servlet.annotation package. Thanks to the addition of annotations, it is now possible to have Servlet, Filter and ServletContextListener in a war file without a web.xml.

It's important to point out that this makes the web.xml optional, rather than redundant. The web.xml may be user to override metadata specified via annotations.

Following discussions in the expert group and the community, it was decided that method level annotations were not to be added, so you keep the doGet, doPost methods and need to extend HttpServlet to use them.

Let's take a closer look at the annotations present in the Servlet 3.0 specification:

Servlet Annotation

In Servlet 3.0, servlet metadata can be specified using @WebServlet

@WebServlet(name="mytest", 
urlPatterns={"/myurl"}, 
initParams={ @InitParam(name="n1", value="v1"), @InitParam(name="n2", value="v2") }) 
public class TestServlet extends javax.servlet.http.HttpServlet { 
.... 
} 

TestServlet extends HttpServlet, while the meta data provided corresponds with the web.xml as follows:

Parameter

Annotation Parameter

web.xml

Servlet name

name=

<servlet><servlet-name>

URL Pattern

urlPatterns={ }

<servlet-mapping>

Initialization parameters

InitParams={ @InitParam{name=””, value=””}

<servlet>

<init-param>

<param-name>.. </param-name>

<param-value>.. </param-value>

<init-param>

 

Servlet Filter Annotation

ServletFilter meta data is specified using the @ServletFilter annotation

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
.... 
} 

public void destroy() { 
.... 
} 
} 

Parameter

Annotation Parameter

web.xml

URL Pattern

urlPatterns={ }

<servlet-mapping>

Initialization parameters

InitParams={ @InitParam{name=””, value=””}

<servlet>

<init-param>

<param-name>.. </param-name>

<param-value>.. </param-value>

<init-param>

 

Servlet Context Listener Annotation

A ServletContextListener is simply marked with the @WebServletContextListener rather than needing to list it the web.xml file.

@WebServletContextListener 
public class TestServletContextListener implements javax.servlet.ServletContextListener { 
.... 
public void contextInitialized(ServletContextEvent sce) { 
.... 
} 

public void contextDestroyed(ServletContextEvent sce) { 
.... 
} 
} 

Async Support

The biggest change made in the specification is the addition of asynchronous processing. The main cases to cover were:

  • Waiting for a resource to become available, such as JDBC or a call to a Web Service.

  • Generating response asynchronously

  • Using exisiting frameworks to generate responses after waiting the for asychronous operation to complete.

While the early draft had suspend and resume, the latest specification has two variations of the startAsync() method on ServletRequest.

One (startAsync() ) takes no parameters and initialises an AsyncContext with the original request and response. The ( startAsync() ) other takes a request and response as a parameter to initialise the AsyncContext with.

Servlets (@WebServlet) and Filters (@ServletFilter) that support asynchronous processing need to be annotated with the supportAsync attribute set. It is illegal to call startAsync if you have a Servlet or Filter that doesn't support asynchronous processing anywhere in the request processing chain.

The is also an AsyncListener that can be registered to get notification on timeout and completion of asynchronous processing of the request. This listener can be used to clean up resources if the wrapped request and response were used to create it's AsyncContext.

You can forward requests back to the container using the AsyncContext.forward(path) and AsyncContext.forward() methods so frameworks like JSP can create a response.

Security Enhancements

HTTPServletRequest will be enhanced with methods allowing programmatic login and logout, while ServletRequest will be able to force a login.The logout method will allow an application to reset the authentication state of a request without requiring the authentication to be bound to a HTTPSession.

Conclusion

The draft is just beginning to be implemented within Glassfish v3. The specification will be a required piece of Java EE 6 and hence all the features described above will be available in all Java EE 6 compatible containers. The presence of the various features described above will make developing Java web appications much easier.

Thanks to Rajiv Mordani for his input into this article, and to Shing Wai and Jan Luehe for the code snippets.

Annotation Framework Java EE Filter (software)

Opinions expressed by DZone contributors are their own.

Related

  • Filtering Java Collections via Annotation-Driven Introspection
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • NullPointerException in Java: Causes and Ways to Avoid It
  • Dependency Injection in Spring

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!