Servlets in Java

Img Src:Wikimedia commons

Servlet is a Java Server Side technology which handles the user request and provides the response to the user. It can also be called a controller.

It is basically a class which interacts with the web server. User request is fetched by the web server and then the web server fwds the request to the servlet.

User while sending the request through web browser to the server mentions the details of the servlet in the url which helps the web server to redirect it to the correct servlet for porcessing the request.

Servlet can by itself send the response or fwd it to a jsp or some other servlet via request dispatcher for further processing.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets. For a class to act like a servlet, that class must implement the java.servlet.Servlet interface.

Why it should implement that interface?

Because the interface has methods which describes the life cycle of a servlet.

Servlet Life Cycle in Java:

1)init() : This method is triggered only once in the servlet life cycle and it is used to initialize the servlet. It is triggered by the web container for example Tomcat. Untill it is initialized, it cannot handle client requests. It can initialize db conns and other resources as well when init is triggered. Logic for those initializations would be written in the init method itself.

2)getServletConfig(): This method returns the configuration information for the servlet.

3)service(): This method is the one which handles the user request in the form of a request object, processes the request and sends the response back to the user. The servlet creates separate thread for each request. So it is an multi threaded architecture and we should maintain data consistency and keep resources thread safe. So important to use synchromization correctly.

4)getServletInfo(): It returns basic servlet info like author, copyright info etc.

5)destroy(): This method is called only once the servlet life cycle. Probably when the web container shuts down. It closes all db conns or resources if any before getting destroyed.

Let’s take a look into the implementation of the servlet using Servlet interface

import java.io.*;  
import javax.servlet.*;  
  
public class ServletExample implements Servlet{  
ServletConfig config=null;  

@Override
public void init(ServletConfig config){  
   this.config=config;  
   System.out.println("Servlet has been Initialized");  
}  

@Override  
public void service(ServletRequest req,ServletResponse res)  
throws IOException,ServletException{  
  
   res.setContentType("text/html");  
  
   PrintWriter out=res.getWriter();  
   out.print("<html><body>");  
   out.print("<b>Hello World</b>");  
   out.print("</body></html>");  
  
}  

@Override
public void destroy(){
   System.out.println("Servlet Has Been Destroyed");
}

@Override  
public ServletConfig getServletConfig(){
   return config;
}

@Override  
public String getServletInfo(){
    return "copyright information....";
}  
  
}

In the next article we will see how a servlet is configured in the web.xml which is present in the WEB-INF folder. Configuration of the servlet class is required in the xml file so that it can accept the user requests VIA URL. We will discuss in detail.

Also we will see the use of HttpServlet class while creating a servlet. Servlet can also be created by extending the HttpServlet class instead of implementing the Servlet interface.