In the previous article, we have seen the Life Cycle of JSP in detail. In that, we have used certain tags in Example. These Tags are Scripting Tags. In this tutorial, we will cover all the basic Elements used in JSP along with some simple Examples.
JSP Scripting Element
In JSP, We can write Java code inside the JSP page using Scripting Element. The code written in this JSP page is handled by JSP Engine during the Translation of the page. There are basically different types of Scripting Elements.
- Scriptlet Tag
- Expression Tag
- Declaration Tag
- Comment Tags.
Scriptlet tag of JSP
In JSP, Java code can be embedded into JSP using the Scriptlet Tag.
<% Java Code %>
- Here within <% %> Scriptlet tag Java Code is written.
- JSP container translates the statement in the _jspservice() method.
- Just like we do normal servlet code, we embed that code within <%%> tag.
Example of Scriptlet Tag for Finding the Fibonacci Series.
In this example, we are using Scriptlet Tag to insert Fibonacci Code on the JSP page.
<%@ 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>Insert title here</title> </head> <body> <% out.println("<h2>Using Scriptlet Tag<br></h2>"); int a=0; int b=1; int c=0,n=10; out.println(a+" "+b); for(int i=2;i<n;i++) { c=a+b; a=b; b=c; out.println(" "+c); } %> </body> </html>
Output
Expression tag of JSP
In order to print values of variable or method, we used Expression Tags. We don’t need to write print() to write data.
- The expression tag is used to evaluate the expression.
- It allows expression such as arithmetic and logical.
<%= expression %>
Example of an Expression tag that prints the name of the user
In this example, we are using the Expression tag for getting the values from the form.
index.jsp
<%@ 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>Insert title here</title> </head> <body> <form action="Expressiontag.jsp"> <input type="text" name="name"><br> <br><input type="submit" value="Click"> </form> </body> </html>
Expressiontag.jsp
<%@ 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>Insert title here</title> </head> <body> <%= "Hey "+request.getParameter("name") %> </body> </html>
Output
Declaration Tag of JSP
A declaration Tag is used for declaring variables, methods, and classes.
<%! Declare variable, method %>
- When the page is translated into servlet class the code which is present in the declaration tag is placed outside the service() method.
- We declare a static variable, instance variable in the Declaration tag.
Example of Declaration tag for the area of Square
In this example, we are using the Declaration tag for a method of areaOfSquare()
<%@ 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>Insert title here</title> </head> <body> <%! int areaOfSquare(int r) { return r*r; } %> <%= "Area of Square is "+areaOfSquare(5) %> </body> </html>
Output
Comments in JSP
Comments are the one which is ignored and is used to hide the content of code. Comments are ignored by the container.
<% -- Comments %>
Note: If anybody wants to add a comment just select the code and press ctrl +shift+/?
Thus we have seen Elements like expression tags, Scriptlet tags, Declaration tags in detail along with Comment tag.
In the next tutorial, we will see the implicit objects that are created during the translation phase such as to request, response, config, etc.