SERVLETS IN JAVA

Servlet is a Java software component that extends the capabilities of a server. These are the Java programs that runs on the Java-enabled web server or application server. They are used to handle the request obtained from the web server, process the request, produce the response, then send response back to the web server. This technology is similar to other Web server extensions such as Common Gateway Interface(CGI) scripts. However, Java Servlets are more acceptable since they solve the limitations of CGI such as low performance and low degree scalability.

 

Life Cycle of Servlet

Step 1: Loading of Servlet
When the web server starts up, the servlet container deploy and loads all the servlets onto the web server.

Step 2: Creating instance of Servlet
Once all the Servlet classes loaded, the servlet container creates instances of each servlet class. Servlet container creates only once instance for a servlet class and all the requests to the servlet are executed on the same servlet instance.

Step 3: Invoke init() method
Once all the servlet classes are instantiated, the init() method is invoked for each instantiated servlet. This method initializes the servlet. init() method isinvoked only once in a life cycle of a servlet.

Step 4: Invoke service() method
Each time the web server receives a request for servlet, it spawns a new thread that calls service() method. If the servlet is GenericServlet then the request is served by the service() method itself, if the servlet is HttpServlet then service() method receives the request and dispatches it to the correct handler method based on the type of request such as doGet(), doPost(), doOptions(), doDelete(), doHead(), doPut() or doTrace(). The service() method can be called any number of times during servlet life cycle.

Step 5: Invoke destroy() method
When servlet container shuts down, it unloads all the servlets and calls destroy() method for each initialized servlets. destroy() method is also invoked only once during the life cycle of a servlet.

Servlets API’s:
Servlets are build from two packages:

  • javax.servlet(Basic)
  • javax.servlet.http(Advance)

 

Various classes and interfaces present in these packages are:

 

Servlet

Interface

javax.servlet.*

ServletRequest

Interface

javax.servlet.*

ServletResponse

Interface

javax.servlet.*

GenericServlet

Class

javax.servlet.*

HttpServlet

Class

javax.servlet.http.*

HttpServletRequest

Interface

javax.servlet.http.*

HttpServletResponse

Interface

javax.servlet.http.*

Filter

Interface

javax.servlet.*

ServletConfig

Interface

javax.servlet.*

 

Please follow and like us:
error20

Your email address will not be published. Required fields are marked *

Contact CPD Technologies