Unit 10 Servlet
Unit 10 Servlet
Servlet Programming
Introduction
• Several years ago, client-server applications have become very
popular for building enterprise applications.
• In this model, client application is usually installed on the client’s
personal computer, which then sends requests to the server
application via network.
• In such a model, client applications usually contained most of the
presentation and application logic and the interaction with the
server application is done through a GUI.
• Such application clients are referred to as fat clients since the
application logic is also included in the client application. Any
change in the application logic requires reinstallation of the client
application on all the computers.
• With the advent of internet, application clients are completely
replaced with web clients.
• In this model, both the application logic and presentation logic are
physically separated from the clients PC and moved to the server
side.
• The Web client delegates all the user interactions to the server side
application logic which then processes the requests and uses the
presentation tier components to send the response.
• Since the application logic is no longer present on the client PC, web
clients are referred to as thin clients.
• Since the presentation logic is also moved to the server side, the
layout of the user interface can also be controlled from the server
side.
• A web client in this case is nothing but a browser application like
Internet Explorer, FireFox etc.
• Applications that use web clients for user interaction are called as
Web Applications.
• Web clients to interact with server side applications, use
the following:
1. A general purpose browser like Internet Explorer to send
requests and display the response.
2. HTML markup for defining the user interfaces for interaction
with the server
3. HTTP protocol to send and receive requests and responses.
• The way the web applications work is,
1. Web client like IE sends a request using HTTP protocol.
2. The server side program takes the HTTP request, processes it
and sends a HTTP response back to the web client. The
response includes body content in HTML format.
3. Web client then reads the HTTP response, formats the HTML
and displays it to the user.
• Since HTTP plays an important role in web applications,
let’s first know some basics about it and then look at the
server side details.
HTTP
• HTTP stands for Hyper Text Transfer Protocol. Following are some of
the important properties of HTTP:
1. It is a stateless protocol, meaning that every HTTP request is
independent of each other.
2. It can send data in the request. The server side program reads
the data, processes it and sends the response back. This is the
most important feature of HTTP, the ability to send data to
server side program.
• Based on how the data is sent in the request, HTTP requests are
categorized into several types, but the most important and widely
used are the GET requests and POST requests.
• A typical HTTP request is identified by something called URI
(Uniform Resource Identifier) as shown below:
• http:// <host name>:<port number>/<request details>
GET Request
• GET request is the simplest of all the requests.
• This type of request is normally used for accessing server-side static
resources such as HTML files, images etc.
• This doesn’t mean that GET requests cannot be used for retrieving
dynamic resources. To retrieve dynamic resources, some data must
be sent in the request and GET request can send the data by
appending it in the URI itself as shown below:
http://somedomain.com?uid=John&role=admin
• In the above URI, the HTTP request sends two pieces of data in the
form of name value pairs. Each name-value pair must be separated by
‘&’ symbol. The server side program will then read the data, process
it, and send a dynamic response.
• Though GET request is simplest of all, it has some limitations:
1. All the data in the request is appended to the URI itself making the
data visible to every one. When secure information need to be sent,
GET request is not the solution.
2. GET requests can only send text data and not the binary data.
Therefore in situations where you need to upload image files to
server, GET request cannot be used.
POST Request
• POST request is normally used for accessing dynamic
resources.
• POST requests are used when we need to send large amounts
of data to the server.
• Moreover, the data in the POST request is hidden behind the
scenes therefore making data transmittal more secure.
• In most of the real world applications, all the HTTP requests
are sent as POST requests.
• Now that we know the basics of HTTP, let’s move on to the
server side of the web applications.
Server Side of the Web Application
• The server- side is the heart of any web application as it
comprises of the following:
– Static resources like HTML files, XML files, Images etc
– Server programs for processing the HTTP requests
– A runtime that executes the server side programs
– A deployment tool for deploying the programs in the server
• In order to meet the above requirements, J2EE offers the
following:
– Servlet and JSP technology to build server side programs
– Web Container for hosting the server side programs.
– Deployment descriptor which is an XML file used to configure
the web application.
Web Container
• A Web container is the heart of Java based web application
which is a sophisticated program that hosts the server side
programs like Servlets.
• Once the Servlet programs are deployed in this container, the
container is ready to receive HTTP requests and delegate
them to the programs that run inside the container.
• These programs then process the requests and generate the
responses.
• Also a single web container can host several web applications.
• Look at the following figure that shows how a typical web
container looks like:
• There are several J2EE Web Containers available in the
market. One of the most notable one is the Apache’s Tomcat
container which is available for free of cost.
• This is what we will install for running web applications.
• Having just the web container is not sufficient. We need to
have a programming model to build server side programs that
run within this container.
• J2EE supports two key technologies to build server side
components in Java. These are Servlet and JSP technologies.
Structure of a Web Application
• For the web container to run the web application, the web application
must follow a standard directory structure within the container.
• Let’s say we want to build a web application named myweb. The
directory structure for this application must be as shown below:
Directory Description