JSP Expression Tag with Syntax and Examples
Job-ready Online Courses: Dive into Knowledge. Learn More!
Scripting elements form the main part of a JSP Code. They are very essential. Expression tag is one of them. In this article, we are going to learn about JSP Expression Tag with Examples. So let’s start!!
JSP Expression Tag
Expressions are a simple means for accessing the value of a Java variable. They can be an access point to other expressions and merging that value with HTML as well. We write expressions in <%__%> these tags. This code inside these gets written to the output stream of the response.
Syntax of expression is:
<%=expression/statement %>
Now, this expression or statement can be any valid Java statement. This code will convert to out.print() statement so we don’t need to write an exclusive out.print() statement. This will occur when an auto generated servlet will form until and unless the expression is convertible to string format. It basically writes the result to the browser (client side) using the response object.
Difference between Scriptlet and Expression Tag
Scriptlet tag | Expression Tag |
Scriptlet tag doesn’t evaluates a Java expression | Expression tag evaluates a Java expression |
It will not display any result itself until the user displays it. | It directly writes the result to the client side. |
We will have to write out.print() statements. | There is no need to write out.println for printing because these are converted into out.print() statement |
Different examples below show different uses of expressions.
Examples for JSP Expressions
Example 1
exp1.jsp <html> <head><title>Expressions</title></head> </head> <body> <h4>--DataFlair--</h4> <%="Welcome to DataFlair."%><br/> <%="Have a nice day."%> </body> </html>
Explanation: In this example, we have printed simple statements using expression tags. We have not used out.print() statements.
Output:
Example 2
exp2.jsp <html> <head><title>Expressions</title></head> </head> <h4>--DataFlair--</h4> <body> Current time: <%=java.util.Calendar.getInstance().getTime()%> </body> </html>
Explanation: In this example we print the time using expression tags. We print the time using getInstance() method of util.Calendar package of Java
Output:
Example 3
exp3.jsp <html> <head><title>Expressions</title> </head> <body> <h6>--DataFlair</h6> <% String s1="hello, kajal here.";%> <% out.print(s1); %> <br/> <%=s1.toUpperCase() %> </body> </html>
Explanation: In this example, we evaluate a logical expression using expression tag. We convert lowercase to uppercase. We didn’t use any out.print() statement at the time of conversion. The result writes itself to the client side.
Output:
Example 4
exp4.jsp <html> <head><tittle>Expressions</head></title> <body> <form action="exp5.jsp"> Username:<br/> <input type="text" name="username"><br/> DOB:<br/> <input type="text" name="DOB"><br/> Password:<br/> <input type="text" name="password"><br/> <input type="submit" value="go"> </form> </body> </html> exp5.jsp <html> <head><tittle>Expressions</title> <br/> <body> <%= "Welcome "+request.getParameter("username") %> <br/> <%= request.getParameter("DOB") %> </body> </html>
Explanation: In this example, we get user details from the form. We call the file through the action attribute and we print user details using expressions.
Output:
Example 5
exp6.jsp <html> <head><title>Expressions</title></head> </head> <h4>--DataFlair--</h4> <body> <%=(2+4*5)%> </body> </html>
Explanation: In this example, we have used expressions to evaluate an arithmetic expression. Following is the output.
Output:
Conclusion
In reference to this JSP Expression Tag article, we came to know about expression tags. They are an important sub division under scripting elements. They directly write the result to the client side without a printing statement. This feature makes it different from other elements. We then saw various uses of expression tags with examples and explanations.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google