Saturday, March 7, 2020

Java Servlets

Java Servlets

The Servlets enhance the capabilities of servers offering applications based upon the request-response model. In spite of the fact that servlets can respond to any type of request, they are more often used to create the applications facilitated by web servers. For such applications, Java Servlets characterize HTTP protocol based servlet classes.

HTTP (Hypertext Transfer Protocol)

  • HTTP is a protocol that enables clients and servers on the web to communicate.
  • This is an application layer protocol like FTP, SMTP.
  • HTTP is a stateless protocol or HTTP supports only one request per connection. This means that with HTTP protocol the client connects to the server to send only one request and then disconnect. This mechanism allows more users to connect to a given server over a period of time.
  • The client sends an HTTP request and the server answers with an HTML page to the client, using HTTP.
  • The HTTP request can be made using a variety of methods, but the ones which we use widely are Get and Post.

The Get and Post

1.  Data is sent in the header body 1.  Data is sent in the request body
2.  Restricted to the limited data transfer 2.  Supports a large amount of data transfer
3.  It is not secured 3.  It is completely secured
4.  It can be bookmarked 4. It cannot be bookmarked

The Servlet Container

The Servlet Container is a system that manages Java Servlet components on top of the Web server to handle the Web client requests.

The Services Provided by Servlet Container

  • Network Services
  • Decode and Encode MIME-based messages
  • Manage Servlet container: Manages the lifecycle of a Servlet
  • Resource management: Manages the static and dynamic resource, such as HTML files, Servlets and JSP pages
  • Security Service Session Management

The Servlet Interface

Servlet interface

All of the interfaces and classes required for creating and deploying servlets are placed in the javax.servlet and the javax.servlet.http packages. The JSPs and Servlets belong to the Java Enterprise Edition.

The Servlet interface is placed above all the classes and interfaces in the class hierarchy. This interface defines the lifecycle methods of the Servlets. All the classes implementing this interface must describe these methods.

The GenericServlet class is placed just below the Servlet interface and implements generic services. GenericServlet class is extended by the HttpServlet class and implements HTTP protocol-specific services.

The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services. All of the user-defined Servlet classes can be created by extending the HttpServlet class.

There are 5 methods in the Servlet interface. The init(), service() and destroy() are the life-cycle methods of the Servlet. These are invoked by the web container itself. The other two methods are getServletConfig() and getServletInfo().

The Request-Response model

The service method implements the request-response model. When the request is mapped to a Servlet available in the Servlet container. The container first instantiates the object of the servlet if it is not available in the container else loads the existing instance. Then the service(ServletRequest, ServletResponse) method is called by the container to respond to the request.

A user can send a request to the server at some remote location by some client program or the web browser. This request is received by the web server and further send to the Servlet container for the processing. The Servlet Container maps this request with the appropriate servlet in the container and invokes the init() if required to create the instance of the Servlet. Eventually, it will invoke the service(ServletRequest, ServletResponse) method to postulate the results.  The results are sent back to the user in the response object.

request-response model