Table of Contents

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

  1. Open the jsplabs project created in previous tutorial.
  2. Right click on the WebContent folder, select New→JSP file.
  3. Name it expression.jsp and click Finish.
  4. Now we are going to add some expressions between the <body> tags.
  5. 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.
  6. For example, insert the following codes between the <body> tags.

    <h1>A random number is: <%= Math.random() %></h1>
  7. Save the file.
  8. Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
  9. A random number will be presented on your web browser at the URL: http://localhost:8080/jsplabs/expression.jsp


JSP scriptlet: <% code %>

  1. Right click on the WebContent folder, select New→JSP file.
  2. Name it scriptlet.jsp and click Finish.
  3. Now we are going to add some scriptlets between the <body> tags.
  4. 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.
  5. 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());
    %>
  6. Save the file.
  7. Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
  8. Current time and your IP address will be presented on your web browser at the URL: http://localhost:8080/jsplabs/scriptlet.jsp


JSP declaration: <%! code %>

  1. Right click on the WebContent folder, select New→JSP file.
  2. Name it declaration.jsp and click Finish.
  3. Now we are going to add some declarations between the <body> tags.
  4. 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.
  5. 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>
  6. Save the file.
  7. Run it by right clicking on it then selecting Run As→Run on Server. Select Finish.
  8. A list of 10 random numbers will be presented on your web browser at the URL: http://localhost:8080/jsplabs/declaration.jsp


Exercises

  1. 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.
  2. 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.
  3. 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.