File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • 0
  • Mark post as helpful
  • send pies
  • 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 ??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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>
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put quotes around all attribute values.
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • 0
  • Mark post as helpful
  • send pies
  • 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}"
>
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic