login

JSP Scripting Elements

JSP Scripting allows you to insert Java code into the servlet which is generated from JSP page. These are the forms of scripting elements such as: comment, expression, scriptlet, declaration and expression language.

JSP Comment

JSP comments are used to explain the complicated logic code or to mark some region inside a JSP page for later changes. Comments in JSP is declared inside a JSP page as follows:

<%-- This is a JSP comment --%>

<%-- 
   This is a JSP comment can span
   in multiple lines
--%>


JSP comment is stripped off from the page in the response to the web browser.

Expression

The most simple and basic of JSP scripting is expression. Expression is used to insert values directly to the output. The syntax of the expression is as follows. ( Be noted that there is no space between <% and =)

<?= expression ?>

For example, if you want to print out the current date and time you can use the expression as follows:

<%= new java.util.Date()%>

The XML syntax of the JSP expression is as follows:

<jsp:expression>
  Java Expression
</jsp:expression>

Scriptlet

Scriptlet is similar to the Expression without the equal sign "=". You can insert any plain Java code inside the scriptlet. Because of mixing between Java code and HTML is difficult to maintain so scriptlet is not recommended to use anymore. Here is the syntax of the scriptlet:

<% // any java source code here %>

In this example, we print the greeting based on the current time of the day using 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>

The XML syntax of JSP scriptlet is as follows:

<jsp:scriptlet>
   // Java code of scriptlet
</jsp:scriptlet>

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; %>

The final semicolon is very important.

The difference between a variable using declaration and a variable is declared using Scriptlet is that a variable declare using declaration tag is accessible by all the methods while a variable declared using Scriptlet is only accessible to the method _jspservice of the generated servlet from JSP page.

We can also declare a method using declaration tag, such as:

        <%!
        public boolean isInRange(int x,int min,int max){
            return x >= min && x <= max;
        }
        %>