Professional Documents
Culture Documents
Servlet Notes
Servlet Notes
INTRODUCTION TO SERVELT:
Servlets similar to CGI allows two-way interaction between the client and
server. Servlets offer advantages over CGI in the areas of performance, portability, and
security.
All servlets that perform some useful function have two things in
common.
init() :
It is called a single time when the servlet is first loaded. It
is similar to class constructor in that it provides a method wherein
initialization code is guaranteed to be run.
destroy() :
It is executed when the servlet is unloaded. It is
used to free any resources held by the servlet.
service() :
The server calls this method wherever a servlet
request is received. For Http servlets, this method should usually not be
overridden.
The three important packages that are to be imported by the Java servlet program are:
javax.servlet
javax.servlet.http
java.io
doget()
dopost()
These methods have two arguments to request and to respond. The servlet program
throws two exceptions namely, ServletException and IOException. It is important to set
the content type which may be text/html or text/css. It corresponds to the program to
which we implement. The inputs can be requested by using the getParameter() method.
The client side program is a HTML program which is used to execute the program. The
URL (where the server program is located) is to be mentioned in the FORM tag. By
connecting the client program to the server program and by running the client program
the required output is obtained.