Java Servlets
Java Servlets
Java Servlets
Servlets are the Java programs that run on the Java-enabled web server or
application server. They are used to handle the request obtained from the
webserver, process the request, produce the response, then send a response back
to the webserver. In Java, to create web applications we use Servlets. To create
Java Servlets, we need to use Servlet API which contains all the necessary
interfaces and classes.
• javax.servlet.http
• javax.servlet
Servlets Architecture
Servlets are grouped under the Advanced Java tree that is used to
create dynamic web applications. Servlets are robust, well scalable, and
are primarily used in developing server-side applications. If we go a
little back in time, we would be able to witness that before the
introduction of servlets, CGI (Common Gateway Interface) was
used. Among several indigenous tasks that a servlet is capable of
doing, dynamically performing client requests and responses
are most common.
Advantages of a Java Servlet
Servlet is faster than CGI as it doesn’t involve the creation of a new process for
every new request received.
Servlets, as written in Java, are platform-independent.
Removes the overhead of creating a new process for each request as Servlet
doesn’t run in a separate process. There is only a single instance that handles all
requests concurrently. This also saves the memory and allows a Servlet to easily
manage the client state.
It is a server-side component, so Servlet inherits the security provided by the Web
server.
The API designed for Java Servlet automatically acquires the advantages of the
Java platforms such as platform-independent and portability. In addition, it
obviously can use the wide range of APIs created on Java platforms such as
JDBC to access the database.
Many Web servers that are suitable for personal use or low-traffic websites are
offered for free or at extremely cheap costs eg. Java servlet. However, the
majority of commercial-grade Web servers are rather expensive, with the notable
exception of Apache, which is free.
5. Servlet : This is the main interface that defines the methods in which all the
servlets must implement. To implement this interface, write a generic servlet
that extends javax.servlet.GenericServlet or an HTTP servlet that extends
javax.servlet.http.HttpServlet.
javax.servlet.http
This package provides the number of interfaces and classes to support HTTP
servlet which is HTTP protocol dependent.
These interfaces and classes describe and define the contracts between a servlet
class running under HTTP protocol and the runtime environment provided by a
servlet container.
You may create a generic servlet by inheriting the GenericServlet class and providing
the implementation of the service method.
3.public void destroy(): Is invoked only once throughout the life cycle and indicates
that servlet is being destroyed.
6.public void init():it is a convenient method for the servlet programmers, now there
is no need to call super.init(config)
8.public String getInitParameter(String name): returns the parameter value for the
given parameter name.
11.public void log(String msg): writes the given message in the servlet log file.
import java.io.*;
import javax.servlet.*;
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</body></html>");
}
}
HttpServlet Class
The HttpServlet class extends the GenericServlet class and implements Serializable
interface. It provides http specific methods such as doGet, doPost, doHead, doTrace
etc.
package com.gfg;
import javax.servlet.*;
import javax.servlet.http.*;
HttpServletResponse rs)
// abstract class
HttpServletResponse rs)
} // class ends
Life Cycle of a Servlet
The web container maintains the life cycle of a servlet instance. Let's see the life cycle
of the servlet:
As displayed in the above diagram, there are three states of a servlet: new, ready and
end. The servlet is in new state if servlet instance is created. After invoking the init()
method, Servlet comes in the ready state. In the ready state, servlet performs all the
tasks. When the web container invokes the destroy() method, it shifts to the end state.
1) Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet class is loaded
when the first request for the servlet is received by the web container.
The web container creates the instance of a servlet after loading the servlet class. The
servlet instance is created only once in the servlet life cycle.
The web container calls the init method only once after creating the servlet instance.
The init method is used to initialize the servlet. It is the life cycle method of the
javax.servlet.Servlet interface. Syntax of the init method is given below:
The web container calls the service method each time when request for the servlet is
received. If servlet is not initialized, it follows the first three steps as described above
then calls the service method. If servlet is initialized, it calls the service method.
Notice that servlet is initialized only once. The syntax of the service method of the
Servlet interface is given below:
The web container calls the destroy method before removing the servlet instance from
the service. It gives the servlet an opportunity to clean up any resource for example
memory, thread etc. The syntax of the destroy method of the Servlet interface is given
below: