• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL cookie Problem

 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I am getting this as output

Cookie name is :javax.servlet.http.Cookie@17509be








 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankitt,

try ${cookie.username.value}

 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Balu Sadhasivam wrote:
Ankitt,

try ${cookie.username.value}




Yes Balu,i tried but when i am running the code it is displaying blank but after refreshing the page it displays the correct result.
And please can you explain why "value" is needed
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ${cookie.username.value} basically does like getCookieByName("username").getValue(). The cookie class at its own has several getter method like getValue(), also see javax.servlet.http.Cookie.

I should add, although this is clearly a test case, but in real life you shouldn't use a cookie for this kind of information. Rather use the HttpSession.
 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:The Cookie class doesn't have a getUsername() method at all. How did you come into it?
Read the API doc which getters are all available: javax.servlet.http.Cookie.

I should add, although this is clearly a test case, but in real life you shouldn't use a cookie for this kind of information. Rather use the HttpSession.



But i am taking the value from
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankitt Gupta wrote:can you explain why "value" is needed

Your original EL expression reference the cookie instance. To obtain its value, you need to reference the value property. This is the same as for any other bean instance.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankitt Gupta wrote:

But i am taking the value from

Sorry, I misinterpreted the problem, I've edited my reply.
 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Ankitt Gupta wrote:can you explain why "value" is needed

Your original EL expression reference the cookie instance. To obtain its value, you need to reference the value property. This is the same as for any other bean instance.


ok i got it ...it was basically printing the objects toString()

but the correct result is displayed after refreshing..On running for the first time it displays a blank
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankitt Gupta wrote:
Balu Sadhasivam wrote:
Ankitt,

try ${cookie.username.value}




Yes Balu,i tried but when i am running the code it is displaying blank but after refreshing the page it displays the correct result.
And please can you explain why "value" is needed



Here "username" is just a cookie name , and uses "value" property to fetch the value ( internally uses getValue() of Cookie Class.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65128
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankitt Gupta wrote:but the correct result is displayed after refreshing..On running for the first time it displays a blank

Cookies are a good way to remember data on the client for later, but are not a great way to transfer data to the client.

Remember, cookies are a client-side mechanism. When you "create" a cookie on the response you are actually doing no such thing. You are creating a header on the response that tells the browser to create the cookie when it gets the response.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, hence my HttpSession suggestion.

Only use a cookie if you really know/understand what you're actually doing. Do not choose solutions randomly.
 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much Appreciated Thanks to Everyone.

I don't know whether it is appropriate to ask here but still....can anyone provide me some article or notes on paths i.e context-path,class path etc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic