what’s Servelet ?
Servlet is the type of class used to make a dynamic web page using java code

how we can make a servlet
a simple servlet class can be done by implementing the Servlet interface
but usually we use the servlet in an HTTP conection to make an HTTP response to the user in a form of HTML page, in this case it’s better to use HttpServlet abstract class

Servlet life cycle
the servlet life cycle is so simple and we can divide it into three steps

  • Intialization
  • Reply for (service) a request
  • Destroying
  • Intialization
    as the name says, this is to initialize our servlet and it’s needed only once in the first launch of our servlet
  • Reply for (service) a request
    this is the main operation of the servlet, in this step the servlet receive a request, process it and then send a response, this step is called many times.
  • Destroying
    this is the last step in the servlet life cycle, when the servlet is no longer needed and ready for being out of service

a valid servlet should have the functionality of the three steps in the life cycle

Implementing the Servlet interface
to make a servlet using the servlet interface we should implement five methods, three of them is the life cycle methods and the other two is informative methods

package web;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class HelloServlet implements Servlet
{
    @Override
    public void init(ServletConfig config) throws ServletException
    {

    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
	
    }

    @Override
    public void destroy()
    {
	
    }
    
    @Override
    public String getServletInfo()
    {
	
    }
    
    @Override
    public ServletConfig getServletConfig()
    {

    }
    
}

  • init
    public void init(ServletConfig config) throws ServletException
  • service
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
  • destroy
    public void destroy()
  • getServletInfo
    public String getServletInfo()
  • getServletConfig
    public ServletConfig getServletConfig()

before we go to in deep with these methods we should know the parameters this methods dialing with.

  • ServletRequest
    It’s an interface passed when initializing the servlet (through init method)
    it’s basic role is to get Initialization Parameters
  • ServletRequest
    It’s an interface, the server should send an object that implement this interface that collects all needed request information
  • ServletResponse
    It’s an interface, the server sent an object that implements it to the servlet, the developer is intended to fill this object with all the needed response information

going back to the servlet methods

  • init
    public void init(ServletConfig config) throws ServletException
    this method called only once when the first request to the servlet, It’s used only to initialize variables and resources.
    It receives a ServletConfig argument that we can get initialization parameters from.
  • service
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
    It’s called on every request to the servlet.
    the application server would call this method providing you with all the request information in ServletRequest waiting you to provide all the response information in the ServletResponse object
  • destroy
    public void destroy()
    It’s used to free resources
  • getServletInfo
    public String getServletInfo()
    It wants a string return statement, that contains some information about the servlet
  • getServletConfig
    public ServletConfig getServletConfig()
    It wants a return of the type ServletConfig

an initial implementation to the methods should be as followed

  • providing a servlet info string return from getServletInfo method
  • Putting a member from the type ServletConfig, setting it from init method and returning it from getServletConfig method
    ServletConfig config;
    
    @Override
    public void init(ServletConfig config) throws ServletException
    {
	this.config = config;
    }

    @Override
    public String getServletInfo()
    {
	return "Testing Servlet interface";
    }
    
    @Override
    public ServletConfig getServletConfig()
    {
	return config;
    }

The most important method is service method.
It defines the type of the response and the actual data.

the response can be in many types like plain text, XML, HTML or binary data
we can set the content type of a response using setContentType method that takes a String argument represents the content type
the content type can be any of MIME types

to send a text response

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
	response.setContentType("text/plain");
	PrintWriter out =  response.getWriter();
	out.write("this is a plain text response");
}

to send an XML response

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
	response.setContentType("text/xml");
	PrintWriter out =  response.getWriter();
	out.write("");
	out.write("");
	out.write("this is an XML content response");
	out.write("");
	out.write("");
}

to send an HTML response

public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
	response.setContentType("text/html");
	PrintWriter out =  response.getWriter();
	out.write("<html>");
	out.write("<head>");
	out.write("html response");
	out.write("</head>");
	out.write("<body bgcolor='green'>");
	out.write("this is an HTML content response");
	out.write("</body>");
	out.write("</html>");
}