Intro to JavaServer Pages: Learn the syntax basics

Web builders don't have to do much Java development, but it's important to know the basics. Learn the syntax basics.

By John Zukowski

Moving beyond installation, we'll now go into the JSP syntax. If you're not familiar with Java programming, you may want to visit Sun's tutorial; however, Web builders shouldn't have to do too much Java development. Other than a few method calls, the Java code in your JSP Web pages should be minimal.

With that in mind, let's look first at JSP directives and scripting elements, after which we'll explain JavaBeans and implicit objects. There are five types of JSP directives and scripting elements. With JSP 1.0, most of your JSP is enclosed within a single tag that begins with <% and ends with %>. With the newer JSP 1.1 specification, there are also XML-compliant versions.

JSP Directives and Scripting Elements
  Directives<%@ directive %>
  Declarations<%! declaration %>
  Expressions<%= expression %>
  Code Fragment/Scriptlet<% code fragment %>
  Comments<%— comment —%>

JSP directives are for the JSP engine. They do not directly produce any visible output but instead tell the engine what to do with the rest of the JSP page. They are always enclosed within the <%@ … %> tag. The two primary directives are page and include. The taglib directive will not be discussed but is available for creating custom tags with JSP 1.1.

The page directive is the one you'll find at the top of almost all your JSP pages. Although not required, it lets you specify things like where to find supporting Java classes:

<%@ page import="java.util.Date" %>

where to send the surfer in the event of a runtime Java problem:

<%@ page errorPage="errorPage.jsp" %>

and whether you need to manage information at the session level for the user, possibly across multiple Web pages (more later on sessions with JavaBeans):

<%@ page session="true" %>

The include directive lets you separate your content into more manageable elements, such as those for including a common page header or footer. The page included could be a fixed HTML page or more JSP content:

<%@ include file="filename.jsp" %>

JSP declarations let you define page-level variables to save information or define supporting methods that the rest of a JSP page may need. If you find yourself including too much code, it is usually better off in a separate Java class. Declarations are found within the <%! … %> tag. Always end variable declarations with a semicolon, as any content must be valid Java statements: <%! int i=0; %>.

With expressions in JSP, the results of evaluating the expression are converted to a string and directly included within the output page. JSP expressions belong within <%= … %> tags and do not include semicolons, unless part of a quoted string:

<%= i %>
<%= "Hello" %>

Code Fragments/Scriptlets
JSP code fragments or scriptlets are embedded within <% … %> tags. This Java code is then run when the request is serviced by the Web server. Around the scriptlets would be raw HTML or XML, where the code fragments let you create conditionally executing code, or just something that uses another piece of code. For example, the following displays the string "Hello" within H1, H2, H3, and H4 tags, combining the use of expressions and scriptlets. Scriptlets are not limited to one line of source code:

<% for (int i=1; i<=4; i++) { %>
<% } %>

The last of the key JSP elements is for embedding comments. Although you can always include HTML comments in your files, users can view these if they view the page's source. If you don't want users to be able to see your comments, you would embed them within the <%— … —%> tag:

<%— comment for server side only —%>

John Zukowski is a software guru and director of education at In addition, John is the author of several Java books and numerous Java-related technical articles.

Editor's Picks

Free Newsletters, In your Inbox