GuidesJavaServer Pages, Introduction to JSP

JavaServer Pages, Introduction to JSP

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Java Programming, Lecture Notes # 750.

May 15, 2000


Introduction

Plain servlets

Other lessons in this online Tutorial series teach you how to write and execute Java servlets that generate HTML text and cause that text to be sent to a web browser.  Writing the code to produce the HTML text in servlets is often very tedious, and is not a fun thing to do.

What is JSP?

JSP makes it possible for you to encode fragments of Java code into special HTML tags.

JSP engine creates a servlet

When the HTML page containing the Java code fragments is presented to a JSP engine, the JSP engine automatically creates, compiles, and executes servlets to implement the behavior of the HTML and the embedded Java code.

For certain kinds of applications, this is a less-tedious approach than writing servlets to generate the HTML.

How do they differ?

The JSP approach is almost exactly the opposite of the approach where you write and compile the servlet.

In that approach, you can write servlets that automatically generate HTML on the fly when they execute.

With JSP, you can write HTML containing embedded Java that automatically generates and executes a servlet.

JSP can become very complex

Of course, if you aren’t careful, it is possible to cause your HTML page containing embedded Java code to become extremely complex.  Debugging such a page tends to be more difficult for me than debugging a pure Java program.

The best of both worlds

The best of both worlds can be achieved by using the embedded Java code fragments to execute Java Beans or other servlets.

With this combination approach, you can keep the HTML page relatively simple.  You can also separate functionality on your HTML page.

Similar to MVC

One way to look at this is to view it in a Model View Control (MVC) paradigm sense.

In this case, the bean approximates the model while the HTML page approximates a combination of the control and the view.  (If you aren’t familiar with the MVC paradigm, see the Tutorial lesson where I discuss MVC.)

Combining HTML and reusable Java

Thus, the judicious use of JSP makes it possible to combine the best of HTML and reusable Java software components for server-side applications.

What’s in this lesson?

In this lesson, I will introduce you to JavaServer (TM) Pages by way of a very simple JSP file.

I will also show you how to download, install, and configure the JavaServer Web Development Kit (JSWDK) from Sun.

I will provide you with a couple of sample files that you can use to confirm proper installation and configuration of the JSWDK on your machine.

What about future lessons?

I will be using the JSWDK for demonstration purposes in several future lessons where I will discuss various aspects of JSP.

A First Look at JSP

To get started, I am going to show you a very simple JavaServer Page or JSP file (see Figure 1).  I am assuming that you already have a rudimentary knowledge of HTML.  If not, there are dozens of good HTML tutorials available on the web.

What does this file do?

If you install this file on a JSP-compatible web server and access it with your browser, an output very similar to that shown in  Figure 2 will appear in your browser window.

First access may be slow

The first time you access a JSP file, there may be a slight pause while the server automatically creates, compiles, and executes a servlet representing the page.

If you then force a reload, the results should appear much more quickly the second time due to the fact that the corresponding servlet already exists on the web server in executable form.

Where’s the Java?

This JSP file contains only one Java code fragment as shown in .
Figure 3

I have highlighted (in boldface) the special HTML tags that surround the Java code (although depending on your display, that might not be apparent).  I will be discussing these tags at some length in subsequent lessons.

A Java for loop

As you can probably surmise from your prior knowledge of Java, this code fragment executes a for loop.  Exactly what happens inside the for loop won’t become clear until later when I discuss the line of HTML code that reads
 

<%= i %>

Tell Me More

JavaServer Pages (JSP) are based on servlet technology.  When combined with the use of JavaBean components, JSP promises to provide a capability that is at least as powerful as servlets, possibly more powerful than raw servlets, and potentially much easier to use.

Each technology does what it does best

JSP makes it possible for you to combine Java code with either HTML or XML in such a way as to allow each technology to be used for what it does best.

Creation and compilation of the servlet is automatic

Each JSP page is automatically compiled into a servlet by the JSP engine.  (You can only use JSP on web servers that are JSP compatible.)

Occurs on first access

The automatic creation and compilation of the servlet occurs the first time the page is accessed.  Depending on the behavior of the web server, the servlet will be saved for some period of time to be used over and over without the need to recreate and recompile it.

May be a pause on first access

Thus, the first time the page is accessed, there may be a pause while the web server creates and compiles the servlet.  After that, access to the page should be much faster.

How are JavaServer Pages used?

