DOC PREVIEW
Penn CIT 597 - Session Tracking

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

More About ServletsPersistent informationServer capabilitiesSession trackingSession tracking solutionsHidden <form> fieldsCookiesUsing cookiesSome more Cookie methodsMore HttpServletRequest methodsThe Session Tracking APISummaryThe EndJan 13, 2019More About ServletsSession TrackingPersistent informationA server site typically needs to maintain two kinds of persistent (remembered) information:Information about the sessionA session starts when the user logs in or otherwise identifies himself/herself, and continues until the user logs out or completes the transaction (for example, makes a purchase)Information about the userUser information must generally be maintained much longer than session information (for example, remembering a purchase)This information must be stored on the server, for example on a file or in a databaseServer capabilitiesServlets, like Applets, can be trusted or untrustedA servlet can use a unique ID to store and retrieve information about a given sessionUser information usually requires a login ID and a passwordSince servlets don’t quit between requests, any servlet can maintain information in its internal data structures, as long as the server keeps runningA trusted servlet can read and write files on the server, hence can maintain information about sessions and users even when the server is stopped and restartedAn untrusted servlet will lose all information when the servlet or server stops for any reasonThis is sometimes good enough for session informationThis is almost never good enough for user informationSession trackingHTTP is stateless: When it gets a page request, it has no memory of any previous requests from the same clientThis makes it difficult to hold a “conversation”Typical example: Putting things one at a time into a shopping cart, then checking out--each page request must somehow be associated with previous requestsThe server must be able to keep track of multiple conversations with multiple usersSession tracking is keeping track of what has gone before in this particular conversationSince HTTP is stateless, it does not do this for youYou have to do it yourself, in your servletsSession tracking solutionsCookies are small files that the servlet can store on the client computer, and retrieve laterURL rewriting: You can append a unique ID after the URL to identify the userHidden <form> fields can be used to store a unique IDJava’s Session Tracking API can be used to do most of the work for youHidden <form> fields<input type="hidden"name="sessionID"value="...">Advantage:Requires the least knowledge: All you need to know is how to read and write parametersDisadvantages:Not kept across sessions, so useless for maintaining persistent information about a userSince the session ID must be incorporated into every HTML page, every HTML page must be dynamically generatedThere’s not much more to say about using hidden form fields, since you should already know enough to do itCookiesA cookie is a small bit of text sent to the client that can be read again laterLimitations (for the protection of the client):Not more than 4KB per cookie (more than enough in general)Not more than 20 cookies per siteNot more than 300 cookies totalCookies are not a security threatCookies can be a privacy threat Cookies can be used to customize advertisementsOutlook Express allows cookies to be embedded in emailA servlet can read your cookiesIncompetent companies might keep your credit card info in a cookieNetscape lets you refuse cookies to sites other than that to which you connectedUsing cookiesimport javax.servlet.http.*;Constructor: Cookie(String name, String value)Assuming request is an HttpServletRequest and response is an HttpServletResponse,response.addCookie(cookie);Cookie[ ] cookies = request.getCookies();String name = cookies[i].getName();String value = cookies[i].getValue();There are, of course, many more methods in the HttpServletRequest, HttpServletResponse, andCookie classes in the javax.servlet.http packageSome more Cookie methodspublic void setComment(Stringpurpose)public String getComment()public void setMaxAge(intexpiry)public int getMaxAge()Max age in seconds after which cookie will expireIf expiry is negative, delete when browser exitsIf expiry is zero, delete cookie immediatelysetSecure(booleanflag )public boolean getSecure()Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSLMore HttpServletRequest methodspublic HttpSession getSession()Gets the session object for this request (or creates one if necessary)public Enumeration getHeaderNames()Gets an Enumeration of all the field names in the HTTP headerpublic String getHeader(Stringname)Given the header name, return its valuepublic int getIntHeader(Stringname)Given the header name, return its value as an intReturns -1 if no such headerCould throw a NumberFormatExceptionpublic Enumeration getHeaders(Stringnam e)Given the header name, return an Enumeration of all its valuesThe Session Tracking APIThe session tracking API is in javax.servlet.http.HttpSession and is built on top of cookiesTo use the session tracking API:Create a session:HttpSession session = r equest.getSession();Returns the session associated with this requestIf there was no associated session, one is createdStore information in the session and retrieve it as needed:session.setAttribute(name, value);Object obj = getAttribute(nam e);Session information is automatically maintained across requestsSummaryA session is a continuous interaction with the userHTTP is stateless, so the programmer must do something to remember session informationThere are multiple ways to remember session informationThe session ends when the user quits the browser (or a session may be set to time out)Some information must be kept longer than just within a sessionFor example, if the user orders a product, that information must be kept in a databaseLong-term storage of information requires that the servlet have some additional privilegesThe


View Full Document

Penn CIT 597 - Session Tracking

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Session Tracking
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Session Tracking and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Session Tracking 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?