In this tutorial you will learn about the JSP ScriptletTag and its application with practical example.
JSP ScriptletTag
JSP scripting elements : These elements used to insert Java code inside the JSP(Java Server Pages). There are three type of scripting elements:
- Scriptlet Tag
- Expression Tag
- Declaration Tag
- Scriptlet Tag: The Tag From which we can execute java source code which is in JSP. The Syntax of this tag is like below
1 |
<% Your Java Source Code %> |
Let’s get understand it by an example. With the help of below Example we can print our java message on JSP.
1 2 3 4 5 6 7 8 9 10 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <title>JSP Page</title> </head> <body> <% out.print("Hello World");%> </body> </html> |
Write this code on your JSP page and you will get output like below Image.
Now Lets get Understand it by one more example. We are creating two JSP file first one is index.jsp and second is Welcome.jsp. We Will send data from index.jsp to Welcome.jsp and the code will be like below:
index.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <body> <form action="Welcome.jsp"> <pre> UserName : <input type="text" name="uname"> Email : <input type="text" name="Email"> <input type="submit" value="Login"><br/> </pre> </form> </body> </html> |
Welcome.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <body> <pre> <% String name = request.getParameter("uname"); String Email = request.getParameter("Email"); out.println("Welcome " + name+" Email : " + Email); %> </pre> </body> </html> |
And the result of these jsp is like below images:
- Expression Tag: This tag is used to write output of Placed code within it. We don’t need to write out.print() to write output. Most of the time this Tag is used to write the values of variable or methods. The Syntax of this Tag is written like below code
1 |
<% = Statement %> |
Let’s use last example for understanding this Tag.
Welcome.jsp:
1 2 3 4 5 6 7 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <body> <%= "Welcome "+request.getParameter("uname")+" Email : "+request.getParameter("Email")%> </body> </html> |
The output for this expression tag code will look like below Image The index.jsp page output is same like above image and we did not change anything in index.jsp.
- Declaration Tag : From the word it is clear that this tag is used for declaration. Declaration Tag is used to declare fields and methods. The code which is used in declaration tag placed outside service() method of auto generated servlet. It doesn’t get memory at each request.
Syntax of Declaration Tag is:
1 |
<%! field or method declaration %> |
Let’s get understand it by an example. This is the example of Declare Field and printing the value of that field also we have used JSP expression tag for printing the output of declared field.
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <%! int i = 38;%> <%= "Value of the variable is:" + i%> </body> </html> |
The output of above code will look like below image
Now lets do code for declare method. This is the example of Delcare method which will written square of given number and the output is displayed through Expression Tag.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <%! int Squre(int n) { return n * n; } %> <%= "Squre of 3 is:" + Squre(3)%> </body> </html> |
The Output of this code will look like below Image.