Open In App

Servlet – Web Terminology

Servlets are the Java programs that run on the Java-enabled web server or application server. They are used to handle the request obtained from the webserver, process the request, produce the response, then send a response back to the webserver. 

How Servlet Works

Container

Web Terminologies

Servlet API

Servlet API are used for creating servlets. There are 2 packages , the Javax.servlet.http packages which contains classes that support http servlet while the javax.servlet package contains the classes that support generic servlet(protocol- independent servlet).






java.lang.Object
    |_extended byjavax.servlet.GenericServlet
            |_extended byjavax.servlet.http.HttpServlet

Generic Servlet

The Generic servlet class is created by extending 

javax.servlet.GenericServlet

GenericServlet is an abstract class that has a service method.



service()

 This implies that the subtype or sub-class should override the service() method in the supertype. 

public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

The Method takes 2 arguments

ServletRequest

 and

ServletResponse 

The request object takes the client’s request while the response object return response back to the client call.

HTTP Servlet

The Http servlet class is created by extending 

Javax.servlet.http.HttpServlet class.

HTTP servlet is an abstract class that extends the GenericServlet class, it provides methods like doPost, doGet, doDelete, doTrace, and so on.

Examples of methods in HTTP Servlet Class:

protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException  
{ // invoked by the web container and it handles the Get request from client call }

protected long getLastModified(HttpServletRequest req)
{ // This method returns the last time Httpservlet was modified }

protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{ // Invoke by the web container to handle the HEAD request }

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{ // invoked by the web and it handles the POST request }

protected void doDelete(HttpServletRequest req, HttpServletResponse res)
{ // Invoked by the web and it handles the DELETE request }

protected void doTrace(HttpServletRequest req, HttpServletResponse res)  
{ // Handles the TRACE request }

protected void doOptions(HttpServletRequest req, HttpServletResponse res)
{ // Handles the OPTIONS request } 

Servlet Life Cycle

There are states in servlet:

 New, ready, and end. 

The web container oversees the servlet life cycle as follows:

Load the Servlet class:

public void init(ServletConfig config) throws ServletException

Web container Invokes the service() method:

The service() method is invoked each time the web container calls it, this happens after when the request is made to the servlet.

Web container Invoke the destroy() method:

The destroy() method is called before removing the servlet instance from the service, this allows the servlet to clean up resources like threads.

Public void destroy()

WAR file

WAR (web archive) File is where files of a web project are and it may contain files like servlet, jsp, html, css, js, jsp, etc.

Conclusion

The web containers help to create multiple threads for handling many requests to the servlet which leads to better performances in place of process. It is Robust as it is managed by JVM, this prevents the problem of memory leaks and garbage collection. It is secure because it is written in Java.


Article Tags :