20 - JSP Standard Tag Library (JSTL): Page 2 of 7

20.3.2 <c:import> 

The <c:import> tag this tag is used to include other resources and is similar to <jsp:include> or @include directive.. The syntax and attributes are as follows:

<c:import var="variable_name" url="relative_url"/ scope=”[page | application |request| session]”>on |request| session]”>

· url – is mandatory attribute and its value is specified as url of resource to be included

· var- is an optional attribute. If we add var attribute then the this variable stores the content of the resource mentioned in url attribute and content will not be included on page until added explicitly. If this attribute is not defined then the content of the url is included on the page.

· scope- is an optional attribute and specifies the scope of variable defined with var attribute. Possible values are page, request, session and application.

Example-

Lets create header.jsp which will be imported in cimport.jsp using <c:import> tag like below

header.jsp

<html>
  <head>
    <title> HEADER </title>
  </head>
  <body>
    <h3>
    This is header page and will be included in other pages. 
    </h3>
  </body>
</html>

cimport.jsp

<html>
  <head>
    <title> c:import tag example </title>
  </head>
  <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <c:out value="This is the example of c:import tag "/>
    <br/>
    <c:importurl="header.jsp"></c:import>
    <br/>
    <c:importvar="importedContent"url="header.jsp"></c:import>
    <c:out value="${importedContent}"escapeXml="false"/>
  </body>
</html>

Access cimport.jsp by hitting http://localhost:8080/jsp-tutorial/cimport.jsp

20.3.3 <c:redirect> 

The <c:redirect> tag is used to redirect request to another resource or URL.. The syntax and attributes are as follows:

<c:redirect url=”relative_rl" context=”/ another web application context root”/>ion context root”/>

· url- is the mandatory attribute and it specifies the value of Url to be redirected.

· context- is an optional attribute and it can have value of context of another web application running on same server prepend with / . This is useful if we need to redirect to page of another web application running on same server.

Note: In URL tags, when the "context" attribute is specified, values of both "context" and "url" must start with "/"

Example-

Lets create credirect.jsp which will redirect to Google.com using <c:redirect> tag like below

<html>
  <head>
    <title> c redirect tag Example </title>
  </head>
  <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core" %>
  <body>
    <c:redirecturl="http://www.google.com"></c:redirect>    
  </body>
</html>

Access credirect.jsp by hitting http://localhost:8080/jsp-tutorial/credirect.jsp . You will see that page is redirected to google.com

Like us on Facebook