About Question enthuware.jwpv6.2.1248 :

Moderator: admin

Post Reply
tanzwud
Posts: 19
Joined: Mon Sep 01, 2014 9:45 pm
Contact:

About Question enthuware.jwpv6.2.1248 :

Post by tanzwud »

Hello. Why the option 1 is not correct?
as soon as I know there is no need to add cookie to response if value is changed. Never had a problem with code like this

Code: Select all

Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("some_name")) {
                    cookie.setValue("some value");                    
                }

            }
        }        
and to test we can forward a request to test without resp.addCookie(c)

Code: Select all

 RequestDispatcher view = req.getRequestDispatcher("cookieresult.jsp");
        view.forward(req, resp);
The following code will change the value of the cookie as well as resp.addCookie(cookie); will do the same. setMaxAge(aTime) has to work same way as soon as 0 value is set.
Am I missing something?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1248 :

Post by admin »

Setting the age on the cookie object from the request will not work unless you add the cookie back to the response. Otherwise the browser will not know what changes you made to the cookie object on the server side.
If you like our products and services, please help us by posting your review here.

tanzwud
Posts: 19
Joined: Mon Sep 01, 2014 9:45 pm
Contact:

Re: About Question enthuware.jwpv6.2.1248 :

Post by tanzwud »

My knowledge is limited about the cookies.
Where to find information about such behaviour ?
Java Doc and servlet spec says nothing about explicit set cookie back to response all what need to do to setMaxAge. The reason why I'm asking. I try option 1 and 3. In both cases I'm able to retrieve information about the cookies from request using Tomcat&Glassfish with time set to 0.

setMaxAge(int expiry)
"A zero value causes the cookie to be deleted." Which mean for me this will delete cookie immidately.
I try to find inforamtion about explicit set HttpServletResponse.addCooke(cookieWithNewTime) but there is no explanation why it has to be done unfortunatelly.

About browser. If we using setValue the browswer know about changes without need to set cookie back to request, why the behaviour is different with setMaxAge and no information in java doc?
And The question is not asking about remove cookie form the browser , only how to remove the cookie from the request.
Thanks for help

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1248 :

Post by admin »

Yes, zero value causes the cookie to be deleted. But you are interpreting it incorrectly. It will be deleted when the browser comes to know that you've set the value to zero. How will the browser come to know? Request object doesn't go back to the browser. Response object does.

Just by removing a cookie from the request will not remove it. It will come back in the next request. So you have to make the browser removing the cookie so that it will be removed.

I am sorry, but I can't point out any statement from the specification about this but I don't think any explicit statement is required in this case.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests