Main Tutorials

JSP – jsessionid appear in CSS and JS link

In Spring MVC + JSP view page environment.

index.jsp

<html>
  <head>
  <title>Welcome!</title>
  
  <c:url var="assets" value="/resources/abc" />
  <link href="${assets}/css/style.min.css" rel="stylesheet">

  <script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />">
  </script>
  
  </head>
  ...
</html>

In the Spring config file, mapped a resource path,

mvc-dispatcher-servlet.xml

<beans ...
	<context:component-scan base-package="com.mkyong.test" />

	<mvc:resources mapping="/resources/**" location="/resources/" />
</beans>

1. Problem

After deployed, Spring MVC can’t get the CSS and JS resource files, and display resources not found error. Review the generated index.jsp page :

index.jsp

<html>
  <head>
  <title>Welcome!</title>
  
  <link href="/resources/simpliq;jsessionid=2957A...5C8DA/css/style.min.css" 
    rel="stylesheet">
  
  <script src="/resources/js/jquery.1.10.2.min.js;jsessionid=2957A...5C8DA">
  </script>
  </head>
  ...
</html>

The jsessionid is appended as a parameter in the CSS and JS url?

2. Solution

To solve this, turn off the page session in JSP page – @page session="true"

index.jsp

<%@page session="true"%>
<html>
	<head>
	<title>Welcome!</title>

	<c:url var="assets" value="/resources/abc" />

	<link href="${assets}/css/style.min.css" rel="stylesheet">
	<script src="<c:url value="/resources/js/jquery.1.10.2.min.js" />"></script>

	</head>
	...
</html>

OR use ${pageContext.request.contextPath} , instead of this c:url

index.jsp

<html>
  <head>
  <title>Welcome!</title>
	
  <link href="${pageContext.request.contextPath}/resources/css/style.min.css" 
    rel="stylesheet">
  
  <script src="${pageContext.request.contextPath}/resources/js/jquery.1.10.2.min.js" />">
  </script>
  
  </head>
  ...
</html>

References

  1. Stackoverflow : Under what conditions is a JSESSIONID created?
  2. Wikipedia – Session ID

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Guest
4 years ago

To disable the session, you should use
@page session=”false”

there was a typo in the article.

YsYau
8 years ago

it should be but not