|1.0||What are JavaServer Pages?|
|2.0||Our Running Example: CoffeeMaker|
|3.0||A Simple JSP|
|4.0||A (Slightly) More Complex JSP|
|5.0||Using JSP in Larger Systems|
JavaServer Pages (JSP) is a Java-based technology for creating dynamic web applications; that is, for creating websites which respond to user input and create content at runtime. Consider a typical flow of events when a user goes to a regular, HTML website:
This is what's known as a static website, because the content of the website's page does not change based on any user input. A dynamic website, such as what JavaServer pages is capable of, works similarly but with one extra step. Consider a typical flow of events for going to a JSP website:
JSPs are executed in a server program (often called a "container"), with access to an API for easier processing.
For the rest of this tutorial, we will be working
with a toy JSP application, called
We all know that most computer scientists love caffeine, so the Computer Science department is looking to put a coffee kiosk in EBII building. The coffee kiosk must be able to make coffee for students to purchase. The department recently upgraded the CoffeeMaker hardware to include a network connection. Now students will be able to order their coffee online, and their order will be waiting for them when they go to the machine.
Download the CoffeeMaker_Web source and deploy just like iTrust (with the same versions of Tomcat and Eclipse). See the lab deployment instructions or home deployment instructions on the iTrust website for more information.
Start up Tomcat and surf to
You should see a webpage like this:
Figure: Homepage of
As you can imagine,
<%@ ... %>
The first directive is the
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
This is a basic definition of a JSP, usually
required for every page. You can just copy-and-paste this to the top
of every JSP you work with. The second directive is an
<%@include file="head.jsp" %>
Eclipse Tip If you
Additionally, we create a session variable on index.jsp. A session variable is a container for an object that is passed between pages, and the variable is specific to a user. Therefore, each user connected to the webpage has a session, and the session can contain one or more variables.
The code snip-it below demonstrates how to get and set a session variable. The session object is always called session. The getAttribute() method returns an Object associated with the String parameter containing the name of the session variable, in this instance "cm". The setAttribute() method always returns an Object; therefore, you must cast the returned object into the type of object it should be.
A session variable is set using the setAttribute() method. This method may be used when creating a new session variable or updating an already existent session variable. The first argument to the setAttribute() method is the name of the session variable, and the second argument is the object.
CoffeeMaker cm = (CoffeeMaker)session.getAttribute("cm");
All other information on this page is HTML to be rendered by the user's browser.
In your browser, click on the link "Add a Recipe ". Fill out the form to add a recipe, for example:
Figure: Using the Add Recipe page
Now take a look at
That covers the basics of JSP. Most of iTrust is built using this format.
As one can imagine, JSP is capable of a whole lot more than what we have shown here. However, most of the JSP development you will need to do in iTrust this semester has already been done in similar features. That being said, I must emphasize that it is up to you to spend some time going through iTrust to learn how the code works. You don't have to learn how the whole system works, but try to understand all that you can (and need) to complete your assignment. I suggest that, rather than specializing in one area of the system (eg JSP), work on knowing how every part of a single feature works. Start with the JSP, dive into the Java classes, study how the database interactions works for that feature, and follow how the content makes its way back to the user's browser.
Although JSP may not appear to be the traditional
environment in which you will develop code, the same rules of good
software design apply. Consider the page in iTrust called "Edit
Patient Demographics", or
iTrust loosely follows the Model-View-Controller design pattern. The design of MVC in the context of iTrust goes like this:
Download the CoffeeMaker_Web.zip file to complete the exercise.
Below is a list of additional resources
Also below are a few extra JSP tricks that are found throughout iTrust and will help you in your assignments