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
- At the request of a servlet, the server checks if it is the first time and if it is so;
- The web container loads the servlet class
- It instantiates the servlet class
- Passes the ServletConfig object at the call of the init() method
- And if otherwise, it calls the service() method and passes the request and response objects.
- The web container maps the request to the servlet in the web.xml file.
- It creates the object of the request and response on request.
- It invokes the service() method on the thread
- The protected service() method invokes the doGet method which depends on the type of request.
- The response is generated by the doGet method and sent to the client.
- The web container deletes the request and response objects after passing the response.
- Depending on the server implementation, the thread is contained in the deleted pool or thread pool.
Container
- It is used in java for dynamically generating the web pages on the server-side
- Loads servlet class
- It creates an instance of the servlet class.
- It invokes the init() method and passes the ServletConfig object.
- It invokes the destroy() method when removing the servlet.
Web Terminologies
- Web Application: This is an application that is accessible from the web and it may be composed of Servlet, JSP, Filter, or HTML, usually executed in the server and responds to HTTP requests.
- Website: It is a collection of related web pages that may contain text, images, audio, and video.
- HTTP: It is the data communication protocol used to establish communication between client and server.
- HTTP Requests: It is the request sent by the computer to a web server that contains all sorts of potentially interesting information.
- Get and Post: These are the HTTP methods in servlets, the Get is for retrieving information from the server and the Post method is used when data is to be submitted or updated on the server.
- Server: It is used to manage the network resources and for running the program or software that provides service
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
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.
- A servlet is new whenever a servlet instance is created.
- A servlet comes into a ready state after the init() method has been invoked and it performs its task.
- Then it enters the end-state whenever the destroyed() method is invoked by the web container.
The web container oversees the servlet life cycle as follows:
Load the Servlet class:
- The class loader loads the servlet class when the web container receives the request
- Create the Servlet instance:
- After loading the Servlet class, the web container creates the instance of the servlet, and it is once in its life cycle.
- Invoke the init() method:
- The init() method is called once by the web container after the creation of the Servlet instance
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.