unable to add a cookie included in JSP via jsp:include

别来无恙 提交于 2019-12-05 10:13:10

The <jsp:include> uses under the covers RequestDispatcher#include() and its docs say:

...

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.

...

(emphasis mine)

Cookies are to be set in the response header. So it stops here. Consider the compile time variant <%@include%>, it get literally inlined in the main JSP's source.

Source code:

request.setAttribute(“res”, response);
<jsp:include page=“url” />

Target code:

HttpServletResponse res = (HttpServletResponse)request.getAttribute(“res”);

//cookie create
Cookie cookie = new Cookie(“test”, “test”);

res.addCookie(cookie);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!