• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EL cookie Problem

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

I am getting this as output

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








 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankitt,

try ${cookie.username.value}

 
Ankitt Gupta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic