|
JSP provides loop and conditional checks. Loop provides us repetition of content without writing again and again same content.
Loop in JSP can be for, while, do while
Look examples of these loops for loop in JSP
Example of for loop in JSP
for.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>For loop in JSP</title>
</head>
<body>
<%
for(int i=0;i<=10;i++)
{
out.print("Loop through JSP count :"+i+"<br/>");
}
%>
</body>
</html>
Example of while loop in JSP
while.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>while loop in JSP</title>
</head>
<body>
<%
int i=0;
while(i<=10)
{
out.print("While Loop through JSP count :"+i+"<br/>");
i++;
}
%>
</body>
</html>
Example of do-while loop in JSP
doWhile.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>do-while loop in JSP</title>
</head>
<body>
<%
int i=0;
do{
out.print("While Loop through JSP count :"+i+"<br/>");
i++;
}
while(i<=10);
%>
</body>
</html>
Examples of conditional and unconditional checks in JSP
Conditional check means check condition and if it satisfies condition then block below execute otherwise block will not execute. Unconditional checks those check that don’t care about condition and execute in every case.
Conditional checks are if, if-else, while. These condition use conditional operator like < > or && or || (greater than, less than, and, or condition)
Example of if-else condition
ifelse.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>while loop in JSP</title>
</head>
<body>
<%
String sName="joe";
String sSecondName="noe";
if(sName.equals("joe")){
out.print("if condition check satisfied JSP count :"+sName+"<br>");
}
if(sName.equals("joe") && sSecondName.equals("joe"))
{
out.print("if condition check if Block <br>");
}
else
{
out.print("if condition check else Block <br>");
}
%>
</body>
</html>
|