Forums Register Login

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

+Pie Number of slices to send: Send
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 ??
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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>
+Pie Number of slices to send: Send
You may want to read up on thus stuff. Try this

${cookie["name"].value}
+Pie Number of slices to send: Send
 

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

${cookie["name"].value}



Yep that did the trick thanks guys...
+Pie Number of slices to send: Send
Do you understand why that works? And ${cookie.name.value} would be a simpler syntax.
+Pie Number of slices to send: Send
 

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"

+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
Quote the value. Always quote attribute values. Just because HTML lets us be sloppy, it's never a good idea to not quote attributes.
+Pie Number of slices to send: Send
 

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 ??
1
+Pie Number of slices to send: Send
Put quotes around all attribute values.
+Pie Number of slices to send: Send
 

Bear Bibeault wrote:Put quotes around all attribute values.



Thanks for the great tip

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

>
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3863 times.
Similar Threads
Cookies example in HFSJ
Cookie Problem
HTML display
Memory leak - Buffer size problem?
if( I can't figure this out....)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:56:46.