JSP jsessionid appear in CSS and JS link
By:Roy.LiuLast updated:2019-08-17
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
Note
Read this – Under what conditions is a JSESSIONID created?
Read this – Under what conditions is a JSESSIONID created?
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
From:一号门
COMMENTS