JSTL Core Tag import Example

JSTL Core Tag import Example


Posted in : Java Posted on : April 1, 2012 at 7:35 PM Comments : [ 0 ]

In this tutorial you will learn about the JSTL core URL management import tag.

JSTL Core Tag import Example

In this tutorial you will learn about the JSTL core URL management import tag.

When there is a need for managing the URL we have an option to choose either the jsp:include element or JSTL core import tag. When we use the jsp:include element it may includes the both static and dynamic resources of the current web application but, when we uses the import tag we may use all the functionalities of jsp:include element as well as it can includes the resources which are aside from the current web application. import tag is used for retrieving either an absolute or relative URL.

Attributes of import tag :

An import tag may have the following attributes

  • url : This is an required attribute which is used for retrieving and import the url into the page.
  • context : This attribute is used for specifying the web application which is followed by '/'. Default value of this attribute is a current application.
  • charEncoding : This is a character set attribute which is used for the data that are being imported. Default value of charset is ISO-8859-1.
  • var : This attribute is used for specifying the imported text name by using which the imported text will be stored.
  • scope : This attribute specifies the scope for the imported text variable name into which it will be stored. The default value of scope is 'page'.
  • varReader : This attribute is used for specifying an another variable name to make accessible java.io.Reader.

Example :

An example is being given below will demonstrate you how to use the <c:import> tag in JSTL. In this example I have used the <c:import> tag and provides the url exists in the same context that I want to retrieve and also specified a name of variables into which I will stored the imported text. As an output this example will include the content of JstlCoreTagForEachExample.jsp page.

JstlCoreTagForEachExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JSTL Core Tag forEach Example.</h3>
<%
List list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
request.setAttribute("list", list);
%>
Elements list = 
<c:forEach var="i" items="${list}">
${i}
</c:forEach>
</body>
</html>

JstlCoreTagImportExample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSTL Example</title>
</head>
<body>
<h3>JSTL Core Tag import Example.</h3>
Following text are of the imported page :
<c:import url="JstlCoreTagForEachExample.jsp" var="frEach" />
${frEach}
</body>
</html>

How to Run this example

Here I am using an IDE Eclipse so I am giving the process of executing this example in perspective of Eclipse. Before executing this example you will have needed to add the following jar files :

  • jstl.jar
  • standard.jar

After adding of these jar files you may execute your program in the following ways :

  • Select JstlCoreTagImportExample.jsp page of your project in Project Explorer -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • On the Eclipse Editor go to your JstlCoreTagImportExample.jsp -> RightClick -> Run As -> Run On Server -> Choose your server -> Finish.
  • Go to Run button look at the toolbar in green color and click -> Choose your server -> Finish.
  • A simplest way to execute the example in Eclipse is to use the CTRL+F11 key -> Choose your server -> Finish

NOTE : In all of the above execution processes you may start the server first and stop the server each time after the execution if not, each time you will may prompted to a dialog box to Restart the server in Eclipse.

Output :

When the execution process will be completed successfully an output will be displayed on your eclipse browser as :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics