aspose file tools*
The moose likes JSP and the fly likes What would be a proper way of reading a cookie in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "What would be a proper way of reading a cookie in JSP" Watch "What would be a proper way of reading a cookie in JSP" New topic
Author

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

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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 ??


Don’t look where you fall, but where you slipped
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

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

Joined: Jun 10, 2011
Posts: 124

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

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

Joined: Jun 10, 2011
Posts: 124

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>
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You may want to read up on thus stuff. Try this

${cookie["name"].value}

Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

Do you understand why that works? And ${cookie.name.value} would be a simpler syntax.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

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

Joined: Jun 10, 2011
Posts: 124

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

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

Joined: Jun 10, 2011
Posts: 124

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61774
    
  67

Put quotes around all attribute values.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Bear Bibeault wrote:Put quotes around all attribute values.


Thanks for the great tip

<input type="text" name="pass" value="${cookie.pass.value}"
>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What would be a proper way of reading a cookie in JSP