for loop is used to execute the code repeatedly.


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

//loop statements;
//'break;' statement can be used to exit early;


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


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP For Loop</title>
<form action="#">
<p>Enter A Number : 
<input type="text" name="num"/>
<input type="submit" value="submit"/>
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++)
<td><%=num %> x <%=i %> = <%=num*i %></td>


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">


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