JavaEnterprise JavaStart serving Java Server Pages: Introduction

Start serving Java Server Pages: Introduction content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Since its introduction just a few months ago, Java Server Pages (JSP) technology has captured the attention of Java and Web developers. JSP offers a unique environment to create highly dynamic Web applications. In this part of our article, we’ll introduce JSP and help you set up an environment where you can experiment with some examples. We’ll also cover some advanced features of JSP and what makes it a unique and dynamic framework for development of Web applications.

JSP: A comparative look

The idea of JSP is similar to a popular offering from Microsoft called Active Server Pages (ASP). Under both architectures, traditional HTML is intermixed with a programming language and interpreted by an engine. While HTML is responsible for primarily the presentation of the information, the programming language contains the logic and connectivity code. While ordinary Web pages (containing HTML) are simply served by the Web server, ASP or JSP pages go through a separate engine that parses all the programming code and executes it. The result is then embedded back in the HTML page and is sent out to the browser. Both ASP and JSP are strictly server-side technologies. No additional software is needed on the client (browser).

While ASP uses VBScript as its programming language, JSP uses Java. That’s the obvious difference. The more fundamental difference is in the way the two engines handle the programming code contained in the page. In the case of ASP, the VBScript code is interpreted. In the case of JSP, the Java code is compiled into a servlet and then executed by the Java Virtual Machine. The compilation occurs only on the first request of the JSP page.

Getting started

To get started with JSP, you should visit the JSP Homepage on Sun’s Web site. The URL is From there you can download the specifications for version 1.0 and 1.1 of JSP. These are the specs that vendors use to create JSP engines. You should also download the JSP Syntax Card (, which is a handy guide as you start your road to learning JSP.

The next step is to download and install a JSP engine so that you can write your own JSP pages and see how they are executed. I recommend the JavaServer Web Development Kit (JSWDK) from Sun. It is a reference implementation for JSP 1.0 and the Java Servlet API 2.1. It contains plenty of examples that you can modify and enhance to learn the new technology. Once you install the JSWDK, you can start the server by running the

shell script (Unix) or the startserver.bat file (Windows). The default configuration starts the server on port 8080, so a URL like http://localhost:8080 should point you to the right page.

Click on the link to JSP Examples. Before you run any of the examples, take a look at the directory where you installed JSWDK. There should be a subdirectory there called “work.” As you execute the examples, monitor the content of the work directory. Note how the JSP pages are translated into Java source files and then compiled into class files (servlets). This is how the magic of JSP happens! You should run a few of the examples and see the result.

The examples fall into two categories. They are either JSP files or they are HTML files that contain a form. The form is then handled by a JSP file. Web developers are used to being able to view the source code for an HTML page via the browser’s View Source option. One common misconception is that they’ll be able to do the same to view the source code for JSP pages. That is not true, because JSP pages are processed by the JSP engine before they are sent to the browser. As part of this processing, all the Java code is taken out and executed, so the browser will only get the final result, which is HTML. So don’t look for Java code inside the JSP-generated pages! The source code for all the examples is provided separately on the “examples” page.

Run your own code

The following is a very simple JSP page. You can create another directory under the “examples” directory of your JSWDK installation and place this file there. You can name it whatever you want, but make sure it has a .jsp extension. You can see that the file has a normal HTML structure, except where Java code is inserted. We are using a for loop to create a string of numbers 0 through 9. That string is then displayed. Note how the <% and %> are used to separate Java code from ordinary HTML. In fact we have placed some HTML text right before and after we output the string.

<HTML><HEAD><TITLE>A JSP page</TITLE></HEAD><BODY><%@ page language="java" %><%! String str="0"; %><% for (int i=1; i < 10; i++) {    str = str + i;} %>Before <B>JSP</B> Output<P><%= str %><P>After <B>JSP</B> Output </BODY></HTML>

Go ahead and run this example.

The JSP code above consists of several distinct parts. The first are directives. They provide information about the page, such as language used, whether session is maintained or not, and buffering. In our example, our directive simply indicates that the Java language is used (even though Java is currently the only supported language in the JSP specification). Directives begin with the <%@ and close with %>. Here is our directive line:

<%@ page language="java" %>

The next part are the JSP Declarations. You can think of them as the place to declare class-level variables and methods. We have just defined a String variable:

<%! String str="0"; %>

Declarations begin with a <%! and close with %>. Also, the semicolon after each declaration is necessary just as if you were writing a class definition.

The JSP Scriptlets are the Java code blocks that form the logic of a JSP page. They are enclosed between <% and %>. In our example, our scriptlet contains a for loop.

Finally, JSP expressions provide a handy way to embed JSP-generated values within your HTML page. In our code, we output the value of the str variable as follows:

<%= str %>

JSP expressions begin with <%= and end with %>.

This has been a quick introduction to JSP. In the second part of this article, we’ll explore the four elements of JSP in more detail and discuss other useful aspects of JSP.

About the author

Piroz Mohseni is president of Bita Technologies, focusing on business improvement through the effective usage of technology. His areas of interest include enterprise Java, XML, and e-commerce applications. Contact him at

Go to Part 2 of this article: Start serving Java Server Pages: Sessions and Beans.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories