Make your own free website on Tripod.com
Enterprise Computing - Servlet





  Home
  Servlet
  JDBC
  EJB
  XML
  JSP
  RMI
  JavaIDL
  Protocols
  UML

  Books

  Contact

Servlet


Introduction
A Servlet is a body of java code that is loaded into and runs inside a servlet engine. It receives and responds to requests from clients. Service calls from clients are handled using a request and response paradigm. For example, a client need information from a database, a servlet can be written that receives the request, get and process the data as needed by the client, and then returns it to the client.These kind of programs are called CGI programs.

Advantage
We can use C, C++, perl, php and even COBOL to write CGI programs. But using java to write CGI programs has definite advantages. The CGI program written using other languages are subjected to the security limitation of the operating system, if further security needed these must be coded by the programmer. But servlets run on a servlet engine are subjected to the security restriction imposed by the servlet security manager. Like all java programs, servlets are portables, memory access violations are not possible. The other advantages are, servlet code is not passed to the client, only response is, that products proprietary informations. Servlet can be a client to other servlet. Servlet can be chained, means one servlet call other.

Servlet Basics
Developer has to import at least one of the two packages,

  1. the javax.servlet package (for any type of servlet)
  2. the javax.servlet.http package (for servlet specific to the HTTP protocol)
The life cycle of a servlet,
Servlet is created then initialized.
Respond to Zero or more request from the client.
Servlet is distroyed then garbage collected.

Interface Servlet defines these life cycle methods. Developer need to directly implement this interface only if their servlet cannot inherit from GenericServlet which implements Servlet interface or its decendent HttpServlet. GenericServlet and HttpServlet are abstract classes. Developer must override the service method if they subclassed GenericServlet or one of the doXxx(doGet, doPost, doPut, doDelete, doTrace, doOption) methods. Servlets run inside multi-threaded servers so it is the servlet writer's responsibility to synchronize access to any shared resources.

Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hello extends HttpServlet
{ 
    public void doGet (HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException
        {
            PrintWriter out;

            // defines mime-type of output
            response.setContentType("text/html");

	    String strHelloTo = request.getParameter("Hello");
            out = response.getWriter();
            
 	    // HTML output
	    out.println("<HTML>\n");
	    out.println("<HEAD><TITLE>Hello " + strHelloTo + "</TITLE></HEAD>\n");
            out.println("<BODY>\n");
	    out.println("<CENTER>Hello " + strHelloTo + "</CENTER>\n");
	    out.println("<CENTER>- response for the GET request</CENTER>\n");
       	    out.println("</BODY>\n");
	    out.println("</HTML>\n");
            out.close();
        }
    public void doPost (HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException
        {
            PrintWriter out;

            // defines mime-type of output
            response.setContentType("text/html");

	    String strHelloTo = request.getParameter("Hello");
            out = response.getWriter();
            
 	    // HTML output
	    	    out.println("<HTML>\n");
	    out.println("<HEAD><TITLE>Hello " + strHelloTo + "</TITLE></HEAD>\n");
            out.println("<BODY>\n");
	    out.println("<CENTER>Hello " + strHelloTo + "</CENTER>\n");
	    out.println("<CENTER>- response for the POST request</CENTER>\n");
       	    out.println("</BODY>\n");
	    out.println("</HTML>\n");
            out.close();
        }
}

You can download the above code from here
You can run the above servlet using GET and POST request

GET request
Click here
or

POST request

more to come.....


Click on the image to order the book from Amazon.com

Java Enterprise in a Nutshell Flanagan, et. al.
cover

Search books!

Search Now:
In Association with Amazon.com