Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
 
Book Must Read
 
 

Loop, Contents Collection, iterator, conditional checks

 
 
Print   Email
 
     
 

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>

 

 

 

 
If You Like this Article, share this

 
 
     
   
  View Forum Post  
 
Optimization of SQL queries for better performance
Example of Between in mysql
Example of sorting in mysql table
Example of join in mysql
Example of subquery in mysql
union join in mysql with example
error too many connection in mysql
calculate second highest salary
How to use select queries with array in mysql
Radio button pre selected in struts
Exception in thread "main" java.lang.NoClassDefFoundError
tree view in jsp
SSL implement in tomcat and java certificate
WARNING: Internal error flushing the buffer in release()
DynaForm validation problem in struts not working
Accessing JavaBean in servlet or servlet in javaBean.
 
     
     
   
     
     
     
 
  Privacy Policy