Tuesday 4 June 2013

SERVLETS

Servlets are small programs that are executed on the server side and they dynamically extend the functionality of the web server. Servlets can run on any environment, as they are platform independent. So servlets are basically added in order to have dynamic contents on our websites. As we know that the html provides us with the content that is just static in nature, if we wish to add dynamic content than servlets are the best choice.

The general working of a servlet is described in the following figure:-

Now after talking on what servlets are, let’s move on to another important topic that what is the meaning of life cycle of a servlet.

LIFE CYCLE OF A SERVLET
There are basically three methods that are concerned with the life cycle of a servlet.
  • init()
  • service()
  • destroy()


init() method is used in the servlets so that it is possible for the server to pass initialization parameter to the servlet, and then the servlet may configure itself.

The service() method is used to process the http request. Another important thing to note here is that the service() method gets executed for every client request. This means that each time the client requests something then the service method will be invoked in response to that request. It is the service() method that contains the code for processing client request and giving back a response to the client.

After a successful response to all the requests, the server may now decide to unload the servlet from its memory. For this thing to be done, destroy() method is invoked to relinquish all the resources that are allocated for the servlet.





For working on servlets in java, we need to include the following two important packages:-
  • javax.servlet.*;
  • javax.servlet.http.*;


The second servlet is specifically used, for the cases in which we are working on the http protocol. The above mentioned packages are actually servlet API’s (Application programming interface), that are essentially required to build a servlet.

Now after covering some of the fundamental concepts related to servlet, let’s move on to the first program that implements servlets.
Source Code:-
/*******************************************************************/
package h_world;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/hello_world")
public class hello_world extends GenericServlet {
       private static final long serialVersionUID = 1L;
      
       public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
       {
              response.setContentType("text/html");
              PrintWriter out = response.getWriter();
              out.println("<b/>Hello World!!!");
              out.close();
       }
}
/*******************************************************************/

Now you would think that what makes a simple class in Java, a servlet class?
The answer to this question is very simple. Any class that extends GenericServlet class becomes a servlet class. The GenericServlet class is defined within the package javax.servlet, implements the Servlet and ServletConfig interface. And in the ServletConfig interface, we have the above mentioned three important methods  inti(), service() and destroy().

Now consider the following code. This code is also a very basic one, that show the implementation of servlets by extending the HttpServlet class.

Source code:-
/*********************************************************************/
package h_world;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello_Worldhttp")

public class Hello_Worldhttp extends HttpServlet {

       private static final long serialVersionUID = 1L;

       protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
       {
              PrintWriter out=response.getWriter();
              out.println("<b/>Hello World!!!");
              out.close();        
       }
}
/***********************************************************************/

Now an important thing to note here is that we can derive our servlet class from either class or from the HttpServlet class. Here the GenericServlet class is not specific to any protocol while the HttpServelet class is specific to the http protocol. And as we know that over internet we use the http protocol, so we are here provided with a http protocol specific class, called the HttpServlet class. This HttpServlet class is basically derived from the GenericServlet class, and added those features that are unique to the http protocol.

Another important thing to note here is that we can either of the methods doGet and doPost, but by default the doGet method is executed. Consider the following code in which we have both the doGet and doPost methods, but on running the servlet on server we just get the output of the code that is within the doGet method.

Source Code:-
/***********************************************************************/
package h_world;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello_Worldhttp")

public class Hello_Worldhttp extends HttpServlet {

       private static final long serialVersionUID = 1L;

       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
              PrintWriter out=response.getWriter();
              out.println("<b/>Hello World!!!");
              out.println("<br/>You are in the doGet() mehtod");
              out.close();
             
       }
      
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
       {
              PrintWriter out=response.getWriter();
              out.println("<b/>Hello World!!!");
              out.println("<br/>You are in the doPost() mehtod");
              out.close();
             
       }

}
/*************************************************************************/
Output:-
Hello World!!!
You are in the doGet() method

So by the above example it is clear that by default the doGet() is invoked. 

The last thing that I would like to share with you is how to embed the html code in the our servlet.
You can simply embed the html code within your servlet, and start using all the html tags, without any extra efforts.

Consider the following source code:-
/****************************************************************************/
package h_world;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Hello_Worldhttp")

public class Hello_Worldhttp extends HttpServlet {

       private static final long serialVersionUID = 1L;

       protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
       {
              PrintWriter out=response.getWriter();
             
              out.println("<html>");
              out.println("<head>");
              out.println("<title>Hello World</title>");
              out.println("</head>");
              out.println("<body>");
              out.println("Hello World!!!");
              out.println("<br/>Using html");
              out.println("</body>");
              out.println("</html>");
             
              out.close();
             
       }
      
}
/****************************************************************************/

No comments:

Post a Comment