TheWebGuru.Com Logo

Servlets
What is Setvlet?
A servlet is a Java class that can be loaded dynamically to the server and provide additional functionality to the server. So a request to the server can be handled by a servlet. For example, servlets can be used to retrieve information from a database, process data from an XML file and generate dynamic HTML web pages. Any java-enables web server can offer specilized services using servlet. The Servlet application program interface (API) was designed to extend any type of server.
The servlets can be used for (1)Dynamic Web page generation (2)Information retrieval (3) Retrieving multimedia applications (4) Client/server applications
Java Servlets have many advantages.
(1) Portablility
Since servlets are Java class files it can be developed and deployed across platforms. The Servlet API is tightly integrated into the server. The Servlet API has been designed to be easily extensible to fit the developer's needs.
(2) Scalability
Java is a development environment that comes with a wealth of built-in tools and classes to make development easier and faster. Developers of servlets can use the tools of security and intelligent error handling techniques of Java Programming language.
(3) Efficiency
Servlets are loaded once and then can be accessed many times. The server invokes the servlet using a simple method invocation process, and concurrent requests are handled by separate threads. So the whole system is scalable and perform faster.
The main disadvantage with servlets is the lack of separation of business logic and presentation logic which makes difficult to use in generating Web responses. In order to tune the appearance of the page, one have to edit and recompile the Java servlet, even if the logic is already working.
How Servlets Work?
A model task list for a servlet consists of (1) Receive a request (2) Fulfill the request (3) Respond to the request.
The Web container manages the life cycle of a servlet instance by calling three methods defined in the Servlet interface: init, service, and destroy. Life cycle refers to the path of existence experienced by a servlet from when it is loaded into a servlet engine, initialized and used, to when it is destroyed and garbage-collected.
There are three stages in the life cycle of a servet and they are (1) Create and initialize the servlet by calling init() method (2) Handle zero or more service calls from clients by calling service() method (3) Destroy the servlet and then garbage-collect it by calling destroy() method.
The init() method is called before the first request is handled by the servlet. The destroy() method is called as the servlet is about to be unloaded and used as a way to free resources used by the servlet. The service method is called by the Web container to process a user request.
At any given moment, the Web container has only a single servlet instance for a servlet definition specified in the deployment descriptor. However, over the life span of the Web container the servlet instance may be created and destroyed any number of times. The Web container controls this life cycle process.
More ...
Related Articles: