In Java EE world, Servlet simply is a web technology with which we can receive and handle the HTTP requests, create HTTP response and return that response back to the client.

Servlet technology makes a basis for other Java web technologies on top of which other frameworks put their extensions. For example JSP, JSF, Spring MVC are such technologies which are uses Servlet technology at behind. 

Servlets run on a web container such as Tomcat which provides the services on Java EE technology stack

What is a servlet ?

Servlet simply makes an object having HTTP-server functionality when its class extends HttpServlet class.
  • A servlet is mapped to a URL to which clients request
  • HTTP requests made to the URL is directed to the corresponding Servlet object
  • Servlet implementation receives the request in the form of GET, POST, PUT or DELETE
  • Servlet implementation runs a business logic and prepares an HTTP response and sends it back to the client just an HTTP server does
  • Lifecycle of the servlet is managed by the web container
In this article, i will not dive into Servlet basics, but mostly the new features come with Java EE 6 and will use this article as umbrella and update it for new coming features of Servlets with upcoming Java EE versions.

1. Servlet 3.0

Servlet 3.0 is specified with JSR 315 under the Java EE 6 specification and added important features to Servlet technology especially annotation-ware usage.

1.1 @WebServlet

@Webservlet annotation has come with Java EE 6. It basically does the mapping of Servlet class with the URLs; instead of doing this job in web.xml file.
@WebServlet(name="Simple Servlet", urlPatterns={"/simpleServlet", "/testServlet"})
public class SimpleServlet extends HttpServlet

1.2 @WebInitParam

@WebInitParam annotation can be used to declare initialization parameters for which we have been using init-param in web.xml, instead.
@WebServlet(name="Simple Servlet", 
          urlPatterns={"/simpleServlet", "/testServlet"},
          initParams={@WebInitParam(name="p1", value="v1"), 
                      @WebInitParam(name="p2", value="v2")})

1.3 @WebFilter

@WebFilter annotation can be used to declare a Servlet Filter.
@WebFilter(servletNames = {"SimpleServlet"})
public class SimpleServletFilter implements Filter

1.4 @WebListener

@WebListener annotation can be used to declare Servlet listener without any decleration in web.xml. Following listeners interfaces are at the scope if implemented:
    - javax.servlet.ServletContextListener
    - javax.servlet.ServletRequestListener
    - javax.servlet.http.HttpSessionIdListener
    - javax.servlet.ServletContextAttributeListener 
    - javax.servlet.ServletRequestAttributeListener
public class SimpleServletContextListener implements ServletContextListener

1.5 Async Servlet

Along with Servlet 3.0, asyncSupported can be used to make long running requests execute on different threads than of threads allocated for Servlets.
@WebServlet(urlPatterns = {"/asynch"}, asyncSupported = true)
public class SimpleAsyncServlet extends HttpServlet

1.6 web.xml

We still need web.xml file for features that are not supported via annotations. Welcome file decleration is one of them. 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
      id="WebApp_ID" version="3.0">

2. Servlet 3.1

Servlet 3.1 is specified with JSR 340 under the Java EE 7 specification and some behavioural changes have been made to the specification and non-blocking io support is added to the specification.

2.1 Non-Blocking I/O

Along with Servlet 3.0, we can execute long-running jobs within seperate threads so we can prevent the Servlet to be blocked with this long-running tasks. However, that is not the only bottleneck; a block can also be happened while reading the request or writing the response and if so, the Servlet could be again blocked. Therefore, the non-blocking io can be used when reading/writing the streams along with the Servlet 3.1.

Sample code for this can be found on my github and the below references section.

2.2 References

3. Servlet 4.0

Along with Java EE 8, Servlet 4.0 includes major updates in servlet specification. Two of the most noteworthy are HTTP/2 protocol support and Server Push which is also the most noteworthy HTTP/2 enhancement.

Major browsers implements HTTP/2 over TLS so https configuration sould be done in your server.
    .addHeader("content-type", "image/png")
    .write("<html><body>" + "<img src='image/javalopment.png'>" + "</body></html>");

You can find my Servlet code samples in my github page:



Java EE Technology Stack

Dependency Injection In Java and CDI