There are many different ways in which you can combine JSP, beans, and servlets.

As mentioned earlier, the judicious use of JSP makes it possible to combine the best capabilities of HTML and reusable software components for server-side applications.

Separating business logic from presentation

This makes it practical to separate the business logic from the presentation of data.

Hence, programmers who are skilled in writing Java Beans can provide reusable software components that implement the business logic.

Page designers skilled in HTML can embed calls to those beans in HTML without the need to become skilled Java programmers.

Getting Started

I recommend that you to study Java with my tutorials in one hand and a Java compiler and virtual machine in the other.  Then you can copy, compile and execute the sample programs and produce your own outputs as you study.

Needed, one JSP server

As is the case with servlets and JDBC, to test your JSP files, you will need more than a Java compiler and virtual machine.

In addition to those items, you will need a JSP-compatible web server in which to test your sample programs.

As of February 6, 2000, such a web server is available free of charge from Sun.  You can download the server at

http://java.sun.com/products/jsp/
download.html.

Here is what Sun has to say about the downloadable web server:
 

JavaServer (TM) Web Development Kit (JSWDK) 1.0.1 Reference Implementation – Final Release

The JavaServer Web Development Kit (JSWDK)1.0.1 combines the reference implementation for JavaServer Pages(TM) 1.0.1 and the Java(TM) Servlet API (2.1).  …

The JSWDK offers a simplified way of creating dynamic web pages that are independent of Web servers and computing platforms, giving you Write Once, Run Anywhere(TM) freedom. The JSWDK is available at no charge.

Installation and Setup

Quick-start instructions

When you download the JSWDK, you will find that quite a lot of documentation is provided to help you to install and set up the server.  At some point, you should read that documentation.

However, to get you off to a quick start, I am going to tell you how I have the server set up on my WinNT machine.  You should be able to set it up in a similar manner on your machine and see your first demonstration of JSP very quickly.  After that, you might want to go back and customize your installation.

Installation of the JSWDK

The JSWDK is downloaded as a zip file.  To install it, you simply extract the files (while preserving the directory structure) from the zip file.

Place the directory tree containing all of the files somewhere on your hard drive.  I elected to place the directory tree on my D-drive under the directory named Program Files.

My path to the JSWDK

As a result, my path to the top-level directory containing the JSWDK directory tree is:

d:Program Filesjswdk-1.0.1

The name of the top-level directory in the directory tree is jswdk-1.0.1.  Over time, as new versions are released, the name of this directory can be expected to change.

What about the JDK?

When you use your browser to request a JSP page from the server, it must create and compile a servlet.

The jswdk server does not contain a compiler and virtual machine.  Therefore, if you haven’t done so already, you will need to install the JDK that the server will use to compile and execute the servlet.

My path to the JDK

At the time of this writing, I have JDK 1.2.2 from Sun installed on my machine.  The path to the top-level directory containing the JDK is:

d:Program Filesjdk1.2.2

My JDK installation is completely standard (as per Sun) except that I have it installed on drive-D instead of drive-C.

What about the classpath?

When the server attempts to compile the servlet, it must know how to locate the class files for the JDK.

Therefore, a classpath environment variable is necessary.  Because of other Java software that I have installed on my machine, my classpath is long and complex.  Therefore, to avoid the risk of confusion, I won’t show you the entire classpath.

The standard JDK class libraries

Suffice it to say that one of the elements in the classpath is as shown below:

d:Program Filesjdk1.2.2libtools.jar

This element identifies the location of the jar file containing the standard class libraries for the JDK.

Classpath for servlets

Because I also use the JSWDK as a servlet engine (independent of JSP), I also need the following element in the classpath.  You will probably need a similar element in your classpath.

d:Program Filesjswdk-1.0.1lib
servlet.jar

This element identifies the location of the jar file containing the various software components necessary to compile and execute servlets.

Pretty simple, isn’t it?

That’s really about all there is to installing the JSWDK and getting it ready for use.  Of course, you will need to have the path environment variable set properly for the JDK, but that has nothing to do with the JSWDK.

Installing HTML, JSP, and Servlet Files

Once you install the JSWDK, you will see that the resulting directory tree is fairly complex with several different branches.

An examination of the documentation will tell you that you can configure the JSWDK in various ways insofar as where you place HTML files, JSP Files, and servlet files.

The default configuration

However, you can use the JSWDK right out of the zip file with no special configuration required.

