Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cookies in servlet

 
Nick Price
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted now to try and add some cookies to my application. I have looked at examples and a lot of them do it through putting the code in their jsp. As i know the purist hate scriplets in jsp, i was looking at adding a cookie into my servlet. So for the time being, i have done this


I am not sure if this is done correctly, but i am hoping the cookie will go too whatever address is forwarded. Is this along the right lines of achieving this?
cheers
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did it work?
 
Nick Price
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles ok, but i am forwarding to a jsp, so this bit dont work


Would it be possible to instantiate the cookie in the jsp, and do print out the amount of visits in the html part?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You need to store "x" as a request attribute in the servlet (e.g., request.setAttribute("x", new Integer(x)) ), and then the JSP can print it.
 
Nick Price
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, like my other servlets then. So i have changed the login part too


So now in my jsp, the only way i know to retrieve the request is in a loop e.g.
<c:forEach var="cookie" items="${x}">

How can i do it out of a loop so i can just print it out once. Can i just do it like
var="cookie" items="${x}"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${x}

<c:forEach> is only for collections.

 
Nick Price
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thats it? I dont have to assign it too a variable or anything? (p.s. I'm not judging your knowledge, because we all know its huge, i just want to get everything done to the way it should be done, as there is nothing worst than getting into bad habits early on)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've used setAttribute() to create x, x is a scoped variable.

Are you talking about scripting variables? Absiolutely not. NO scriptlets in JSPs!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the value of x needs to be HTML-encoded upon display, then:

<c:out value="${x}"/>


 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be sure:
Nick Price wrote:


You've set a request attribute with name "cookie" and as value the 'x' reference.

If you want to access the 'x' reference in JSP, you need to access it by the attribute name.


At least you should use sensible variable names. 'x' certainly isn't. If it is actually an instance of Cookie, then call it at least 'cookie'. You can access it in JSP the usual way (Cookie class has getters as well):
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While x does indeed suck as a variable name, cookie is a poor choice because it will conflict with the cookie builtin EL scoped variable.

P.S. To grab cookie values in a JSP, you can use this builtin variable rather than doing something yourself in the servlet.

P.P.S. Nick, before moving any further, you really need to familiarize yourself with the EL, its expression syntax, its builtin variables, and at least the core JSTL tags.
 
Nick Price
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so pretty much the same as before, and i guess that it printing out this
{JSESSIONID=javax.servlet.http.Cookie@3a9bba, counter=javax.servlet.http.Cookie@1c5ddc9, CyberUser=javax.servlet.http.Cookie@163f7a1} Name: Value:
Is due to it needing to be looped, as i guess that is the memory location.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Price wrote:Is due to it needing to be looped
Where do you keep coming up with this looping? No. Only collections need to be looped.

The EL will emit whatever the toString() method of a bean returns. When no explicit toString() is defined, the default method returns the notation that you see there.

Again, deal with cookies through the builtin variable, not on your own.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:While x does indeed suck as a variable name, cookie is a poor choice because it will conflict with the cookie builtin EL scoped variable.
You're right. I thought that it was called -imho more straightforward- 'cookies'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic