aspose file tools*
The moose likes Servlets and the fly likes Cookies in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cookies in servlet" Watch "Cookies in servlet" New topic
Author

Cookies in servlet

Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

Did it work?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
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
Marshal

Joined: Mar 22, 2005
Posts: 42950
    
  72
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

Joined: Jan 03, 2009
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

${x}

<c:forEach> is only for collections.

Nick Price
Greenhorn

Joined: Jan 03, 2009
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

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

Joined: Jan 10, 2002
Posts: 61772
    
  67

If the value of x needs to be HTML-encoded upon display, then:

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


Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

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

Joined: Jan 03, 2009
Posts: 25
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

Joined: Jan 10, 2002
Posts: 61772
    
  67

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

Joined: Oct 08, 2006
Posts: 2458
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'.
 
Consider Paul's rocket mass heater.
 
subject: Cookies in servlet