We can use if statement in JavaServer Pages.
The if...else
block works as normal if...else
statement.
We need to use Scriptlet at each line with HTML text included between Scriptlet tags.
The following code shows how to use the if...else statement with Scriptlet tags.
<%! int day = 7; %> <html> <body> <% if (day == 1) { %> <p> is one </p> <% } else { %> <p> not one</p> <% } %> </body> </html>
The following code shows how to use switch...case
block
<%! int day = 3; %> <html> <body> <% switch(day) { case 0: out.println("It\'s Sunday."); break; case 1: out.println("It\'s Monday."); break; case 2: out.println("It\'s Tuesday."); break; case 3: out.println("It\'s Wednesday."); break; case 4: out.println("It\'s Thursday."); break; case 5: out.println("It\'s Friday."); break; default: out.println("It's Saturday."); } %> </body> </html>
The following code shows how to use for
loop.
<%! int fontSize; %> <html> <body> <%for ( fontSize = 1; fontSize <= 3; fontSize++){ %> <font color="red" size="<%= fontSize %>"> JSP Tutorial </font><br /> <%}%> </body> </html>
The following code shows how to use while loop.
<%! int fontSize; %> <html> <head><title>WHILE LOOP Example</title></head> <body> <%while ( fontSize <= 3){ %> <font color="green" size="<%= fontSize %>"> JSP Tutorial </font><br /> <%fontSize++;%> <%}%> </body> </html>