JSP expressions, scriptlets and declarations
In this tutorial, we will practice with the JSP expressions, scriptlets and declarations. Expressions are placed between <%= and %>, scriptlets are placed between <% and %> and declarations are placed between <%! and %>.
JSP expression: <%= code %>
-
Right click on the WebContent folder, select New→JSP file.
Name it expression.jsp and click Finish.
Now we are going to add some expressions between the <body> tags.
Expressions are placed between
<%= and
%> tags. The JSP will
evaluate expressions, convert them to Strings and place them at the position they are in the HTML page.
For example, insert the following codes between the
<body> tags.
<h1>A random number is: <%= Math.random() %></h1>
Save the file.
Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
-
JSP scriptlet: <% code %>
Right click on the WebContent folder, select New→JSP file.
Name it scriptlet.jsp and click Finish.
Now we are going to add some scriptlets between the <body> tags.
Scriptlets are placed between <% and %> tags. The JSP will execute scriptlets without converting them to Strings. To show the execution result using scriptlets, we use the out function provided by Java Servlet.
For example, insert the following codes between the
<body> tags.
<%
// This scriptlet declares and initializes "date"
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
The time is now:
<%
// The following scriptlets generate HTML output
out.println( String.valueOf( date ));
out.println( "<br>Your machine's address is: " );
out.println( request.getRemoteHost());
%>
Save the file.
Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
-
JSP declaration: <%! code %>
Right click on the WebContent folder, select New→JSP file.
Name it declaration.jsp and click Finish.
Now we are going to add some declarations between the <body> tags.
Declarations are placed between <%! and %> tags. They are used to define variables and methods that are outside of any methods and can be reused by later expressions or scriptlets.
For example, insert the following codes between the
<body> tags.
<%! private int numEntries = 10;
private int randomInt(int range) {
return(1 + ((int)(Math.random() * range)));
} %>
<h1>A random list from 1 to 100:</h1>
<ul>
<% for(int i=0; i<numEntries; i++) {
out.println("<li>" + randomInt(100));
} %>
</ul>
Save the file.
Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
-
Exercises
We are going to set the background color of the file expression.jsp to blue or red according to a random number. So, change the first <body> tag to <body bgcolor=“YOUR_CODE_HERE”>. Check if the random number is < 0.5, replace YOUR_CODE_HERE=blue, otherwise YOUR_CODE_HERE=red.
To add a parameter in the request, we use
[filename].jsp?[paramtername1]=[value1]&[paramtername2]=[value2]… Now, add the code:
<%= request.getParameter(“color”) %> into the file
declaration.jsp. Then open:
http://localhost:8080/jsplabs/declaration.jsp?color=red to check whether you catch the right parameter's value.
Let's add a function to the declaration.jsp file do set the its background color according to user's request. For example, if user requested color is red, we display the background in red, if requested color is blue, we display the background in blue.
Back to top