You are on page 1of 3

SERVLET

INTRODUCTION TO SERVELT:

A Servlet is a server-side component, written in java that


dynamically extends the functionality of a server. Similar to the manner in which applets
run inside a Java-enabled Web browser on the client, servlets execute on a Java-enabled
user.

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.

Basic Servlet Structure:

All servlets that perform some useful function have two things in
common.

 First, they all extend one of two servlet classes- GenericServlet or


HttpServlet. Extending these classes provides a framework for creating a
servlet as well as significant default functionality.
 Second, all servlets override at least one method wherein custom
functionality is implemented.

public class SkeletonServlet extends HttpServlet


{
public void init()
{
//initialization code goes here
}

public void service()


{
//meaningful work happens here
}

public void destroy()


{
//free resources here
}
}

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

The service() method makes use of two important methods:

 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.

You might also like