URL in JSP Core Tag Library
What is "url" tag in JSP Core Tag Library?
Explanation
The
url tag of the core tag library can be used to apply appropriate url encoding and coversion rules for relative urls. This tag is also used to rewrite a url by adding the session ID only if the browser does not support cookies.
Syntax:
<c:url value="url" [context="externalContextPath"]
[var="var"] [scope="page|request|session|application"] />
In the above example the "var" attribute stores the url with its scope in "scope" attribute. The "context" attribute is used to add a context name prepended to the url. Example :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<body>
<form method="POST">
<input type="text" name="url" value="/coreurleg.jsp"/>
<input type="text" name="txt"/></td>
</tr>
<input type="submit" value="Submit" name="submit" />
</form>
<c:if test="${pageContext.request.method=='POST'}">
<hr>
<c:url value="${param.url}" var="url">
<c:param name="txt" value="${param.txt}"/>
</c:url>
<br/><b>The Rewritten URL is:</b>
<c:out value="${url}"/>
</c:if>
<hr>
</body>
</html>
Result:
The Rewritten URL is: /test/coreurleg.jsp?txt=result
In the above example we have used just the relative url of the file "coreurleg.jsp", by using the "<c:url;" tags "<c:param" attribute we get complete url, along with the text entered in the page as "/test /coreurleg.jsp?txt=result."