JSP Control Statements

There are various standard programming languages like C, C++, Java etc. that supports control statements. On the basis of that, JSP also follows the same methodology. Let's study the flow of control statements in Java Server Pages. Here, are some conditional statements about which we will discuss: -

  • if else
  • for loop
  • while loop
  • switch

IF...ELSE

JSP allows to use IF ELSE conditional statement as a programming logic of scriptlet tag. Apart from that, IF can also be used in many other ways like nested IF or standalone IF. Here is a simple example of IF ELSE statement.

index.jsp 

<%! int num=8; %> <% if(num%2==0) { out.println("Number is even"); } else { out.println("Number is odd"); } %>

For Loop

Here, is a simple example of for loop.  

<% for(int i=0;i<5;i++) { for(int j=1;j<=i+1;j++) { out.print(j); } out.print(""); } %>

While loop

Here, is a simple example of while loop.  

<% int i=0; while(i<4) { i++; out.print(i+" "); } %>

Switch Statement

Switch is used to maintain the flow of control statement. It contains various cases with one default case. The default case will execute only once, when none other case satisfies the condition. Here, is a simple example of switch statement.

index.jsp  

<%! int weekday=4; %> 
<% switch(weekday) { case 1: out.print("Monday"); break; 
case 2: out.print("Tuesday"); break; 
case 3: out.print("Wednesday"); break; 
case 4: out.print("Thursday"); break; 
case 5: out.print("Friday"); break; 
case 6: out.print("Saturday"); break; 
default: out.print("Sunday"); break; } %>

Related Topics

JSP Directives

In JSP, the role of directive is to provide directions to the JSP container regarding the compilation of JSP page. Directives convey information from JSP page to container that give...

1 minute read.

JSP exception

Like Java, exceptions can also be occurred in JSP. To handle these exceptions, JSP exception object is used. Handling exceptions in JSP using exception object is much easier approach. This...

1 minute read.

JSP Control Statements

There are various standard programming languages like C, C++, Java etc. that supports control statements. On the basis of that, JSP also follows the same methodology. Let's study the flow...

2 minutes read.

Scripting in JSP

JSP scripting provides an easy and efficient way to insert Java programming language in JSP pages. Here, Java is a default language. JSP also allows you to use any other...

1 minute read.

JSP EL Implicit Objects

Apart from operators, JSP also provide expression language implicit objects. The role of these implicit objects is to get the attribute associated with various object. Here, is the list of...

1 minute read.

JSTL XML Tags

In JSTL, XML tags are used for creating and manipulating xml documents. The following syntax represent the URL of XML tag: - <%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> To utilize the functionality of...

1 minute read.

JSP Request

The request object is used to fetch the user's data from the browser and pass this data to the server. The working of JSP request object is similar to the...

1 minute read.

JSP Expression Language

JSP introduced the concept of expression language (EL) in JSP 2.0 version. The purpose of expression language is to access and manipulate data easily without using any type of scriptlet. Why...

1 minute read.

JSP Custom Tag

JSP custom tag provides flexibility to programmers, to declare their own tag. Thus, a user-defined tag can be created through custom tags to perform various operations. These custom tags behave as...

2 minutes read.

JSP Expression Tag

JSP expression tag allows you to place the result of Java expression in a convenient way. It can be seen as an alternative of out.print() method. Thus, if we want to...

1 minute read.

JSP application

JSP application object is an instance of servlet's ServletContext interface. It is used initialize parameter of one or more JSP pages in an application. Thus, the scope of application object...

1 minute read.

JSP Cookie Handling

Cookie is a small piece of information sent by server to recognize the user. This information is stored at client side in the textual form. In JSP, cookie is an object...

1 minute read.

JSP pageContext

The object pageContext is used to access all the information of JSP page. It is an instance of javax.servlet.jsp.PageContext. The scope of pageContext can be defined explicitly. The object is valid...

2 minutes read.

JSTL Function Tags

In JSTL, the purpose of function tags is similar to the various string methods in Java. Hence, these tag performs common string manipulation operations. Functional tag is represented by following URL:...

2 minutes read.

JSP Implicit Objects

In JSP, implicit objects are pre-defined objects, created by the web container. These objects are created during the translation phase from JSP to Servlet. JSP allows you to call these...

1 minute read.

JSTL Core Tags

In JSTL, core tags are most frequently used tags that provides variable support, manages URL and control the flow of JSP page. To use JSTL it is required to associate...

2 minutes read.

JSTL - Java Standard Tag Library

Java Standard tag library (JSTL) is a collection of various type of JSP tags. Each tag control the behavior of JSP page and perform specific task such as database access,...

1 minute read.

JSP page

In JSP, the purpose of page object is to create the servlet instance. The role of page object is similar to this keyword in Java. The page object represents the entire...

1 minute read.

JSP forward and include tag

In JSP, forward and include tags are the most frequently used action tags. Unlike Servlets, there is no need to create object of RequestDispatcher interface to use the functionality of...

1 minute read.

JSP config

JSP config object is used to send the configuration information to JSP page. Here, config is an instance of servlet's ServletConfig interface. In this case, the information is send to...

1 minute read.