Professional Documents
Culture Documents
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.
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
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.
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 */
}
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.
getParameterNames() method : This method is called if you want a complete list of all
parameters in the current request.