What's new in Servlet 3.1 ? - Java EE 7 moving forward

Guest Author

Servlet 3.0 was released as part of Java EE 6 and made huge changes
focused at ease-of-use. The idea was to leverage the latest language
features such as annotations and generics and modernize how Servlets
can be written. The web.xml was made as optional as possible. Servet
3.1 (JSR 340),
scheduled to be part of Java EE 7, is an incremental release
focusing on couple of key features and some clarifications in the

The main features of Servlet 3.1 are explained below:
  • Non-blocking I/O - Servlet 3.0 allowed asynchronous request
    processing but only traditional I/O was permitted. This can
    restrict scalability of your applications. Non-blocking I/O
    allow to build scalable applications. href="https://blogs.oracle.com/arungupta/entry/non_blocking_i_o_using">TOTD
    #188 provide more details about how non-blocking I/O can
    be done using Servlet 3.1.

  • HTTP protocol upgrade mechanism - href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.42">Section
    14.42 in the HTTP 1.1 specification (href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616)
    defines an upgrade mechanism that allows to transition from HTTP
    1.1 to some other, incompatible protocol. The capabilities and
    nature of the application-layer communication after the protocol
    change is entirely dependent upon the new protocol chosen. After
    an upgrade is negotiated between the client and the server, the
    subsequent requests use the new chosen protocol for message
    exchanges. A typical example is how WebSocket protocol is
    upgraded from HTTP as described in href="http://tools.ietf.org/html/rfc6455#section-1.3">Opening
    Handshake section of href="http://tools.ietf.org/html/rfc6455">RFC 6455.

    The decision to upgrade is made in Servlet.service
    method. This is achieved by adding a new method: HttpServletRequest.upgrade
    and two new interfaces: javax.servlet.http.HttpUpgradeHandler
    and javax.servlet.http.WebConnection.

    TyrusHttpUpgradeHandler shows how WebSocket protocol
    upgrade is done in Tyrus
    (Reference Implementation for Java API for WebSocket).

  • Security enhancements
    • Applying run-as security roles to #init and #destroy methods
    • Session
      attack by adding HttpServletRequest.changeSessionId
      and a new interface HttpSessionIdListener. You
      can listen for any session id changes using these methods.
    • Default security semantic for non-specified HTTP method in <security-constraint>
    • Clarifying the semantics if a parameter is specified in the
      URI and payload

  • Miscellaneous
    • ServletResponse.reset clears any data that
      exists in the buffer as well as the status code, headers. In
      addition, Servlet 3.1 will also clears the state of calling getServletOutputStream
      or getWriter.

    • ServletResponse.setCharacterEncoding: Sets the
      character encoding (MIME charset) of the response being sent
      to the client, for example, to UTF-8.
    • Relative protocol URL can be specified in HttpServletResponse.sendRedirect.
      This will allow a URL to be specified without a scheme. That
      means instead of specifying
      "http://anotherhost.com/foo/bar.jsp" as a redirect address,
      "//anotherhost.com/foo/bar.jsp" can be specified. In this case
      the scheme of the corresponding request will be used.
    • Clarification in HttpServletRequest.getPart
      and .getParts without multipart configuration.
    • Clarification that ServletContainerInitializer
      is independent of metadata-complete and is
      instantiated per web application.

A complete replay of href="https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=6793">What's
New in Servlet 3.1: An Overview from JavaOne 2012 can be href="https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=6793">seen
here (click on CON6793_mp4_6793_001 in Media).

Each feature will be added to the JSR subject to EG approval. You
can share your feedback to href="http://java.net/projects/servlet-spec/lists/users/archive">users@servlet-spec.java.net.

Here are some more references for you:
  • href="http://java.net/projects/servlet-spec/downloads/directory/Public%20Review">Servlet
    3.1 Public Review Candidate Downloads

    • href="http://java.net/projects/servlet-spec/downloads/download/Public%20Review/servlet-3.1-pdr-draft.pdf">Servlet
      3.1 PR Candidate Spec
    • href="http://java.net/projects/servlet-spec/downloads/download/Public%20Review/javax.servlet-api-3.1-pdr-draft-javadoc.jar">Servlet
      3.1 PR Candidate Javadocs
  • Servlet
    Specification Project
  • href="http://java.net/projects/servlet-spec/lists/users/archive">JSR
    Expert Group Discussion Archive
  • href="https://wikis.oracle.com/display/GlassFish/PlanForGlassFish4.0#PlanForGlassFish4.0-SpecificationStatus">Java
    EE 7 Specification Status

Several features have already been integrated in href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/">GlassFish
4 Promoted Builds. Have you tried any of them ?

Here are some other Java EE 7 primers published so far:

  • href="https://blogs.oracle.com/arungupta/entry/concurrency_utilities_for_java_ee">Concurrency
    Utilities for Java EE (JSR 236)
  • href="https://blogs.oracle.com/arungupta/entry/collaborative_whiteboard_using_websocket_in">Collaborative
    Whiteboard using WebSocket in GlassFish 4 (TOTD #189)

  • href="https://blogs.oracle.com/arungupta/entry/non_blocking_i_o_using">Non-blocking
    I/O using Servlet 3.1 (TOTD #188)
  • href="https://blogs.oracle.com/arungupta/entry/what_s_new_in_ejb">What's
    New in EJB 3.2 ?
  • href="https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation">JPA
    2.1 Schema Generation (TOTD #187)
  • href="https://blogs.oracle.com/arungupta/entry/websocket_applications_using_java_jsr">WebSocket
    Applications using Java (JSR 356)
  • href="https://blogs.oracle.com/arungupta/entry/jersey_2_in_glassfish_4">Jersey
    2 in GlassFish 4 (TOTD #182)
  • href="https://blogs.oracle.com/arungupta/entry/websockets_and_java_ee_7">WebSocket
    and Java EE 7 (TOTD #181)
  • href="https://blogs.oracle.com/arungupta/entry/json_p_java_api_for">Java
    API for JSON Processing (JSR 353)
  • href="https://blogs.oracle.com/arungupta/entry/jms_2_0_early_draft">JMS
    2.0 Early Draft (JSR 343)

And of course, more on their way! Do you want to see any particular
one first ?

Join the discussion

Comments ( 2 )
  • Binh Thanh Nguyen Tuesday, July 2, 2013

    Thanks for your nice summary about Servlet 3.1

  • guest Sunday, September 15, 2013

    Servlet 3.1 supported in

    Tomcat 8.0

    Glassfish Server 4.0

    Netbeans 7.3.1

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.