To use the JSWDK in its default configuration, install your HTML files, JSP files, and servlet files in the directories shown below.
 

Place HTML and JSP files in the following directory

d:Program Filesjswdk-1.0.1
webpages

Place servlet class files in the following directory

d:Program Filesjswdk-1.0.1
examplesWeb-infservlets

(The beginning portion of your path will probably be different, depending on where you place the JSWDK on your hard drive).

Starting and Stopping the Server

You can start the server running by executing the following batch file:

d:Program Filesjswdk-1.0.1
startserver.bat

You should stop the server by executing the following batch file:

d:Program Filesjswdk-1.0.1
stopserver.bat

I placed shortcuts to these two batch files on my desktop to make it easy to start and stop the server.

Temporary files

When you start the server and access a JSP page, if you pay attention to the directory tree, you will notice that several temporary files are created and placed in a directory named work.  This directory is a subdirectory of the top-level directory named jswdk-1.0.1.

When you stop the server by executing the above batch file, these temporary files are automatically deleted.

Accessing HTML and JSP Files

Accessing a JSP file

Having performed the installation and setup described above, and having installed a JSP file (named jsp001.jsp) as described above, you should be able to access that JSP file by entering the following URL into your browser window.  (I will provide a sample file named jsp001.jsp below for you to use.)

http://localhost:8080/jsp001.jsp

You may need to be online for this to work.  In case this doesn’t work, you will need to study the JSWDK documentation to learn about alternative ways to address the server.

If you are unfamiliar with the use of the term localhost, you might want to do a little research on the web regarding that term.  There is also some information on the subject in my online Tutorials.

Accessing an HTML file

Similarly, you should be able to access an HTML file named jsp001.htm by entering the following into your browser window

http://localhost:8080/jsp001.htm
 

Sample HTML and JSP Files

A sample HTML file and a sample JSP file follow.  You should be able to

  • Paste them into your text editor,
  • Name them jsp001.htm and jsp001.jsp respectively, and
  • Install them in the web server as described above.

Then you should be able to access the HTML file as described above.

A link to the JSP file

When the HTML file is displayed in your browser window, it will contain a link to the JSP file.

Selecting that link should access the JSP file.

Executing the JSP file

This will, in turn, cause the server to create and execute a servlet and deliver the results of executing that servlet to your browser window.

The raw HTML file

The contents of the raw HTML file are shown in Figure 4.  Note that the text in the HTML file contains a lot of explanatory material.

The HTML results

When you access the above HTML file via your browser as described earlier, the result displayed in your browser window will be similar to that shown in Figure 5.

The links

Note that the underlined blue text is not a real link in Figure 5, but it will be a real link in your browser window.

Pay particular attention the link in the first paragraph.  I will be referring to it later.

The raw JSP file

The contents of the raw JSP file are shown in Figure 6.

I will discuss the particulars of this file in a future lesson.  The purpose of this lesson is simply to give you an overview and to help you get your server set up so that you can write and execute JSP files.  The discussion of programming details will be deferred until later.

The JSP results

Clicking the link shown in the first paragraph in your browser window

(http://localhost:8080/jsp001.jsp)

when the above HTML file is displayed will cause the above JSP file to be loaded by the web server.  This, in turn will cause the servlet to be created, compiled and executed.

Execution of the servlet will then cause results similar to that shown in Figure 2 to appear on your browser screen.

Accessing Servlet Files

This lesson really has anything to do with servlets in the absence of JSP, but the JSWDK is also useful as a servlet engine.

While you are at it, you might as well make certain that you have it properly set up for the execution of servlets that you write.

Having performed the installation and setup described above, and having installed a servlet class file named Servlet01.class in the directory identified described above, you should be able to access that servlet file by entering the following URL into your browser window.

http://localhost:8080/examples/
servlet/Servlet01

Source code for a servlet

The source code for a simple servlet is shown in Figure 7.  You should be able to

  • Compile this servlet,
  • Install the class file in the proper directory of the JSWDK as described earlier, and
  • Access the servlet as discussed above.

Hello Big Red World

When you do, the words “Hello Big Red World” should appear in red in your browser window.

If you aren’t familiar with servlet programming, I have previously written a large number of lessons on servlet programming in my online Java Tutorials that you can refer to in order to gain an understanding of the code in this servlet.

 

Copyright 2000, Richard G. Baldwin

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin’s Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories