Servlet
Mostafa Abdallah
1
Static content
How server process it
URL (Request)
Web Browser Web Server HTML Files
HTML (Response)
3
Dynamic content.
Dynamic web Contents
• Request for the same document returns
different results depend on who made the
request.
• The Web server needs to run certain
programs(Servlet) to process user requests.
5
How to send data to program
• We send data to Servlet program using HTML forms.
• HTML forms send it using Get or Post methods
6
The GET and POST Methods
• If the GET method is used, the data in the
form is appended to the request string as if
it were submitted using a URL
• If the POST method is used, the data in the
form is packaged as part of the request file.
• The server program obtains the data by
reading the file.
7
?What does Servlet Do
• Receives client request.
• Extract some information from the request.
• Perform dynamic contents generation.
• Create and send response to client or
forward the request to another servlet or
JSP page.
Servlet Request and Response
Model
9
Creating Servlet
• Every servlet is a subclass of the HttpServlet
class.
• You need to override appropriate methods
(doGet, doPost) in the HttpServlet class to
implement the servlet.
10
Example
11
Running Servlets
• To run Java servlets, you need a servlet
container.
• Many servlet containers are available
(Tomcat, Glassfish,…..).
12
Servlet Life-Cycle
Servlet Life-Cycle
14
Handling Requests Containing
Data
• Parameters are passed as (name-value) pairs
in request.
• The request object's getParameter()
method accepts the name as an argument
and returns the corresponding String value,
or null if the parameter is not part of the
request.
• Ex: String firstName = [Link]( "firstname" );
15
Registering Student into a Database
16
? What is a Session
• A session can be defined as a series of
related interactions between a single client
and the Web server over a period of time.
• To track data among requests in a session is
known as session tracking.
17
Session Tracking
• Web servers use Hyper-Text Transport
Protocol (HTTP). HTTP is a stateless protocol.
• The HTTP Web server cannot associate
requests from a client together.
• We solve this problem Using hidden values,
using cookies, and using servlet API.
18
Example
19
Example
20
Session Tracking Using Hidden
Values
• You can track session by passing data from
the servlet to the client as hidden value.
• <input type=”hidden” name=”lastName”
value=”Smith”>
• So the next request will submit the data
back to the servlet.
21
Session Tracking Using Hidden
Values
• How this solve the problem?
22
Session Tracking Using Cookies
• Cookies are small text files that store sets of
name=value pairs on the disk in the client’s
computer.
• Cookies are sent from the server through
the instructions in the header of the HTTP
response.
• The browser will then send the cookie with
any request submitted to the same server.
23
Session Tracking Using Cookies
• How this solve the problem?
24
Session Tracking Using Cookies
•To send a cookie to the browser
• [Link](cookie);
• it take an object of the Cookie class
•To obtain cookies from a browser, use
• [Link]();
25
Session Tracking Using the
Servlet API
• Problems of session tracking with hidden data
and cookies are that data are not secured and
difficult to deal with large set of data.
• Java servlet API provides a session tracking
tool, which enables tracking of a large set of
data.
• Data can be stored as objects.
• Data are kept on the server side so they are
secure.
26
The HttpSession Class
• To use the Java servlet API for session tracking,
• create a session object
• HttpSession session = [Link](true);
• This obtains the session or creates a new session if the
client does not have a session on the server.
• The HttpSession class provides the methods for
reading and storing data to the session, and for
manipulating the session.
27
The HttpSession Class
• How this solve the problem?
28