• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PageContext.getSession vs req.getSession vs Session in JSP

 
andy armstrong
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are advatanges of using different methods
in a jsp.
to get a session you can use
pageContext.getSession()
vs.
session
vs.
request.getSession()?
 
Mike J. Beaty
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it not true that
pageContext.getSession() != request.getSession
and
session = request.getSession
Furthermore, isn't request.getSession() a thread-safe way to manage data while the context session is not?
Mike
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are all different way to access the same Session Object for that users Session. In a JSP the accepted way to access the session is to use the session reference provided.
You'll use pageContext.getSession in classes where all you have is a pageContext reference (ie TagLibs)
Session object are never garenteed to be Thread Safe. If a user fires up multiple browser windows under the same Server Session, they can get multiple threads running on your server accessing the same session object. You should always provide protection of the session objects to prevent contention.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic