Servlet

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
@WebListener
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="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      id="WebApp_ID" version="3.0">
   <display-name>servlet3</display-name>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>


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

https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/cwlp_servlet31_behavior.html


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.
request.newPushBuilder()
    .path("image/javalopment.png")
    .addHeader("content-type", "image/png")
    .push();
response.getWriter()
    .write("<html><body>" + "<img src='image/javalopment.png'>" + "</body></html>");


References

https://www.oracle.com/a/ocom/docs/corporate/java-magazine-nov-dec-2017.pdf

Yorumlar

Popular

Dependency Injection In Java and CDI

Spring Components & Auto Scanning & Configuration