Sunday, October 26, 2014

pageContext.request.contextPath returns "/" instead of empty string [FIXED]

Recently I was creating a Spring application hosted on Heroku and realized that ${pageContext.request.contextPath} was returning "/" instead of "" for application deployed on root.

When "" (Empty String) is returned:

Ideally the contextPath should not end with slash "/" because we will be using it in the urls like given below:

 <link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">  

so here url in my local machine would map to something like

 <link href="/my-app-name/resources/css/bootstrap.min.css" rel="stylesheet">  

and when run from the remote server, it should map to something like

 <link href="/resources/css/bootstrap.min.css" rel="stylesheet">  

When "/" is returned:

When the contextPath wrongly returns "/" my urls are wrongly mapped like below (with double slash //):

 <link href="//resources/css/bootstrap.min.css" rel="stylesheet">  

Fix:

After a lot of research, I found the issue. Heroku uses webapp-runner. Version 7.0.34.0 returns "/" as context path and version 7.0.34.1 returns "". So upgrading webapp-runner to 7.0.34.1 solved the issue for me.

No comments:

Post a Comment