150 likes | 252 Views
Sessions. Many interactive Web sites spread user data entry out over several pages : Ex: add items to cart, enter shipping information, enter billing information Problem: how does the server know which users generated which HTTP requests?
E N D
Sessions • Many interactive Web sites spread user data entry out over several pages: • Ex: add items to cart, enter shipping information, enter billing information • Problem: how does the server know which users generated which HTTP requests? • Cannot rely on standard HTTP headers to identify a user
Sessions Server sends back new unique session ID when the request has none
Sessions Client that supports session stores the ID and sends it back to the server in subsequent requests
Sessions Server knows that all of these requests are from the same client. The set of requests is known as a session.
Sessions And the server knows that all of these requests are from a different client.
Session Tracking It is a concept which allows you to maintain a relation between 2 successive requests made to a server on the internet. HTTP Stateless protocol Implement the session tracking Http session object live on server which seems like hash table - Hidden fields in form <input type=“hidden” name=“bookID” value=“100”> - URL Rewriting hidden fields are view some browser specify the limit on the length of the URL Example search result for book with checkbox <b>Search results for books</b> <form method =“post” action =“serverprogram.jsp”> <input type=“checkbox” name=“bookID” value=“100”> Java Servlet Programming <br> <input type=“checkbox” name=“bookID” value=“101”> C Programming <br> </form>
Basic steps for session tracking • Accessing the session object associated with the current request Call request.getSession() to get an HttpSession object which is a simple hash table for storing user-specific data. • Looking up information associated with a session. Call getAttribute()on the HttpSession object, cast the return value to the appropriate type,and check whether the result is null. • Storing information in a session. Use setAttribute()wth a key and a value. • Discarding session data. Call removeAttribute() to discard a specific value. Call invalidate() to discard an entire session • Method of getId() Returns the unique identifier generated for each session. • Method of isNew() Returns true : if the client (browser) has never seen the session Returns false: for preexisting session.
Sessions • Returns HttpSession object associated • with this HTTP request. • Creates new HttpSession object if no session ID in request or no object with this ID exists • Otherwise, returns previously created object
Sessions Boolean indicating whether returned object was newly created or already existed.
Sessions Incremented once per session