JSTL Core Tags: URL Tags 

Joined:
08/13/2009
Posts:
110

July 26, 2010 19:18:28    Last update: August 18, 2010 19:13:02
The tags

<c:import>

The <c:import> tag imports the contents of a URL and expose that in one of three ways:
  1. Import contents from a URL and write it out to the page (url may be relative or absolute):
    <c:import url="theUrl" />
    


  2. Import contents from a URL and save it to a scoped variable string named by the var attribute. Use the scope attribute to define the scope of the exported variable.
    <c:import url="theUrl" var="importTest" scope="session"/>
    


  3. Import a URL and expose to a Reader object named by the varReader attribute. The scope attribute does not apply. The varReader scoped 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.

  1. Write URL in page:
    <c:url value="anotherPage.jsp"/>
    


  2. Save URL to a variable in session scope:
    <c:url value="anotherPage.jsp" var="aref" scope="session"/>
    


  3. 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"/>
    


  4. 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.

  1. Redirect to absoute URL:
    <c:redirect url="http://www.google.com"/>
    


  2. Redirect to relative URL:
    <c:redirect url="resultsPage.jsp"/>
    


  3. Redirect to a URL in a different servlet context:
    <c:redirect url="/contact.jsp" context="/support"/>
    


  4. 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:
  1. 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);
        }
    }
    


  2. Create a new JSP (coreurl.jsp) under webapp:
    <%@ 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>&lt;c:import&gt; 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 &lt;c:out&gt;:
        <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>&lt;c:url&gt; 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>&lt;c:redirect&gt; test</h2>
    <c:if test="${param.redirect == 'true'}">
        <c:redirect url="${pageContext.request.requestURL}"/>
    </c:if>
    </body>
    </html>
    


  3. Add a static HTML file to_be_included.html under webapp:
    This content is from "<i>to_be_included.html</i>".
    


  4. Compile and package the WAR with: mvn package

  5. Deploy the WAR to a servlet container of your choice (for example, Tomcat 6).

  6. Test the page with this URL (Tomcat 6 running on port 8080):
    http://localhost:8080/jstl-demo/demo/CoreURL

  7. 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.
Share |
| Comment  | Tags