I showed you how to get the base URL in the Controller of Spring MVC and Spring Boot application. To do this in the JSP page, we will need to use the pageContext object of the Java Servlet.
Roughly speaking about this pageContext object, it is a hidden object of class javax.servlet.jsp.PageContext, used to represent the entire JSP page. We can get the object of the HttpServletRequest class from this pageContext object and from there we will get the base URL information.
Please see the following code snippet:
1 2 3 4 5 6 7 |
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <c:set var="scheme" value="${pageContext.request.scheme}"/> <c:set var="serverName" value="${pageContext.request.serverName}"/> <c:set var="serverPort" value="${pageContext.request.serverPort}"/> Base URL: ${scheme}://${serverName}:${serverPort} |
Very simple, easy to understand, right?
Result: