Chapter Six Servlets: Debre Markos University Department of Computer Science
Chapter Six Servlets: Debre Markos University Department of Computer Science
Chapter Six Servlets: Debre Markos University Department of Computer Science
Chapter Six
Servlets
Advanced Programming(CoSc2084)
03/18/2021
Introduction
* Today we all are aware of the need of creating dynamic web pages i.e
the ones which have the capability to change the site contents
according to the time or are able to generate the contents according to
the request received by the client.
* If you like coding in Java, then you will be happy to know that using
Java there also exists a way to generate dynamic web pages and that
way is Java Servlet.
* But before we move forward with our topic let’s first understand the
need for server-side extensions.
* Servlets are the Java programs that runs on the Java-enabled web server
or application server.
* They are used to handle the request obtained from the web server,
process the request, produce the response, then send response back to
the web server
* Servlets work on the server-side.
* Servlets are capable of handling complex requests obtained from web
server
……..
Execution of Servlets :
Execution of Servlets involves six basic steps:
* Java servlets have been created and compiled just like any
other Java class.
* After you install the servlet packages and add them to your
computer's classpath, you can compile servlets with the
JDK's Java compiler or any other current compiler.
Servlets Architecture
* So, in CGI server has to create and destroy the process for
every request.
* The init() method simply creates or loads some data that will
be used throughout the life of the servlet.
ServletResponse response)
throwsServletException,IOException{
// servicing code
}
……..
* The service () method is called by the container and service
method invokes doGet, doPost, doPut, doDelete, etc.
methods as appropriate.
* There are two common ways of passing data from the web
browser to the web server.
HttpServletResponse response)
throwsServletException,IOException{
// Servlet code
}
The doPost() Method
HttpServletResponse response)
throwsServletException,IOException{
// Servlet code
}
The destroy() method
* The destroy() method is called only once at the end of the life cycle
of a servlet.
* After the destroy() method is called, the servlet object is marked for
garbage collection.
// Finalization code...
}
……..
* Servlets are Java classes which service HTTP requests and
implement the javax.servlet.Servlet interface.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
{
……..
// Do required initialization
HttpServletResponse response)
throwsServletException,IOException
response.setContentType("text/html");
}
……..
public void destroy()
// do nothing.
// Finalization code...
}
OU
K Y
A N
TH
RY
VE
! !
C H
MU
03/18/2021