set zero to session variable

hi experts

this is my scenario
in my jsp i have
<c:set var="cartobject" value="${cart.lineItemsCount}" scope="session"/>    

in my java class i am trying to set cartobject to zero, but it doesnt seem to work

this is my code in java class

if ((String) session.getAttribute("cartobject") != null)
              {
                    
                   String cartobj = (String) session.getAttribute("cartobject");
                  System.out.println("Value of cartobject before making zero==="+cartobj);
                  session.setAttribute("cartobj","0");
                  System.out.println("Value of cartobject after making zero==="+cartobj);

              }


console output :

Value of cartobject before making zero===1   // this is fine .

Value of cartobject after making zero===1  // this is supposed to be zero as in above code , but it shows 1.

any ideas

thanks
J
LVL 10
jaggernatAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
You need to do


System.out.println("Value of cartobject after making zero==="+session.getAttribute("cartobj"));
0
 
jaggernatAuthor Commented:
so u think code is OK

0
 
CEHJCommented:
Apart from the line i changed
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
CEHJCommented:
:-)

Try not to call getAttribute twice. Do you need to call it at all, since your objective is to set it?
0
 
jaggernatAuthor Commented:
Do you mean i should avoid this line :   String cartobj = (String) session.getAttribute("cartobject");

and directly put it like this

session.setAttribute((String) session.getAttribute("cartobject"),"0");


0
 
CEHJCommented:
First of all, why don't you do

session.setAttribute("cartobject", "0");

?
0
 
jaggernatAuthor Commented:
oh yea, thats right.

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.