Solved

Is it possible to set a session attribute in a JSP and then recall it when the page is loader at a later time?

Posted on 2009-05-04
8
436 Views
Last Modified: 2013-11-24
Is it possible to set a session attribute in a JSP and then recall it when the page is loader at a later time?
My attribute is actually set below where it is gotten.  Its okay if the first load of the page is null but when the page is returned to in a minute or so, I would like to get the value when I reload the page.  Here is are the lines of code as they appear in my JSP:

My flag value is <%=  session.getAttribute("relatedCasesFlag")%>

<% session.setAttribute("myVisitFlag", Boolean.valueOf(true)); %>
0
Comment
Question by:jwright9
  • 5
  • 3
8 Comments
 
LVL 27

Expert Comment

by:rrz
ID: 24299619
Yes, a session-scoped object will be around for the whole session.  
Please restate your question. It is not clear.
0
 

Author Comment

by:jwright9
ID: 24299710
Sure I'll be glad to restate it.   I want to set a flag when a user first visits a page.  The flag is set to true and I put the value of myVisitFlag to true.

<% session.setAttribute("myVisitFlag", Boolean.valueOf(true)); %>

When the user visits the page again, the flag will be set to true.  In my page's JSP code, I have put the line you see below above the line where the value is put in the session.

My flag value is <%=  session.getAttribute("relatedCasesFlag")%>  

If the flag is true the page has been visited before and my other code behaves how I need it to when the page has been visited already.  Hope this helps.  Thanks for your help.

0
 
LVL 27

Expert Comment

by:rrz
ID: 24300063
I am still unclear as to what you are trying to do here.  You don't need to create a flag in order to know if the user has joined the session and has returned to the page. See code below here.
<html>

<body>

<%

  if(session.isNew()){

                      out.print("This is your first visit for this session.");

  }else out.print("This is a repeat visit for this session.");

%>

</body>

</html>

Open in new window

0
 

Author Comment

by:jwright9
ID: 24300108
I insert this code in my page but it did not work.  It definitely should work I would think.  I am working inside of RAD 7.  I ran the application in a separate browser and it still did not work.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:jwright9
ID: 24300158
Oh one other thing.  I not concerned if the session is new or not.   I want a flag so that when the user goes up in the page "hierarchy" I can reset the flag so that things are reset back to normal until the user visits the page again and drills down into links on the page.
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 24300387
Please try this code and tell us if it is closer to what you want to accomplish. I have shown two ways. Sometimes it is clearer  to use Strings instead of the Boolean object.
<html>

<body>

<%

  Boolean myVisitFlag = (Boolean)session.getAttribute("myVisitFlag");

  if(myVisitFlag == null){

                          session.setAttribute("myVisitFlag", new Boolean(false));

                          myVisitFlag = new Boolean(false);

  }

  if(myVisitFlag) out.print("This is a repeat visit for this session.");

  else{

       out.print("This is your first visit for this session or the flag has been reset.");

       session.setAttribute("myVisitFlag", new Boolean(true)); 

   }

%>

<br/>

<%

  String flag = (String)session.getAttribute("flag");

  if(flag == null){

                          session.setAttribute("flag", "false");

                          flag = "false";

  }

  if("true".equals(flag)) out.print("This is a repeat visit for this session, using String flag.");

  else{

       out.print("This is your first visit for this session or the flag has been reset, using String flag.");

       session.setAttribute("flag", "true"); 

   }

%>

</body>

</html>

Open in new window

0
 

Author Comment

by:jwright9
ID: 24336508
This helped thanks rrz@871311.  There was a process that was clearing out the "unregistered" name value pairs from the session.  I really appreciate the effort that you put forth and the example code helped me understand the situation much better.  Cheers.
0
 

Author Closing Comment

by:jwright9
ID: 31577774
This person is a great teacher.  
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now