JSP
Top 20 Maven Interview Questions & Answers
Download PDF 1) Explain what is Maven? How does it work? Maven is a project management tool. It...
Expression Language (EL) is mechanism that simplifies the accessibility of the data stored in Java bean component and other object like request, session and application, etc. There are many operators in JSP that are used in EL like arithmetic and logical operators to perform an expression. It was introduced in JSP 2.0
In this tutorial, you will learn-
Syntax of EL :$(expression)
To get a better idea, on how expression works in JSP, we will see below example.
In this example, we will see how EL is used as an operator to add two numbers (1+2) and get the output respectively.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru JSP1</title> </head> <body> <a>Expression is:</a> ${1+2}; </body> </html>
Explanation of Code:
When you execute the above code, you will have the following Output.
Output:
Flow Control Statements:
JSP provides the power of Java to be embedded in the application. We can use all the APIs and building blocks of Java in JSP programming including control flow statements which include decision making and the loop statements.
There are two types of flow control statements described below;
Decision-Making Statements:
Decision-making statement in JSP is based on whether the condition set is true or false. The statement will behave accordingly.
There are two types of decision-making statements described below:
"If else" statement is basic of all control flow statements, and it tells the program to execute the certain section of code only if the particular test evaluates to true.
This condition is used to test for more than one condition whether they are true or false.
Syntax for if – else statement:
If(test condition) { //Block of statements } else { //Block of statements }
In this example,
We are going to test "if else" condition by taking variable and checking the value if the variable matches with what it is initialized:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru JSP2</title> </head> <body> <%! int month=5; %> <% if(month==2){ %> <a>Its February</a> <% }else{ %> <a>Any month other than February</a> <%} %> </body> </html>
Explanation of the code:
When you execute the above code, you will have the following Output.
Output:
The body of the switch statement is called as a "switch block".
Syntax for switch statement:
switch (operator) { Case 1: Block of statements break; Case 2: Block of statements break; case n: Block of statements break; default: Block of statements break; }
In below example,
We have defined a variable week, and it is matched with the case whichever is true.In this case, week is 2 hence 2nd case is matched, and the output is Tuesday:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru JSP3</title> </head> <body> <%! int week=2; %> <% switch(week){ case 0: out.println("Sunday"); break; case 1: out.println("Monday"); break; case 2: out.println("Tuesday"); break; case 3: out.println("wednesday"); break; case 4: out.println("Thursday"); break; case 5: out.println("Friday"); break; default: out.println("Saturday"); } %> </body> </html>
Explanation of Code:
When you execute the above code, you will have the following Output.
Output:
Loop Statements
It is used for iterating the elements for a certain condition, and it has three parameters.
For loop Syntax:
For(inti=0;i<n;i++) { //block of statements }
In this Example,
We have for loop which iterates till counter is less than the given number:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru JSP4</title> </head> <body> <%! int num=5; %> <% out.println("Numbers are:"); for(int i=0;i<num;i++){ out.println(i); }%> </body> </html>
Explanation for the code:
In the body of "forloop", there is out a class of JSP which prints into the output stream using method println where we are printing the variable i.
When you execute the above code, you will have the following Output.
Output:
It is used to iterate the elements wherein it has one parameter of the condition.
Syntax:
While(i<n) { //Block of statements }
In this example,
We have a while loop which will iterate till day is greater than equal to the counter:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru JSP5</title> </head> <body> <%! int day=2; int i=1; %> <% while(day>=i){ if(day==i){ out.println("Its Monday"); break;} i++;} %> </body> </html>
Explanation of the code:
Within that there is "if condition" (day is equal to i) and "if condition" is true then it will print the output stream, and it will exit the while loop else the i variable is incremented and loop iterates.
When we execute the code, we will have the following output
Output is:
JSP Operators supports most of its arithmetic and logical operators which are supported by java within expression language (EL) tags.
Frequently used operators are mentioned below:
Following are the operators:
. | Access a bean property or Map entry |
[] | Access an array or List element |
( ) | Group a subexpression to change the evaluation order |
+ | Addition |
- | Subtraction or negation of a value |
* | Multiplication |
/ or div | Division |
% or mod | Modulo (remainder) |
== or eq | Test for equality |
!= or ne | Test for inequality |
< or lt | Test for less than |
> or gt | Test for greater than |
<= or le | Test for less than or equal |
>= or ge | Test for greater than or equal |
&& or and | Test for logical AND |
|| or or | Test for logical OR |
! or not | Unary Boolean complement |
Empty | Test for empty variable values |
In this example,
These all numbers should be printed out as our output:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru JSP6</title> </head> <body> <% int num1=10; int num2 = 50; int num3 = num1+num2; if(num3 != 0 || num3 > 0){ int num4= num1*num2; out.println("Number 4 is " +num4); out.println("Number 3 is " +num3); }%> </body> </html>
Explanation of the code:
When you execute the above code, you will have the following Output.
Output:
Summary:
Download PDF 1) Explain what is Maven? How does it work? Maven is a project management tool. It...
What is MVC? MVC is an architecture that separates business logic, presentation and data. In MVC, M...
Debugging is the process to trace the error in the application. It is not very easy to trace bugs...
In this tutorial, we are going to study the basics of writing and running a JSP. We will install...
In this example, we are going to learn about uploading and downloading of a file through JSP. File...
JSP Form Processing Forms are the common method in web processing. We need to send information to...