Java Servlets are well known for their popularity in building dynamic content for web-based applications. Java Servlet technology gives Web developers a simple and consistent approach for extending the functionalities of a Web server and therefore accessing business systems . It overrides the disadvantages of CGI( Common Gateway Interface)systems in many things. Servlet 3.0 released with JEE 6.0 has many new features including.
  • Ease of development
  • Pluggability and extensibility
  • Asynchronous supports
  • Security enhancements
  • Other changes
  • The advantages of Servlet lies in their ability to process HTTP requests and send back the response to clients which enables them to implement business logic in small applications
    Ease of Development with Annotations
    In earlier versions the Servlets and Servlet filters etc need to be mentioned in deployment descriptor( web.xml file). In Servlet 3.0, it is also possible to specify these information in the definition of a component itself, through the Annotations. However information in the web.xml has precedence the annotations.
    The following annotations are applicable starting from Servlet 3.0 specification,
  • @WebServlet
  • @WebServletContextListener
  • @ServletFilter
  • @InitParam
  • e.g
    //for normal servlet
    @WebServlet(
    name = “MyServlet”,
    urlPatterns = {“/myservlet”},
    initParams = {
    @InitParam(name = “p1”, value = “v1”),
    @InitParam(name = “p12”, value = “v12”)}
    )
    //for filters

    @ServletFilter(
    filterName = “MyFilter”,
    urlPatterns = “/url”,
    initParams =
    {@InitParam(name = “p”, value = “v1”),
    @InitParam(name = “p2”, value = “v2”)}
    )
    //for listeners
    @WebServletContextListener()

    public class MyServlet {

    }

    The element metadata-complete , one of the child element of web-app element ( root of web.xml) will determine the processing of these annotations . There are two possible values for this component
  • True – meta information available in web.xml is complete ,so no need to process annotations.
  • False – meta information available not complete and components with annotations, should be processed
  • Pluggability
    If your application uses different frame work they are often configured using Servlets. This require the editing of web.xml. The problem with this approach is that it is not modular and will make the deployment descriptor more complex. Servlet 3.0 address this issue by introducing another child components for web-app, known as
    <web-fragment >
    It allows logical partitioning of a different frame works into elements like servlet-mapping, servlet, servlet-filter, filter-mapping, etc.
    Servlet 3.0 also support pluggability by allowing Programmatic support for adding web components . It allows programmatic addition of servlets and filter classes .
    Asynchronous Support
    In slow applications like a database connection ,the Servlet has to wait for the response from the server to process the next request,causing delay in the entire system. In order to avoid this problem Servlet 3.0 introduces a new concept called asynchronous request processing. The request can be made to put into asynchronous mode and also possible to specify the timeout duration .It is also possible to add asynchronous listeners to the request ,so to receive notifications when the operation is completed or the timeout expired.
    @WebServlet(
    asyncSupported = true,
    asyncTimeout = 3000
    )
    public class MyServlet {

    }

    Security
    Servlet 3.0 added new APIs to HTTPServletRequest to make it more safer. The login method in HTTPServletRequest will allow the application to force a authentication. The logout and HTTPSession methods allows the application to reset the authentication of the request.
    Other Changes
    HttpOnly Cookies:These cookies are not exposed to client-side scripting code, so it can prevent certain cross-site scripting attacks.
    API Changesl: New methods are added in Servlets 3.0 to get response and context elements
    ServletResponse getServletResponse()
    ServletContext getServletContext()