Introduction to Servlet

Posted in : Servlet Posted on : October 29, 2010 at 4:49 PM Comments : [ 0 ]
In this tutorial you will introduce to a web technology servlet and also know how it works

Introduction to Servlet

A Servlet is a server side program written in java which handles client request coming from web browser through a network. It is basically java classes which implements javax.servlet.Servlet*; interface. It is the convenient means of generating dynamic web page. The execution of servlet is started by the servlet container, the servlet container is responsible of initializing, executing, and destroying the servlet. A servlet class doesn't have a main method, therefore it can not execute itself directly. Servlets are protocol specific, for example for handling HTTP request you need to extend HTTPServlet class of javax.servlet.http*; package.

request/response between client and servlet

When the container started it initializes the servlet, and keep into the memory, when request comes, the container forwarded it to the servlet object. The servlet can only once initialized and destroyed within its life cycle but it can handle many requests. The response generated by the servlet is commonly in the form of HTML which run on the browser. The servlet basically contains two methods service(). This method takes reference of two interfaces ServletRequest and ServletResponse. Servletrequest for getting request from the client and ServletResponse for generating appropriate response to the client. The servlet contains the HTML code inside it println("<HTML>") tag. On running the servlet this HTML appears on the web browser.

You need to set the content type of the response or MIME type, so that the servlet can response in that type. HTML/Text is the common response type  widely used. You can set it as


Here is a Simple Servlet Code
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
        response.setContentType("text/html");// Setting Response Type
        PrintWriter out = response.getWriter(); //It write data to the buffer
Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Tutorial Topics