Servlet: Mostafa Abdallah
Servlet: Mostafa Abdallah
Mostafa Abdallah
1
Static content
How server process it
URL (Request)
HTML (Response)
3
Dynamic content.
Dynamic web Contents
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
10
Example
11
Running Servlets
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 = request.getParameter( "firstname" );
15
Registering Student into a Database
16
? What is a Session
17
Session Tracking
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
22
Session Tracking Using Cookies
23
Session Tracking Using Cookies
24
Session Tracking Using Cookies
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
27
The HttpSession Class
28