Unit II SERVLET
Unit II SERVLET
SERVERSIDE SCRIPTING
Servlet
Servlet technology is used to create web application
Before servlet, CGI (Common Gateway Interface) scripting language
was popular as a server-side programming language.
There are many interfaces and classes in the servlet API such as
Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse
etc.
Servlet is an interface that must be implemented for creating any
servlet.
Servlet is a class that extend the capabilities of the servers and
respond to the incoming request. It can respond to any type of
requests.
Servlet is a web component that is deployed on the server to create
dynamic web page.
Web Application
1. Better Performance: because it creates a thread for each request not process.
3. Robust: Servlets are managed by JVM so no need to worry about memory leak, garbage
collection etc.
This is the process of a servlet execution takes place when client (browser)
makes a request to the webserver.
Servlet architecture includes:
a) Servlet Interface
To write a servlet we need to implement Servlet interface. Servlet interface can be implemented directly
or indirectly by extending GenericServlet or HttpServlet class.
The first time a servlet is invoked, the init method is called. It is called only once during the lifetime of
a servlet.
The Service method is used for handling the client request. As the client request reaches to the
container it creates a thread of the servlet object, and request and response object are also created.
These request and response object are then passed as parameter to the service method, which then
process the client request. The service method in turn calls the doGet or doPost methods (if the user
has extended the class from HttpServlet).
c) Number of instances
Basic Structure of a Servlet
Public class firstServlet extends HttpServlet
{
publicvoidinit()
{
/* Put your initialization code in this method,
* as this method is called only once */
}
publicvoid service()
{ // Service request for Servlet }
publicvoid destroy()
{
// For taking the servlet out of service, this method is
called only once }}