You are on page 1of 7

Servlet

Table of Contents
1 Prerequisites ................................................................................................................................ 2
2 What are Servlets? ....................................................................................................................... 2
3 Functions of Servlets .................................................................................................................... 2
4 How Servlets work? ..................................................................................................................... 3
5 Life Cycle ...................................................................................................................................... 3
5.1 The init() method .................................................................................................................. 3
5.2 The service() method ............................................................................................................ 4
5.3 The destroy() method ........................................................................................................... 4
6 Sample Program ........................................................................................................................... 5
7 Form Data..................................................................................................................................... 7
7.1 GET and POST method .......................................................................................................... 7
7.1.1 GET method ................................................................................................................... 7
7.1.2 POST method ................................................................................................................. 7
7.2 Reading Form Data from Servlet .......................................................................................... 7

1 Prerequisites
You should know HTML, CSS and JavaScript before starting with Servlets. You should also have
good knowledge of Java programming language.

2 What are Servlets?


A Servlet is a Java programming language class that is used to extend the capabilities of servers
that host applications accessed by means of a request-response programming model. Servlets
provide a component-based, platform-independent method for building Web-based
applications. Servlets have access to the entire family of Java APIs, including the JDBC API to
access enterprise databases.
Java Servlets are programs that run on a Web or Application server and act as a middle layer
between a request coming from a Web browser or other HTTP client and databases or
applications on the HTTP server.

3 Functions of Servlets
Servlets perform the following functions
Servlet is used to read data entered in HTML form. This includes an HTML form on a
Web page or it could also come from an applet or a custom HTTP client program.
Servlets read the implicit HTTP request data sent by the clients. This includes cookies,
media types and compression schemes the browser understands, and so forth.
Servlets process the data and generate the results.
Servlets send the explicit data like document to the clients.
Servlets tell the browsers or other clients what type of document is being returned (e.g.,
HTML), setting cookies and caching parameters, and other such tasks.
2

4 How Servlets work?


Request

Client

Server
Response
Servlet generates
response
Server executes
servlet

Servlet
The server contains the Servlet Container. The requests are first authorized by Servlet Container
and then the Servlet is executed. The servlet container handles multiple requests by creating
multiple threads, each thread executing the service() method (discussed below) of a single
instance of the servlet.

5 Life Cycle
Life cycle of Servlet consist of three methods.
init() method : The servlet is initialized.
service() method : This method is called to process a clients request.
destroy() : The servlet is terminated.

5.1 The init() method


The init() method is called only once, when an instance of the servlet is created. When a user
invokes a servlet, a single instance of each servlet gets created. The init() method simply creates

or loads some data that will be used throughout the life of the servlet. This method can throw
ServletException.
Definition:
public void init() throws ServletException {
/* Initialization code */
}

5.2 The service() method


The service() method processes the clients request. When the server receives a request for a
servlet, it creates a new thread and calls service. The service() method checks the HTTP request
type like GET, POST, PUT, DELETE, etc. and calls doGet, doPost, doPut, doDelete, etc. methods
as required. Multiple service() methods are invoked by Servlet container in the form of threads
to handle multiple requests.
Definition:
public void service(ServletRequest request, ServletResponse response) throws
ServletException, IOException {
/* Code */
}

5.3 The destroy() method


The destroy() method terminates the servlet. It also allows to close database connections.
Definition:
public void destroy() {
/* Ending code */
}

6 Sample Program
Here we are going to use Netbeans IDE.
1. Download and install Netbeans IDE for Java EE (around 200 MB).
2. Open Netbeans IDE. Go to File -> New Project.
3. In Categories select Java Web, and in Projects select Web Application. Click Next.
4. Name your project and click Next.
5. Click Finish.
6. A directory structure will be displayed on the left side column.

7. To create a Servlet, expand Source Packages. Right click on <default package> and go to
New -> Servlet.
8. Name your Servlet class file and Servlet and click Finish.
9. A Servlet file will get created in <default package>

10. Go through the Servlet code. You will find this section there.

11. If you look closely they are HTML tags. Put some HTML tags of your own here.
12. Then go to Web Pages and open index.html.
13. Create an hyperlink there to call your servlet. See the following example.

14. In the heading <h3> tag I have used <a> tag. Note that the value of href attribute of <a>
tag should be the name of your servlet.
15. Thats it. Now Run the web application.

7 Form Data
7.1 GET and POST method
The GET and POST methods are used to pass information to web server.

7.1.1 GET method


It is used to pass information from browser to web server. It is the default method. Servlet
handles this type of request using doGet() method.

7.1.2 POST method


It is a more reliable method. It is similar to GET method. Servlet handles this type of requests
using doPost() method.

7.2 Reading Form Data from Servlet


Servlet handles form data parsing using following methods.

getParameter() method : This method gets the value of a form parameter.

getParameterValues() method : This method is called if the parameter appears more


than once and returns multiple values.

getParameterNames() method : This method is called if you want a complete list of all
parameters in the current request.

You might also like