简体   繁体   中英

Java Servlets - set or alter cookies in included servlet

Given that we have two servlets A and B and servlet A includes servlet B using

request.getRequestDispatcher("/B").include(request,response);

Why can't I set or alter cookies in servlet B? I don't receive any errors but it seems like the changes on the cookies are being ignored. Can anyone explain this kind of behaviour?

According to RequestDispatcher.include documentation :

.....

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers ; any attempt to make a change is ignored .

.....

And since Cookies are part of the returned header (of the calling Servlet), that's why the changes are not taking effect (ignored).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM