• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unable to understand jsp session concept

 
Mahesh Bidarkar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody please give me a brief introduction about session in java
 
Tim Holloway
Bartender
Posts: 18417
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Mahesh!

I'm sure someone can point you to a more formal text, but the essence is this:

HTTP is a stateless protocol. That is, you don't connect client to server and communicate continuously like some sort of time-sharing computer system. Instead a client connects, sends a request, gets a response, then disconnects. Want another page? connect, request, response, disconnect.

This is good for low-overhead operations, but since there's no ongoing state, it makes it difficult to do things where the next request wants to build on the results of the last request on an individual client basis. To get around this, we have HttpSessions.

An HTTPSession is an internal server object that gets built by an getSession(true) method call. It contains a number of support objects, but the most important one for most webapp users is the session object dictionary. You can construct (serializable) Java objects and store them by name as session objects using the HttpSession get/set attribute methods and therefore hold application state on a per-user basis.

Since HTTP itself is stateless, however, there needs to be some sort of ongoing way to associate a given client with its server-side HttpSession. This is done by keeping another dictionary on the server that's full of HttpSession objects, keyed by "jsessionid" values. The jsessionid is a (more or less) random character string that is assigned to a given user session and sent to the client, which can then send it back to the server as part of the next request to identify the client's session.

The jsessionid can be propagated in one of 2 ways. Via a cookie or as an appendix to a URL. Cookies are less fragile, but in many countries cookies are controlled, so ideally a webapp will support both possibilities. Cookie support is automatic, but you do have to use URL rewriting to append the jsessionid if the client won't accept cookies.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim has given you an excellent explanation, but I'll add a simple example that may help. A shopping cart. When are shopping on Amazon or wherever, your session is how the server keeps track of the items you've added to the cart. If it didn't have this ability, the contents of the shopping cart would be lost every time you changed to a different page.
 
Vin Pal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahesh, I struggled to understand JSP's my self. The following you tube link will give you a very clear explanation of what a JSP actually is. It is a playlist. You can either start from the beginning of the play list, or if you want to dive straight into JSP's then kindly proceed to the 11th tutorial of the playlist.

Here is the Youtube link

https://www.youtube.com/watch?v=jynBtHZ7HKw&list=PLTo_YL4Dk4Z_SxrG0XwbE1pGYyehh9gX3
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!