When your first JSP page contains lots of includes, the session cookie might not be sent back when using Wildfly
We are using Spring Session 2.0.6.RELEASE and wildfly 10.1.0.RELEASE. We have implemented Spring session using JDBC persistence. What we see in one of our applications is that the session cookie is not returned to the browser when the JSP page includes lot of jsp:include. What internally happens is that Wildfly will call flushBuffer (in fact this is org.apache.jasper who's calling this). This will internally fire up the filter OnCommittedResponseWrapper. This will fire the commitSession which will try to write the cookie.
Below a screenshot of the stack trace:

But... in the HttpServletResponse implementation of undertow (used as web container within Wildfly), the cookie will NOT be written to the response when the JSP engine is busy treating an include file. This is the code of undertow:
@Override
public void addCookie(final Cookie cookie) {
if (insideInclude) {
return;
}
final ServletCookieAdaptor servletCookieAdaptor = new ServletCookieAdaptor(cookie);
if (cookie.getVersion() == 0) {
servletCookieAdaptor.setVersion(servletContext.getDeployment().getDeploymentInfo().getDefaultCookieVersion());
}
exchange.setResponseCookie(servletCookieAdaptor);
}
As we can see when the insideInclude boolean is true, the cookie will not be added to the response.
I have created a sample project to reproduce it and I attach it to this ticket. You can simply run the docker-compose file (docker-compose up) and it will create a Wildfly app and a Postgresql container to store the session (note that probably the issue is the same with another type of session persistence.). When you call http://localhost/test/main and then later on http://localhost/test/main2 you see that the page could not retrieve the object 'test' from the session (value is empty). Now when you close your browser and restart but then first by calling http://localhost/test/main3 and then later on http://localhost/test/main2, you now see the value for the test object retrieved from the session. This is because on main3, the cookie was correctly set on the first call since the page is very small. Here the zip file with the project: test-spring-session.zip
Please also note that this is done on purpose by undertow team: http://lists.jboss.org/pipermail/undertow-dev/2015-June/001265.html
I think we need to wrap the HttpServletRequest (similar to how we wrap the HttpServletResponse) and mark the response as committed just before an include is done.
Related https://github.com/spring-projects/spring-security/issues/5499
Closing is favor of PR #1243.
@ovanekem This is now fixed in master and 2.0.x thanks to @jzheaux. If you could give 2.1.2.BUILD-SNAPSHOT or 2.0.8.BUILD-SNAPSHOT a spin, that would be highly appreciated.