[E21] introduction to Sorviets
° Se ne eaealy the Java programs that run on
a x the programs that are requested by
Pe oounents and are displayed in the
a response to the request.
+’ The servlet class is instantiated when web server
begins the execution,
+ The execution of servlet is managed by servlet
container, stich as Tomcat.
+ The servlet container is used in java for dynamically
generate the web pages on the server side. Therefore
the servlet container is the part of a web server that
interacts with the servlet for handling the dynamic
web pages from the client.
* Servlets are most commonly used with HTTP (.e.
Hyper Text Transfer Protocol) hence sometimes
servlets are also called as "HTTP Servlet”.
+.The main purpose of servlets is to add up the
functionality to a web server.
Client
Working of How Servlet Works ?
. The obtained servlet gathers the
Before learning the actual servlet programming it is,
very important to understand how servlet works.
(Refer Fig. 6.1.1)
. When a client make a request for some servlet,
he/she actually uses the Web browser in which
request is written as a URL.
. The web browser then sends this request to Web
server. The web server first finds the requested
servlet.
relevant
information in order to satisfy the client's request
and builds a web page accordingly.
|. This web page is then displayed to the client, Thus
.the request made by the client gets satisfied by the
servlets.
Fig. 6.1.1 How serviet works 7
(6-1)[644] Advantages
+ The servlets are very efficient in their performance
and get executed in the address space of, the
belonging web server.
+ The servlets are platform independent and can be
executed on different web servers.
+ The servlets working is based on Request-
Response. Any HTML form can take the user input
and can forward this input to the servlet. The
servlets are then responsible to communicate with
the back-end database and manipulate the required
‘business logic. These servlets embedded on the web
servers using Servlets API.
© Servlets provide a way to generate the dynamic
document. For instance : A servlet can display the
information of current user logged in, his logging
time, his last access, total number of access he made
s0 far and so on.
+ Multiple users can keep a co-ordination for some
application among themselves using servlets.
+ Using servlets multiple requests can be
synchronized and then can be concurrently
handled.
Review Questions
1. What is serolet ? Explain how it works ?
2,_What are advantages of servlets ?
[EZ] The Lite cycte of Serviet
+ In the life cycle of servlet there are three important
methods. These methods are,
LInit 2 Service 3. Destroy
Fig. 6.2.1 Life cycle of serviet
Sq
« The dient enters the URL in the web browse
makes a request. The browser then generates
HTTP request and sends it to the Web server,
Fig. 62.1)
+ Web server maps this request to the correspong,
servlet.
1. Init() Method : The server basically invokes y,
init) method of servlet. This method is cat
only when the servlet is loaded in the m
for the first time. Using this meth
initialization parameters, can also be passed
the servlet in order to configure itself.
2. service() Method : Server can invoke the serv
for particular HTTP request using servicd
method. The servlets can then read the dy
provided by the HTTP request with the help ¢
service() method.
3. destroy( ) Method : Finally server unloads te
servlet from the memory using the destroy!
method,
Review Q
1,_Explain the life cycle methods of servlet.
Creating Simple Serviet
+ When we write a servlet program, it is necessary ®
- i) Either implement Servlet interface or ii) Extend?
class that implements Servlet interface.
+ While implementing Servlet interface we mi
include [Link] package. Hence the first line*
out servlet program must be
‘import [Link].*;
* GenericServiet class is a predefit
implementation of Servlet interface. Hence we #
extend GenericServlet class in our servlet prog
Similarly, the HttpServiet class is a child asd
GenericServlet class, hence we can extend this
as well while writing the servlet program.
weAdvanced Jatw Programming 6:3
+ Hence following are the two ways by which we can write servlet program
‘import javaxsorviet.*;
import [Link].
import java.o:
public class Test extends public class Test extends HttpServiet
GonericServiet {
{ MMbody of serviet
Moody of sorviet }
}
+ The servlet gets the request from the client for some service. The servlet then processes the request
and sends the response back to the client. In order to handle these issues HttpServletRequest and
HitpServietResponse are used in servlet program.
+ These requests are handled with the help of some methods that are popularly known as methods of
HttpServlet. Thes
aut
methods are as follows
‘Method: Purpose
idoGet This method handles the HTTP GET request
‘doPost This method handles the HTTP POST request .
/doPut __ This method handles the HTTP Put request. é
/doDelete This method handles the DELETE request. Gaal
The doGet and doPost methods
+ The doGet method requests the data from the source
+ The doPost method submits the processed data to the source.
+ The protocol of doGet method is as follows
protected void doGet(HttpServietRequest request, HttpServletResponse response)
throws ServletException,IOException
© The ServletException and IOException are thrown to handle the Servlet problems gracefully.
©. The HitpServletRequest request : Contain the client request made by client.
©. The HttpServletResponse response : Contains the response made by servlet back to the client.
+” The protocol of doPost method is same as doGet method. It is as follows -
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
+ The GET request is more efficient than the POST request.
+ The GET request is less secure than the POST request.
How to Write Serviet Program ?
Open Notepad and write the first servlet code to display Greeting messages. Itis as follows
FirstServietjava
import [Link].*;
import [Link]."
a
Ta vacinical Publications” ~An up thrust fr knowtedaeuli
“Adoanced java Programming
import [Link].*;
public class FirstServlet extends HttpServiet
2
fi void doGet(tittpServietRequest request HttpServietResponse response)
throws IOException ServletException
{
[Link]("‘text/atml);
PrintWriter out=response. getWriter();
out printin(*");
[Link]("");
‘out printin(*
My First Servlet");
[Link](*"); into
-printin(*
Hello How are U?
");
pera ‘am enjoying this Servlet Application"); i
[Link](*
See You laterl
");
out. printla(""
[Link](**);
}
}
Program Explanation :
* ‘Inthe above program, we have imported following files, :
import [Link].*;
import [Link],*;
import [Link].*;
Out of these files [Link] package is useful for taking care of I/O operations.
The [Link] and [Link]-http are important Packages containing the classes and interfaces
that are required for the operation of servlets, The most commonly used interface from [Link]!
Package is Servlet, Similarly most commonly used class in this Package is GenericServlet. The
ServletRequest and ServletResponse are another two commonly used interfaces defined in
Parameters. The commonly uy;
ServletException,
* The MIME type is specified as using the setCoritent ,
for the HTTP response to type tn ie madthon ee TyPe() method, This method sets the content typ?
that the browser should interpret the ‘contents as cette as the MIME type. This means
¥ le,How to execute Serviet program?
Step 1: Compile the above program using the javac command at command prompt.
Di\test>javac FirstServiet java
The class file for this program gets generated.
Step2 : Before running any serviet program, itis necessary to have
1. JDK installed .
2. Tomcat installed.
3. Class path for above two packages must be set,
For Tomcat installation, I prefer to install the package XAMPP. The XAMP contains a directory for
‘tomcat. The XAMPP package contains Apache Web server, MySQL,.PHP and Perl support. It can work
on both Windows and Linux operating System.
Step 3: Copy the class file generated in Step 1 to the path
C : \xampp\tomcat\webapps\examples\WEB-INF\classes
Step 4: Goto the directory
C: \xampp\tomcet\webapps\examples\WEB-INF
Open [Link] file and edit it as follows
FirstServiet
FirstServiet
FirstServlet
/servlet/ FirstServiet
= ° ae .
er a is popularly known as deployment descriptor. This is basically the configuration file
wel
that describes how to map the URL of web application to servlet.
Internal alias NameAdvanced Jaoa Programming
FirstServiet
Es
FirstServlet
External alias Name
/serviet/ FirstServlet
°
The Servlet comes with two alias names, internal and external. The internal name is used by the Tomcat
and the external name is given (to be written in