Currently I am using
${pageContext.servletContext.contextPath}
or
${pageContext.request.contextPath}
to print the context path on the JSP page. It is working fine for the local server, but when I go live it prints blank instead of the actual path.
I am able to print the request port number as well as request method, but not the context path.
How is this caused and how can I solve it?
That will happen when it's deployed to root. E.g. as ROOT.war
in Tomcat, or having a context path of /
. The ${pageContext.request.contextPath}
will then resolve to an empty string.
If you're actually seeing a "context path" in browser's address bar URL, then it can happen when the webapp is published to a (poor) 3rd party host which in turn actually deploys the webapp as root and adds that "context path" to some proxy server on that host. You could check/confirm that by checking the Server
entry in the response headers. You'd best contact the 3rd party host for clues about how to obtain information about that "context path" from the proxy server (perhaps it's present in a request header), or look for a different 3rd party host which is less el cheapo.
来源:https://stackoverflow.com/questions/30443205/pagecontext-request-contextpath-works-fine-local-but-shows-blank-live