• 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

What would be a proper way of reading a cookie in JSP

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have a login page which has two textboxes for name and pasword.
Once the user submits the information the servlet sends it a cookie which contains the login and password so that the user doesn't have to type it again when he comes to this address. Anyways I wanted to ask what would be the proper way of reading the cookies when the JSP page is loaded.
I am currently using the following method which works. But i have been told using scriptlets is a bad practise... which i agree so is there any better way to accomplish this
My current jsp looks something like this



One more thing which bothers me is that if there are no cookies my textbox has a "/" written in it why is that ??
 
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
Scriptlets are never the proper way of doing anything in a JSP. This is no longer 2001. Have you looked through the EL built-in variables to see if there's anything useful?
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Scriptlets are never the proper way of doing anything in a JSP. This is no longer 2001. Have you looked through the EL built-in variables to see if there's anything useful?



Looked at http://download.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html and EL does have an implicit object called cookie so i tried using it as

<input type="text" name="name" value=${cookie.name} />
but the output was
javax.servlet.http.Cookie@bb2bc3

Any idea on how i could use that implicit object?
 
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
Do you have a cookie named name? If, so then ${cookie.name} resolves to the cookie instance. But that's not what you are after, is it?
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Do you have a cookie named name? If, so then ${cookie.name} resolves to the cookie instance. But that's not what you are after, is it?



The cookie which was sent to the browser was
String name = "Cartman";
Cookie cookie = new Cookie("name", name);
cookie.setMaxAge(3600);
response.addCookie(cookie);

so the name of the cookie is actually "name"
so i did try ${cookie.name} but that doesnt seem to work
 
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
Of course it works. It gives you the cookie instance as evidenced by the output. But is the toString() of the instance what you are after? Think of the cookie instance as a bean.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Of course it works. It gives you the cookie instance as evidenced by the output. But is the toString() of the instance what you are after? Think of the cookie instance as a bean.



Tried using
<td><input type="text" name="name" value=${cookie.name.toString()}>

output is:
javax.servlet.http.Cookie@1dfd90f>
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may want to read up on thus stuff. Try this

${cookie["name"].value}
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bosun Bello wrote:You may want to read up on thus stuff. Try this

${cookie["name"].value}



Yep that did the trick thanks guys...
 
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
Do you understand why that works? And ${cookie.name.value} would be a simpler syntax.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Do you understand why that works? And ${cookie.name.value} would be a simpler syntax.



Yeah ... since in my actual (topmost) example i used

tempCookie.getName().equals(SearchString) //Here i am getting the name and comparing it

since the cookie is a name value pair
cookie.name would just give me the coookie Id i guess

and cooki.name.value would actually give me the value of the "name"

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing a weird problem ... When there are no cookies
The line


just displays "/" in the textbox. Is there any way by which i can make it display nothing since there is nothing to display???
 
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
Quote the value. Always quote attribute values. Just because HTML lets us be sloppy, it's never a good idea to not quote attributes.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Quote the value. Always quote attribute values. Just because HTML lets us be sloppy, it's never a good idea to not quote attributes.


Quote values ?? Sorry you lost me there ?? What does quoting a value mean in this context ??
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Put quotes around all attribute values.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Put quotes around all attribute values.



Thanks for the great tip

<input type="text" name="pass" value="${cookie.pass.value}"

>
 
reply
    Bookmark Topic Watch Topic
  • New Topic