JSP scripting allows you to insert Java code into Java Servlet generated from the JSP page. These are the scripting elements: comment, expression, scriptlet, declaration, and expression language.
JSP Comment
JSP comment is to document the code. In addition, JSP comment is used to note some parts of the JSP page to make it clearer and easier to maintain. The following illustrates JSP comments inside a JSP page:
<%-- This is a JSP comment --%>
<%--
This is a JSP comment can span
in multiple lines
--%>
Code language: HTML, XML (xml)
JSP comment is stripped off from the page in the response to the web browser.
Expression
The expression is one of the most basic scripting elements in JSP. The expression is used to insert value directly into the output. The syntax of an expression is as follows:
<?= expression ?>
Code language: HTML, XML (xml)
It is noticed that there is no space between <% and =. For example, if you want to display the current date and time, you can use an expression as follows:
<%= new java.util.Date()%>
Code language: HTML, XML (xml)
You can also use the XML syntax of the JSP expression to achieve the same effect as the following example:
<jsp:expression>
Java Expression
</jsp:expression>
Code language: HTML, XML (xml)
Scriptlet
A scriptlet is similar to an expression except for the equal sign “=”. You can insert any plain Java code inside a scriptlet. Because mixing between Java code and HTML makes JSP pages difficult to maintain so scriptlet is not recommended for future development.
The following illustrates the syntax of the scriptlet:
<% // any java source code here %>
Code language: HTML, XML (xml)
In this example, we display a greeting message based on the current time of the day using the scriptlet.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JSP syntax</title>
</head>
<body>
<%
// using scriptlet
java.util.Calendar now = new java.util.GregorianCalendar();
String tod = "";
if (now.get(now.HOUR_OF_DAY) < 12) {
tod = "Morning!";
} else if (now.get(now.HOUR_OF_DAY) < 18) {
tod = "Afternoon!";
} else {
tod = "Evening!";
}
%>
Good <%=tod%>
</body>
</html>
Code language: JavaScript (javascript)
The XML syntax of the JSP scriptlet is as follows:
<jsp:scriptlet>
// Java code of scriptlet
</jsp:scriptlet>
Code language: HTML, XML (xml)
Declaration
If you want to define methods or fields, you can use JSP declaration. The JSP declaration is surrounded by the sign <%! and %>. For example, if you want to declare a variable x, you can use JSP declaration as follows:
<%! int x = 10; %>
Code language: HTML, XML (xml)
The final semicolon is required.
The difference between a variable using declaration and a variable declared using scriptlet is that a variable declared using a declaration tag is accessible by all methods, while a variable declared using scriptlet is only accessible to the _jspservice() method of the generated servlet from the JSP page.
We can also declare a method using a declaration tag as the following example:
<%!
public boolean isInRange(int x,int min,int max){
return x >= min && x <= max;
}
%>
Code language: HTML, XML (xml)