Search code examples
spring-mvcjspjsessionidjava-resources

Sprint MVC tries to get resources with session id


When I load my Spring MVC site, spring:url appends jsessionid to the resource sources and they will not be found.

This only happens the first time opening it. When reloading the site, it isn't there and the page loads correctly. Also every time, when reloading, even with deleting the caches, the resources load correctly.

I implemented Spring MVC in my project. My resources are located in webapp/resources/core.

My Spring MVC Config handles the resources like this:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("resources/**").addResourceLocations("/resources/");
}

I try to load the JavaScript in my JSP file with spring:url

<spring:url value="resources/core/js/starter.js" var="starterJs"/>
<script src="${starterJs}" type="module"></script>

The first time loading the webpage, the jsessionid gets appended to all the resource paths.
With that, the resources aren't getting loaded and the page has neither any JavaScript nor its CSS.

<script src="resources/core/js/starter.js;jsessionid=F089849888EFF0CAF599C7BFEDC07792" type="module"></script>

After reloading the page, the session id isn't there anymore and the resources get loaded correctly. Even after reloading with [CTRL] + [SHIFT] + r the jsessionid is not added and everything loads as expected.


Solution

  • JSP pages implicitly create a HTTP Session. Initially there is no cookie (the most used way to manage sessions) so it will append the session identifier to the URL.

    You can disable implicit session creation by specifing session="false" in your page directive on top of your JSP>

    <%@ page session="false" %>