Servlets: Query Parameters, Sessions
Servlets: Query Parameters, Sessions
Passing parameters to
Servlets
Passing Parameters to
Servlet
Query Parameters
Request Headers
Request Body
Query Parameters
Parameters that are passed with the
resource URL as a query string
Query String: Starts with ?
Parameters separated by: &
Example: query-parameters
getRequestURI
Returns the context root
getServletPath
Returns the url-pattern
Example:
Use query-parameters
Request Headers
We can set arbitrary headers
Convention is to start the header
with X-
http://stackoverflow.com/questions/3561
381/custom-http-headers-namingconventions
Request Body
Parameters can be sent in as request
body
Example:
Use query-parameters doPost
Use RESTClient Firefox add-on
Request Headers
E.g.: Cookies
Request Body
Sessions
What is a Session
Application-defined set of logical actions
that have some application-specific
semantic meaning
Example 1: Shopping cart
A session is actions between user adding
something to a cart till the payment is confirmed
on the payment screen
IP: 1.1.1.1
Client 2
IP: 1.1.1.2
Network
Address
Translation
IP: 2.2.2.2
Server
Req 2
Persistent
Connection
Socket
Server
Session
How to send sessionid to the client?
What mechanisms does the server have to send
back data to the Client?
Approach 1: Response Headers
Approach 2: Resource URL itself
Session Cookie
Approach 1: By embedding it in a
Response Header (Cookies)
What is a Cookie?
Small piece of information sent by the
server in a response header to the client
for tracking purposes
Session Cookie
Server uses Set-Cookie response
header to send the sessionid to the
client
Client uses Cookie request header to
send the sessionid in subsequent
requests
RFC 2109
https://www.ietf.org/rfc/rfc2109.txt
Session Cookie
Cookie attributes
Cookie name
Maximum age
Secure flag
The client will send the cookie only if the
server is operating with a secure protocol
like https
Http-only flag
The cookie will be accessible only to
browsers and not to technologies such as
JavaScript
Session Cookie
Cookie name, cookie value:
ASCII character set
http://stackoverflow.com/questions/1969232/
allowed-characters-in-cookies
Maximum age
Specified in seconds
Negative value means that the cookie wont
be persistently stored
Session Cookie
Cookie attributes
Domain name
Cookie set by parent domain is available to
all its sub-domains
Example:
Cookie set by cs.utexas.edu will be sent when
requesting the page for www.cs.utexas.edu
But it wont be sent for utexas.edu
Path
Resource path
What is a ``Cookie?
Small piece of information used for tracking
purposes
Examples
Cookie storage in Firefox
Show how to access cookies.sqlite
http://stackoverflow.com/questions/7610896/
how-to-use-sqlite-to-read-data-from-thefirefox-cookies-file
URL Rewriting
URL Rewriting
Server will generate a unique session ID
Within application code, we ``rewrite URL by
``encoding it with the generated session ID
Encoding step appends the session ID to the
URL
JSESSIONID=<sessionID>
URL Rewriting
Example:
Use url-rewriting:
- Key steps:
- HttpSession session = request.getSession(true);
- String encodedURL = response.encodeURL(url);
- request.getSession():
- Returns the current HttpSession associated with this
request or, if there is no current session and create is
true, returns a new session
- response.encodeURL():
- Encodes the specified URL by including the session ID
in it
How to avoid
Disable embedding sessionIDs in URLs
So no URL rewriting method for session tracking
Session Fixation
Attacker sends the links to the victims with a
session ID embedded in it
When users click on it, the attacker gets the control
Insecure Cookies
Man-in-the-middle attack
Attacker observing network traffic between Client and Server
How to prevent?
Use the ``Secure attribute
Indicates that the cookie should only be transferred over HTTPS
Cookie is transmitted as encrypted
Drawback:
Site needs to be behind HTTPS
Readings
Chapters 1, 2, 3, 5 of ``Java for Web
Applications book