Scriptlet Tag
Scriptlet Tag allows you to write java code inside JSP page. Scriptlet tag implements the
_jspService
method functionality by writing script/java code. Syntax of Scriptlet Tag is as follows :<% java code %>
Example of Scriptlet
In this example, we will show number of page visit.
<html> <head> <title>My First JSP Page</title> </head> <% int count = 0; %> <body> Page Count is <% out.println(++count); %> </body> </html>
We have been using the above example since last few lessons and in this scriptlet tags are used. Everything written inside the scriptlet tag is compiled as java code. Like in the above example, we initialize
count
variable of type int
with a value of 0. And then we print it while using ++
operator to perform addition on it.
JSP makes it so easy to perform calculations, database interactions etc directly from inside the HTML code. Just write your java code inside the scriptlet tags.
Example of JSP Scriptlet Tag
In this example, we will create a simple JSP page which retrieves the name of the user from the request parameter. The index.html page will get the username from the user.
index.html
<form method="POST" action="welcome.jsp"> Name <input type="text" name="user" > <input type="submit" value="Submit"> </form>
In the above HTML file, we have created a form, with an input text field for user to enter his/her name, and a Submit button to submit the form. On submission an HTTP Post request (
method="POST"
) is made to the welcome.jsp file ( action="welcome.jsp"
), with the form values.
welcome.jsp
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome Page</title> </head> <% String user = request.getParameter("user"); %> <body> Hello, <% out.println(user); %> </body> </html>
As we know that a JSP code is translated to Servlet code, in which
_jspService
method is executed which has HttpServletRequest and HttpServletResponse as argument. So in the welcome.jsp file, request is the HTTP Request and it has all the parameters sent from the form in index.html page, which we can be easily get using getParameter()
with name of parameter as argument, to get its value.Mixing scriptlet Tag and HTML
Let's see how we can utilize the power of JSP scripting with HTML to build dynamic webpages with help of a few examples.
If we want to create a Table in HTML with some dynamic data, for example by reading data from some MySQL table or file. How to do that? Here we will describe you the technique by creating a table with numbers 1 to n.
<table border = 1> <% for ( int i = 0; i < n; i++ ) { %> <tr> <td>Number</td> <td><%= i+1 %></td> </tr> <% } %> </table>
The above piece of code will go inside the
<body>
tag of the JSP file and will work when you initialize n
with some value.
Also, observer closely we have only included the java code inside the scriptlet tag, and all the HTML part is outside of it. Similarly we can do plenty of stuff.
Here is one more very simple example :
<% if ( hello ) { %> <p>Hello, world</p> <% } else { %> <p>Goodbye, world</p> <% } %>
Above code is using if-else condition to evaluate what to show, based on the value of a boolean variable named
hello
.
You can even ask user to enter the value of
hello
, using HTML Form and evaluate your JSP code based on that.