We know that HTTP is a stateless protocol. It provides no built-in way for a server to recognize that a sequence of requests all originated from the same user.
Privacy advocates may consider this a feature, but most web programmers see it as a major headache because web applications aren't stateless.
Robust web applications need to interact back and forth with the user, remembering information about the user between requests.
For example, in a shopping cart application, a client has to be able to put items into his virtual cart, and the server has to remember his items until he checks out several page requests later, or sometimes even days later!
The solution, as you may have already guessed, is for a client to introduce itself as it makes each request. Each client needs to provide a unique identifier that lets the server identify it, or it needs to give some information that the server can use to properly handle the request.
How to Manage Session
HTTP and session state have to do with how a Web Application uses HTTP to maintain the state of a user's session. HTTP is a connection-oriented protocol; it goes over TCP and not UDP.
When a browser sends a request to a server, the browser establishes a connection, sends an HTTP request, and consumes an HTTP response. If the response is an HTML page, then the client will typically parse the page looking for other tags that require data to be downloaded.
If there are such tags on the page, then the browser will re-use the same connection to download that data. However, as soon as the page "transaction" is complete, the browser will close the connection.
This has a major impact on the way Web Applications work. Most applications maintain data on behalf of a user and need to track users. The data may be a shopping cart or simply user preferences, but as each user request is sent over a different connection, there is no way to link subsequent requests and keep the state.
Client Identification
Given the preceding, you should be curious as to how a server identifies a client. Essentially, the client and server code need to exchange some identifier that uniquely identifies this client to this server. Note that this identifier does not uniquely identify this client on the Internet; it is a value that only means something to the server that creates it. This identifier can be exchanged either with or without the client's cooperation.
Session Scope
The HttpSession object is a data container. The data stored in the container are private to a given client and will persist until the server destroys the client's session. HttpSession has four methods that allow objects to be used with session scope.
Example
Source Code of index.jsp
Source Code of LoginServ.java
Source Code of home.jsp
Let's try another code
Source Code of index.jsp
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.