CSCI A348/548
Lab Notes Eleven

Fall 2000


Java Server Pages
The process of making JavaServer Pages accessible on the Web is much simpler than that for servlets.

Assuming that

you

No compiling, no packages, no CLASSPATH settings.

Do you have a web server that supports JSP?

Yes you do, since you installed your Tomcat.

Here's a set of examples to get you started with JSP.

  1. Make sure your Tomcat is running fine.

  2. Check Tomcat's JSP examples. They should run fine too.

  3. Find your document root directory in your Tomcat and change index.html file.
My document root directory is in
$TOMCAT_HOME/webapps/ROOT
Here's my index.html (Felix the Cat .gif added).

You should change yours also just to make sure you can locate it properly.

Note that page does not contain the actual path to it on the page, so don't get misled by that.

Now create a file (called test.jsp) in this directory with the following contents:

<html>
  <body bgcolor=white>
  Current time: <%= new java.util.Date() %>
  </body>
</html>
Then access it over the web. Here's how mine works.

JSP scripting elements let you insert code into the servlet that will be generated from the JSP page. There are three forms, and we have just looked at one of them:

  1. Expressions of the form
    <%= expression %>
    are evaluated and inserted into the servlet's output.
Let's practice some more with these.

Create a directory lab11 and cd to it.

Create e1.jsp with the following contents:

<html><body bgcolor=white>

JSP Expressions: <ul>

<li> Current time: <%= new java.util.Date() %> 
<li> Your hostname: <%= request.getRemoteHost() %> 
<li> Your session ID: <%= session.getId() %> 
<li> The value of a form's <code>param</code> parameter: 
     <%= request.getParameter("param") %> 

</ul> 

For the last one call your <code>.jsp</code> with

<blockquote><pre>e1.jsp?param=butterfly</pre></blockquote>

</body></html>
Here's how mine works.

OK, let's look at some other examples.

If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert arbitrary code into the servlet's _jspService (which is called by service).

Scriptlets have the following form:

<% Java Code %>
Here's an example:
Create this as e2.jsp in your lab11 directory.

<html>
  <head><title>Color Testing</title></head>
  
  <%
      String bgColor = request.getParameter("bgColor"); 
      boolean hasExplicitColor; 
      if (bgColor != null) {
        hasExplicitColor = true; 
      } else {
        hasExplicitColor = false; 
        bgColor = "WHITE"; 
      }
   %>

<body bgcolor="<%= bgColor %>">
<h2 align="center">Color Testing</h2>

  <%
      if (hasExplicitColor) {
        out.println("You supplied an explicit background color of " + 
                    bgColor + ".");
      } else {
        out.println("Using default background color of WHITE. " + 
                    "Supply the bgColor request attribute to try " +
                    "a standard color, an RRGGBB value, or to see " +
                    "if your browser supports X11 color names."); 
      } 
   %>

</body>

</html>
Here's mine called in three different ways: Please note that debugging is somewhat tricky so make sure you become familiar with the syntax quickly.

Let's review what we've done so far.

JSP scripting elements let you insert code into the servlet that will be generated from the JSP page.

There are three forms, and we have just looked at two of them:

  1. Expressions of the form
    <%= expression %>
    are evaluated and inserted into the servlet's output.

  2. Scriptlets of the form
    <% code %>
    which are inserted into the servlet's _jspService method (called by service).

The third form is called declarations.

They are of this form

<%! code %>
and are inserted into the body of the servlet class, outside of any existing methods.

Let's practice with declarations.

Create e3.jsp in your lab11 directory:

<html><head><title>JSP declarations</title></head><body>

<h1>JSP Declarations</h1> <p> 

<%! private int accessCount = 0; %>

<h3>Accesses to page since server reboot: <%= ++accessCount %> </h3>

</body></html>
Then test it. (You can test mine). So now you've seen the three JSP scripting elements in action.

You can now try to look over the JSP examples that Tomcat comes equipped with.

In class we will be going over the JSP topic in greater detail, and in a more structured way.

Here are a few more examples for your study and enjoyment.

For example you can easily predict the outcome of the presidential election with the following script:

<html><body>

<% if (Math.random() < 0.5) { %>

The new US president is Al Gore. 

<% } else { %> 

The new US president is George Bush. 

<% } %> 

</body></html>
This script illustrates conditional HTML.

Here's how mine works.


Last updated on November 8, 2000, by Adrian German for A348/A548