Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SHould we get session on JSP pages before writing session.getattribute?  RSS feed

 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may sound like a novice question but i couldn't get answer anywhere so i am posting it here.
I have set a session on my other JSP page as

session.setAttribute("session_set",name);

Now I am getting my session variable in other JSP pages as :

session.getAttribute("session_set");

But I have seen people sestting session onJSP page as :

HttpSession session=request.getSession(true);
session.setAttribute("session_set",name);

and get session on JSP page as:

HttpSession session=request.getSession(false);
session.getAttribute("session_set");

Kindly tell me which is the correct approach and is there any difference in both approaches?
 
Viktor Pergjoka
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


This returns the current session. If there is no current session, a new session will be created.



This will return the current session. If there is no current session, no new session will be created.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using JSTL and EL, you don't need to get the session at -- and you should be using the JSTL and EL. You can just reference the scoped variable by name, and it will automatically be found in the session if it is there.
 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Using JSTL and EL, you don't need to get the session at -- and you should be using the JSTL and EL. You can just reference the scoped variable by name, and it will automatically be found in the session if it is there.

Could you give me some example how to do that?
 
Prashant Soood
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Viktor Pergjoka wrote:Hi,


This returns the current session. If there is no current session, a new session will be created.



This will return the current session. If there is no current session, no new session will be created.


I know that my question is that could i get session on JSP page with writing only session.getattribute directly.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prashant Soood wrote:
Bear Bibeault wrote:Using JSTL and EL, you don't need to get the session at -- and you should be using the JSTL and EL. You can just reference the scoped variable by name, and it will automatically be found in the session if it is there.

Could you give me some example how to do that?


You refer to a scoped variable in EL like this:



You really ought to spend some time learning about JSTL and EL, since it's been the mainstream method of coding JSP for over a decade now, instead of scriptlets.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!