Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get absolute URL with EL

Tags:

java

jsp

el

How can I build up an absolute URL using no scriptlets (only EL) to the current server, using the current protocol, port, application etc?

like image 748
Mark W Avatar asked Apr 18 '11 14:04

Mark W


2 Answers

Another way is:

http://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/thePage.jsp
like image 22
Mark W Avatar answered Oct 19 '22 23:10

Mark W


You can get the base URL up to with the context root with help of JSTL as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<c:set var="baseURL" value="${fn:replace(pageContext.request.requestURL, pageContext.request.requestURI, pageContext.request.contextPath)}" />
...
<link rel="stylesheet" href="${baseURL}/foo.css" />
<script src="${baseURL}/foo.js"></script>
<a href="${baseURL}/foo.jsp">link</a>
like image 76
BalusC Avatar answered Oct 19 '22 23:10

BalusC