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.
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.
Developer has to import at least one of the two packages,
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.
You can download the above code from here
You can run the above servlet using GET and POST request
more to come.....
Click on the image to order the book from Amazon.com