Kodejava website provides Java examples to use the Java API (Application Programming Interface) to build Java applications. In this website you will find a lot of examples grouped by the Java API packages or libraries.

Category Archives: Taglib and JSTL

The <jsp:forward/> tag forward user request to other page. For example, a user request page1.jsp and in this page the server found a <jsp:forward page="page2.jsp"/>. The server immediately stop the processing of page1.jsp and jump to the page2.jsp.

Let see an example of using <jsp:forward/> tag.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

<!--Thisispage1.jsp-->

<%@pagecontentType="text/html;charset=UTF-8"language="java"%>

<html>

<head>

<title>Page1</title>

</head>

<body>

<strong>Thisispage1</strong>

<jsp:forward page="page2.jsp"/>

</body>

</html>

<!--Thisispage2.jsp-->

<%@pagecontentType="text/html;charset=UTF-8"language="java"%>

<html>

<head>

<title>Page2</title>

</head>

<body>

<strong>Thisispage2</strong>

</body>

</html>

When you try to run the example above by accessing the URL http://localhost:8080/page1.jsp you are going to see the content of page2.jsp instead of page1.jsp. It’s happen because on the server side page1.jsp forward your request to the page2.jsp. But if you look at your browser URL address it will still pointing to page1.jsp.

The <jsp:include/> tag is use to include another page fragment of a JSP page into another page. This is useful when you have a common page such as header, footer or a menu that applied to many of all of your pages.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<%@pagecontentType="text/html;charset=UTF-8"language="java"%>

<html>

<head>

<title><jsp:include/>Demo</title>

</head>

<body>

<div id="header">

<jsp:include page="common/header.jsp"/>

</div>

<div id="main">

Lorem ipsum dolor sit amet,consectetur adipisicing elit,

sed doeiusmod tempor incididunt ut labore et dolore magna

aliqua.

</div>

<div id="footer">

<jsp:include page="common/footer.jsp"/>

</div>

</body>

</html>

Here are the page fragment of the header.jsp, footer.jsp and menu.jsp. All of them are placed in the common folder in the same location with the index.jsp file.

The c:forEach tag in the core JSTL tag library is a useful tag when we want to iterate over a collection of data such as array. It is commonly use to render a tabular data in our web pages in form of HTML table.

In the example below we display a weather data that we stored as two dimensional array of string. After declaring and initializing the data with some value we put it into the request scope. Later on the c:forEach tag can use the data, iterates it row by row to form an HTML table. Our weather data consist of the date, condition and the high and low temperature.