FOR Loop In JSP

for loop is used to execute the code repeatedly.

FOR Loop In JSP

FOR Loop In JSP

In this section we will read about how to use the for loop in JSP (JavaServer Pages).

In Java programming there are various types of loops are used e.g. while loop, do while loop, for loop. In programming these loops are differentiated by their syntax. Loop is the execution of statements in a repeated manner. Loop statements are executed on the specified condition. These sequence of instructions are executed until the specified condition for executing the statement is not getting false.

For Loop

For loop is a statement for executing the code in repetition manner. It is considered to be as an iteration of statements. Generally, for loop is used for the known number of iteration before entering the loop. For loop in JSP is used as same as it is used in Java i.e. syntax of the for loop is same as its syntax defined for using in core Java.

Syntax of for loop

for(INITIALIZATION; CONDITION; INCREEMENT/DECREEMENT) {
//loop statements;
//'break;' statement can be used to exit early;
}

Example

Here an example is being given which will demonstrate you about how to use for loop in JSP. In this example I will create a JSP page where I will create a multiplication table using the for loop.

Directory Structure of Application

forLoop.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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP For Loop</title>
</head>
<body>
<form action="#">
<p>Enter A Number : 
<input type="text" name="num"/>
</p>
<input type="submit" value="submit"/>
</form>
<%
String n = request.getParameter("num");
if(!(n == null || (n.equals(""))))
{
int num = Integer.parseInt(n);
%>
<p>Multiplication Table of <%=num %> is : </p>
<%
for(int i = 1; i <= 10; i++)
{
%>
<table>
<tr>
<td><%=num %> x <%=i %> = <%=num*i %></td>
</tr>
</table>
<%
}
}
%>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jspForLoop</display-name>
<welcome-file-list>
<welcome-file>forLoop.jsp</welcome-file>
</welcome-file-list>
</web-app>

Output

When you will compile and deploy the above web application on Tomcat 7 you will get the output as follows :

The first page will be opened as follows :

If you will provide the numeric value to the textbox the multiplication table of the specified number will be displayed. For example I have provided '5' in the textbox then the multiplication table of '5' will be displayed as follows :

Download Source Code