Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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" %>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading