Java EE Tutorials - JSP Syntax

In our last post, we wrote a basic JSP page and served it in a web application. Now let’s take a close look at JSP’s syntax and features.

The example code is located in the jsp_part_2 branch of our tutorial Github repository. The ZIP file can be found here.

JSP Expressions

The JSP code from our bare bones page was an example of a JSP Expression. Syntactically, they use the <%= … %> construct.

We saw how expressions are translated into Servlet code - they’re simply passed into a call to out.write(). That should give you a pretty good idea as to what is and isn’t allowed in a JSP expression. Keep in mind, however, that JSP expressions don’t end in a semicolon - if they did, the Servlet would have something like out.write(new java.util.Random.nextInt(););, which just wouldn’t compile.

JSP Scriptlets

Scriptlets allow us to write “normal” java code within a JSP page. They are contained within the <% … %> construct. scriptlets.jsp shows us an example:


<html>
    <body>
        <%
            int x = 2;
            out.println("<p>We declared a variable named x in a scriptlet. " +
                        "It's value is currently " + x + "</p>");

            x = x + 3;
        %>

        <p>
            We changed the value of x in the scriplet. It's value is now <%= x %>
        </p>

        <%
            for(int i = 0; i < 3; i++) {
        %>
            <p>Repeat this line</p>
        <%
            }
        %>
    </body>
</html>

And here is what it looks like when it is loaded up in the browser:

Oh, and for good measure, the relevant code from the generated Servlet:

out.write("<html>\n");
out.write("    <body>\n");
out.write("        ");

int x = 2;
out.println("<p>We declared a variable named x in a scriptlet. " +
            "It's value is currently " + x + "</p>");

x = x + 3;

out.write("\n");
out.write("\n");
out.write("        <p>\n");
out.write("            We changed the value of x in the scriplet. It's value is now ");
out.print( x );
out.write("\n");
out.write("        </p>\n");
out.write("\n");
out.write("        ");

for(int i = 0; i < 3; i++) {

    out.write("\n");
    out.write("            <p>Repeat this line</p>\n");
    out.write("        ");

}

out.write("\n");
out.write("    </body>\n");
out.write("</html>\n");

Basically, the code in each Scriptlet is inserted into the Servlet in the exact place in which it is located in JSP page. This explains a lot of things, such as why you can declare multiple Scriptlets, and why you can declare a variable in one Scriptlet and use it in another. It also explains how the for-loop - which looks kinda weird in JSP - manages to work,

JSP Comments

JSP Comments are just like HTML comments. They look like <!– –>

JSP Declarations

I never mentioned this yet, but all of the code we write in Scriptlets and Expressions is added to the service() method of the generated Servlet. We can use a JSP Declaration to define variables and methods outside of this method. Declarations use the <%! … %> syntax.

Here is a quick example, courtesy of declarations.jsp:


<html>
    <body>
        <%! String str = "Test String";%>
        <%!
            String getStr() {
                return str;
            }
        %>
        <p>Let's call <code>getStr -</code> <%= getStr() %></p>
    </body>
</html>

We use two declaration tags to declare a variable and a method. Here is how they are added to the Servlet:

public final class declaration_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  String str = "Test String";

  String getStr() {
      return str;
  }
...

Of course, we can still use these variables and methods within our Expressions and Scriptlets.

Since our JSP declared and calls a method named getStr(), we should see the output of this method in our page. Indeed, that is the case:

JSP Directive

A JSP Directive can be used to include and import stuff into the page. They use the <%@ … %> syntax.

There are several types of things you can include using a Directive. For one, you can use them to add Java import statements into your JSP Page. You can also use them to include other JSP files (there’s a third thing you can include, but we’ll leave that for another post).

Here is directives.jsp, which demonstrates the use of both:


<%@ page import="java.math.*,java.io.*" %>
<%@ include file="scriptlets.jsp" %>
<html>
    <body>
       No JSP tags to show here in the page body ...
    </body>
</html>

The two Directives are at the top of the page. Notice that they’re both slightly different in content. page import is used for import statements, while include file is for including other JSP pages.

So what will these Directives do? First, if you look at the generated Servlet code, you will see the import statements at the top of the file like so:

import java.math.*;
import java.io.*;

As for the include Directive, well, just look at how the page renders in the browser:

No Directives whatsoever showing up on the page.

General Comments

  • If you were to ask most seasoned developers, they would highly discourage excessive usage of Scriplets and Expressions (though again, we still want to examine them for educational purposes). This is because there is another, “more advanced” option, in the form of JSP taglibs. We’ll cover these in a later post.
  • Excluding the use of taglibgs, what we learned in this post is pretty much all there is to the standard JSP syntax. While it doesn't look like much, you can do quite a lot with it if you think about it.

    Or rather, you *could* do a lot with it. The trouble with all of our examples so far is that they're not very interactive. We still don't have any way to get data from the user, and use that data to dynamically change the content of the page. We'll take a look at one way to fix this in our next post.