JSTL Core Tags: URL Tags
July 26, 2010 19:18:28 Last update: August 18, 2010 19:13:02
The tags
The
The
The
The
Test it
Make these additions to the expanded test application:
<c:import>
The
<c:import> tag imports the contents of a URL and expose that in one of three ways:
- Import contents from a URL and write it out to the page (url may be relative or absolute):
<c:import url="theUrl" />
- Import contents from a URL and save it to a scoped variable string named by the
varattribute. Use thescopeattribute to define the scope of the exported variable.
<c:import url="theUrl" var="importTest" scope="session"/>
- Import a URL and expose to a
Readerobject named by thevarReaderattribute. Thescopeattribute does not apply. ThevarReaderscoped variable can only be accessed within the body of<c:import>.
<c:import url="theUrl" varReader="theReader"/>
<c:url>
The
<c:url> tag constructs a URL and writes it out to the page or exports it as a scoped variable. The URL may be absolute (starting with a scheme, e.g., http://...), or relative. Relative URLs within the same servlet context are rewritten with session info, other URLs are not rewritten.
- Write URL in page:
<c:url value="anotherPage.jsp"/>
- Save URL to a variable in session scope:
<c:url value="anotherPage.jsp" var="aref" scope="session"/>
- Contruct URL for another servlet context (but same host), save it to a variable in request scope (both url and context must start with "/" in this case):
<c:url value="/anotherPage.jsp" context="/anotherContext" var="aref" scope="request"/>
- Construct URL with parameter and save it to a variable in page scope (default):
<c:url value="/anotherPage.jsp" var="aref"> <c:param name="param1" value="value1"/> </c:url>
<c:redirect>
The
<c:redirect> sends an HTTP redirect to the client. The URL may be absolute (starting with a scheme, e.g., http://...), or relative. Relative URLs are redirected to the same servlet context, unless the context attribute is specified.
- Redirect to absoute URL:
<c:redirect url="http://www.google.com"/>
- Redirect to relative URL:
<c:redirect url="resultsPage.jsp"/>
- Redirect to a URL in a different servlet context:
<c:redirect url="/contact.jsp" context="/support"/>
- Redirect to a URL with parameter:
<c:redirect url="query.jsp"> <c:param name="term" value="jsp tutorial"/> </c:redirect>
<c:param>
The
<c:param> tag is nested in the bodies of the above tags. Examples are given above.
Test it
Make these additions to the expanded test application:
- Create a new Java class
CoreURL:package jstl.demo.handler; import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; import jstl.demo.DemoHandler; public class CoreURL implements DemoHandler { public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { RequestDispatcher d = req.getRequestDispatcher("/coreurl.jsp"); d.forward(req, resp); } }
- Create a new JSP (
coreurl.jsp) underwebapp:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%> <html> <head> <title>JSTL Core Tags: URL</title> <style type="text/css"> body { font-family: sans-serif; } td { font-size: 0.9em; font-style: italic; } </style> </head> <body> <h2><c:import> test</h2> <ul> <li> Write out to page: <c:import url="to_be_included.html"/> </li> <li> Read to String variable and write out with <c:out>: <c:import url="to_be_included.html" var="importTest" scope="session"/> <c:out value="${importTest}" escapeXml="false"/> </li> <li> Read RSS feed to Reader variable and parse with JSTL: <c:import url="http://slashdot.org/firehose_recent.rss" varReader="rssReader"> <x:parse var="feed" doc="${rssReader}"/> <table cellspacing="0" cellpadding="4"> <x:forEach var="story" select="$feed/*[local-name()='RDF']/*[local-name()='item']"> <tr><td><x:out select="*[local-name()='title']"/></td></tr> </x:forEach> </table> </c:import> </li> </ul> <h2><c:url> test</h2> <ul> <li> Write URL in page: <c:url value="anotherPage.jsp"/> </li> <li> Save URL to a variable in session scope: <c:url value="sessionUrl.jsp" var="aref" scope="session"/> <c:out value="${sessionScope.aref}"/> </li> <li> Construct URL for another context: <c:url value="/c2Page.jsp" context="/c2"/> </li> <li> Construct URL with parameters: <c:url value="/paramDemo"> <c:param name="param1" value="value1"/> </c:url> </li> </ul> <h2><c:redirect> test</h2> <c:if test="${param.redirect == 'true'}"> <c:redirect url="${pageContext.request.requestURL}"/> </c:if> </body> </html>
- Add a static HTML file
to_be_included.htmlunderwebapp:This content is from "<i>to_be_included.html</i>".
- Compile and package the WAR with:
mvn package
- Deploy the WAR to a servlet container of your choice (for example, Tomcat 6).
- Test the page with this URL (Tomcat 6 running on port 8080):
http://localhost:8080/jstl-demo/demo/CoreURL
- Test URL redirect with:
http://localhost:8080/jstl-demo/demo/CoreURL?redirect=true
Notice the URL change in the address bar.
You may adjust the URL if your servlet container runs on a different port or the web app is bound to a different context root.