JSP Scripting elements
Before JSP Scripting elements you should know What is JSP, As we discussed in the previous tutorial JSP is Java server page, So we can write java code inside JSP file with that we can write HTML code. Java code is written inside this <% java code %> tag and at the time of translation, this java code is processed by JSP engine. and the other text is treated as an HTML code an execute on the browser.
In this tutorial, I am going to discuss what is JSP Scripting Elements and along with we are going to create a web application using JSP.
Following are JSP Scripting elements
- Scriptlet tag in JSP.
- Expression tag in JSP.
- Declaration tag in JSP.
Scriptlet tag in JSP
The Scriptlet tag is used to write Java code inside JSP file. we will write java code inside <% %> this tag Let’s see an example.
<html> <body> <% out.print("welcome to CodeBun.com"); %> </body> </html>
Expression tag in JSP
Expression tag is used to display variable value or print a method. We don’t need to write “out.println”.
We can perform Arithmetic or logical operation inside this tag.
Syntex : <%= expression%>
<html> <head> <title>Expression JSP</title> </head> <% int x = 20 ; %> <body> Page Count is <%= x+5 %> </body>
Declaration tag in JSP
Declaration tag is used to declare a value to the variable but at the same time we can declare value inside the scriptlet tag also but the difference is, At the time of translation the statement of declaration tag will come inside the servlet but outside the service method. And the service method calls on every request so it’s good if you declare all the variable inside the declaration tag.
Syntex is : <%! int a =5; %>
<html> <head> <title>Declaration tag in jsp</title> </head> <%! int x = 20; %> <body> <% out.println(++x); %> </body> </html>
Some Importent point realted JSP Scripting elements.
Java code which is written in <% %> tag moves in _jspService() method.
Java Code which is written is <%! %> moves outside the _jspService() method.
We can call any predefined or user-defined method inside the Expression tag <%= %>.