JAVA SERVLETS!
Servlets are server-side Java programs that are loaded and executed by an Application Server like Apache Tomcat. A servlet can be considered as server-side applet that accepts requests from a client (via the Web Server), performs some task, and returns the result. The following figure depicts the role of a Servlet in a web application:
The following is a list of interactions that take place between a web client and a Servlet (server):
- The client (Web Browser) makes a request via HTTP
- The Web Server (or Application Server) receives the request and forwards it to the Servlet. If the Servlet has not yet been loaded, the Web Server will load it on the Java Virtual Machine and execute it.
- The Servlet that receives the HTTP request will process the request by running the script provided in the servlet
- The Servlet will return a response back to the Web Server in the form of a web page (html file).
- The Web Server will forward the response to the client.
Servlet technology is used to create dynamic web pages by writing programs in Java that are executed on a server. Servlets are designed to handle HTTP requests - GET, and POST. They can run on any Java-enabled platform having an application server.
What are the advantages of Servlets over other server-side technologies used for dynamic web pages?
The advantages of Servlets over Common Gateway Interface (CGI), a standard defined for running application code on the server, are as follows:
- Servlets run faster than CGI.
- Servlets are platform independent as they are written in Java, which is platform independent. Servlets can run on any Servlet enabled web server.
- Servlets make use of a standard vendor independent API supported by many web servers. Moreover, the API used by Servlets posses the features of Java, such as platform independence and portability.
- Using Servlets removes the overhead of creating a new process for each client, as Servlet does not run a separate process for each request made by a client. There will be only one instance of a Servlet that can handle all requests made by the clients concurrently.
Servlet Container:
Servlet container, also known as Servlet engine, is a component of Server software like Tomcat Apache that provides the runtime environment for Servlets. The services provided by the Servlet container are as follows:
- Network services: The Servlet container is responsible for loading a Servlet into memory. The loading may be done from a local file system, a remote file system, or other network services. Moreover, the Servlet container provides network services over which the request and response between the Servlet and its clients are carried out.
- Decode and Encode MIME based messages: The Servlet container provides the service of decoding and encoding MIME based messages.
- Manage Servlet life-cycle: The Servlet container manages the lifecycle of a Servlet
- Resource Management: The Servlet container manages the files required for generating static and dynamic web pages, such as HTML files, Servlets, and JSP pages.
- Security service: The Servlet container handles authorization and authentication of resources available on the server.
- Session Management: The Servlet container maintains a session by appending a session ID to the URL path.
Servlet container makes use of HTTP as the protocol for receiving request and sending response to its clients; however, additional protocols, such as HTTPS (HTTP over SSL) may be supported based on the requirement.
The Servlet API:
Servlet API refers to the set of packages provided by Sun Microsystems for the development of Servlet programs. In addition to these packages, we need an Application Server such as Apache Tomcat or Internet Information Server (IIS) that act as a container or runner for the Servlet.
All web servers that support Servlet must have Servlet API for the creation and running of Servlets. Servlet API consists of the following two packages:
- javax.servlet &
- javax.servlet.http
These packages contain classes and interfaces that can be used by a Servlet for its development. The javax.servlet package contains classes and interfaces to support generic, protocol-independent Servlet. These classes are extended by the classes in javax.servlet.http package to add HTTP-specific functionality in them.
A Servlet, in its most general form, is an instance of a class which implements the
javax.servlet.Servlet
interface. Most Servlets, however, extend one of the standard implementations of that interface - javax.servlet.GenericServlet
or javax.servlet.http.HttpServlet
. The various stages of a Servlet created using
javax.servlet.http.HttpServlet
class is also called servlet life cycle, which includes three stages namely initialization (loading), running (servicing) and unloading (terminating).Servlet Life Cycle:
In order to initialize a Servlet, the following steps are followed by the servlet container:
- Servlet container loads the Servlet class (and probably other classes which are referenced by the Servlet) and creates an instance of it by calling the no-args constructor.
- Servlet container calls the Servlet's
init(ServletConfig config)
method, which contains the code for performing one-time setup of Servlet. - Servlet container also creates the ServletConfig object so that the configuration details can be retrieved later by calling the Servlet's
getServletConfig()
method.
Servlets which extend
GenericServlet
(or its subclass HttpServlet
) should call super.init(config)
at the beginning of the init
method to make use of configuration object. The ServletConfig
object contains Servlet parameters and a reference to the Servlet's ServletContext
. The init
method is guaranteed to be called only once during the Servlet's lifecycle. Hence, it does not need to be thread-safe because the service
method will not be called until the call to init
returns. Once the Servlet is initialized, its
service(ServletRequest req, ServletResponse res)
method will be called for every request made by the client to the Servlet. This method may be called concurrently (i.e. multiple threads may call this method at the same time) so it should be implemented in a thread-safe manner. When the Servlet needs to be unloaded (e.g. because a new version should be loaded or the server is shutting down) the
destroy()
method is called. There may still be threads that execute the service
method when destroy
is called, so destroy
has to be thread-safe. All resources which were allocated in init
should be released in destroy
. This method like init is guaranteed to be called only once during the Servlet's lifecycle.