Session Management

Session Management - 1 Cookies are a general mechanism...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Cookies are a general mechanism which server side applications can use to both store and retrieve information on the client side Servers send cookies in the HTTP response and browsers are expected to save and to send the cookie back to the Server whenever they make additional requests from the Server 2 3 Web browser Web server request response put cookie ... 4 Web server response request ... :Cookie Web browser 5 HTTP/1.1 200 OK Date: Tue, 04 May 2004 22:19:02 GMT Content-Length: 43 Expires: Thu, 15 Apr 2010 20:00:00 GMT Cache-Control: private Set-Cookie: B=08b858509g5mm&b=2&f=g ; expires=Thu, 15 Apr 2010 20:00:00 GMT ; path=/ ; domain=.yahoo.com 6 HTTP/1.1 302 Found Content-Type: text/html Proxy-Connection: close Set-Cookie: PREF=ID=3e6397d87f7e7160:LD=en:CR=2:TM=10812 94885:LM=1081295099:S=98ujTaoPdeFrVcKD ; expires=Sun, 17-Jan-2038 19:14:07 GMT ; path=/ ; domain=.google.com Location: http://www.google.com/ A cookie in a response header: Set-Cookie: NAME = VALUE ; expires= DATE ; path= PATH ; domain= DOMAIN_NAME ; secure Only the NAME field is required A cookie in a request header: Cookie: NAME1 = VALUE1 ; NAME2 = VALUE2 ; NAME3 = VALUE3 ... This header contains all matching stored cookies 7 NAME = VALUE : the content of the cookie should not contain semi-colon, comma or white-space expires= DATE : expiration date default is the session life time path= PATH : the paths to which the cookie is valid matches every path that begins with PATH domain= DOMAIN_NAME : the cookies domain matches every path that ends with DOMAIN_NAME secure : send only through secure channels (i.e. https) 8 Get the cookies from the service request: Cookie HttpServletRequest.getCookies() Add a cookie to the service response: HttpServletResponse.addCookie( Cookie cookie) Cookie getter methods: getName() , getValue() , getPath() , getDomain() , getMaxAge , getSecure Cookie setter methods: setValue() , setPath() , setDomain() 9 <html> <head> <title>Login Page</title> </head> <body> <h1> Logon to My Site </h1> <form action= "servlet/WelcomeBack" > Your Name: <input type= "text" name= "username" > <input type= "submit" > </form> </body> </html> 10 public class WelcomeBack extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String user = req.getParameter( "username" ); if (user == null) { Cookie cookies = req. getCookies() ; for (int i = 0 ; cookies!=null && i < cookies.length ; i++) { if (cookies[i].getName().equals("username")) user = cookies[i]. getValue() ; } } else res. addCookie(new Cookie( "username" , user )) ; if (user != null) { res.setContentType( "text/html" ); PrintWriter out = res.getWriter(); out.println(" <html><body><H1>Welcome Back "...
View Full Document

Page1 / 64

Session Management - 1 Cookies are a general mechanism...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online