1 / 18

CS320 Web and Internet Programming Cookies and Session Tracking

CS320 Web and Internet Programming Cookies and Session Tracking. Chengyu Sun California State University, Los Angeles. Session Tracking. The Need shopping cart, personalization, ... The Difficulty HTTP is a “stateless” protocol Even persistent connections only last seconds The Trick??.

lfowlkes
Download Presentation

CS320 Web and Internet Programming Cookies and Session Tracking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS320 Web and Internet ProgrammingCookies and Session Tracking Chengyu Sun California State University, Los Angeles

  2. Session Tracking • The Need • shopping cart, personalization, ... • The Difficulty • HTTP is a “stateless” protocol • Even persistent connections only last seconds • The Trick??

  3. General Idea request client server response + session id (sid) request + sid request + sid request + sid request + sid

  4. Three Ways to Implement Session Tracking • URL Re-writing • E.g. http://acm.calstatela.edu/forum/viewforum.php?f=2&sid=eef552c5e31118841a5f03a8221d0868 • Hidden form field • Cookies

  5. Example: GuestBook1 • Track user using hidden form field

  6. Cookies • Issued by the server • HTTP Response: Set-Cookie • Part of the next client request • HTTP Request: Cookie

  7. Cookie Attributes • Name, Value • Host/Domain, Path • Require secure connection • Max age • Comment (Version 1)

  8. Servlet Cookie API • Cookie • http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/Cookie.html • HttpServletResponse • addCookie( Cookie ) • HttpServletRequest • Cookie[] getCookies()

  9. Example: GuestBook2 • Track user using cookies

  10. Cookie or No Cookie? • Is cookie a potential security problem? • Virus?? • DoS?? • How about privacy? • Cookie manager in Mozilla/Firefox(?) • Internet Options in IE

  11. It’s Not Easy ... • ... to generate unique and random session id’s • ... to associate more information with a session • ... to tell whether the client has already left

  12. Servlet Session Tracking API • HttpServletRequest • HttpSession getSession() • HttpSession • http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/HttpSession.html • setAttribute( String, Object ) • getAttribute( String ) • invalidate()

  13. Example: Shopping Cart • Shopping cart • Show a list of products • Add a product to the shopping cart • Remove a product from the shopping cart • Using Session Tracking API

  14. Is Session Shared among Servlets? Example: Members Only! Username: Password: Login Members

  15. Example: Login and Members • Login • Validate username and password • Failed: redirect to error page • Succeeded: set a session attribute “username”, and redirect to Members • Members • Check session attribute “username” • null: redirect to Login • otherwise display content

  16. ServletContext Attributes vs. Session Attributes • Application Scope variables • Session Scope variables

  17. Example: SharedRequestCounter2 • Two request counters, one in application scope and one in session scope • Application scope vs. session scope • Similarities?? • Differences?? • Usage??

  18. Session Configuration in web.xml • Default session timeout in Tomcat is 30 minutes • Session timeout can be changed in web.xml • The timeout value must be an integer • Session never timeout if value <= 0 <session-config> <session-timeout>60</session-timeout> </session-config>

More Related