A Brief Introduction to Java Server Page
K. Yue April 2001

Introduction

Architecture

JSP Core Syntax

Example:   HelloWorld.jsp

<%@ page info="A Hello World JSP Page" %>
<% for (int i=0; i<10; i++) {
out.println("Hello, World.");
}
%>

Example:  IncludeIncorrect.jsp

<%@ page info="A Simple Hello World Page"
contentType="text/html;charset=ISO-8859-1"
%>
<BODY BGCOLOR="#CCCCFF">
<%@ include file="HelloWorld.jsp" %>
</BODY>

Note that this may cause trouble in some JSP server as the page directive is now duplicated.

Example: ParameterSnoop.jsp

<%@ page import = "java.util.*" %>
<%-- The directive import allows importing Java packages. --%>

<%
   out.println("Request Parameters:");
   Enumeration enum = request.getParameterNames();
   while (enum.hasMoreElements()) {
      String name = (String) enum.nextElement();
      String values[] = request.getParameterValues(name);
      if (values != null) {
         for (int i = 0; i < values.length; i++) {
            out.println(name + " (" + i + "): " + values[i]);
         }
      }
   }
%>

Example: TempCount.jsp

<%@ page isThreadSafe="true"
info="Simple useless non-permanent counter" %>

<%-- Define a static variable count --%>
<%! int count = 0; %>

<BODY BGCOLOR="ccccff">
The number of visitors since this JSP page is loaded:
<%= ++count %>.
</BODY>

Using Java Beans

Example: HelloBean.jsp

<jsp:useBean id="hello" class="temp.HelloBean" />
<BODY BGCOLOR=#CCCCFF>
Hello,
<SPAN STYLE="COLOR:BLUE"><%= hello.getMessage() %></SPAN>.
</BODY>

temp/HelloBean.java (an 'ugly' version)

package temp;
public class HelloBean {
   private String message = "You are the sunshine of my life.";
   public String getMessage() {
      return message;
   }
}
 

Example: Random quote in JSP and Java beans

In this example, a Java bean RandomQuote is used to get a random quote in the area of "Java", "Perl" or "Misc". The default is Java. The Java bean:

test/RandomQuote.java:

package test;
import java.util.Random;

public class RandomQuote {
   private static final String JAVA = "Java";
   private static final String PERL = "Perl";
   private static final String MISC = "miscellaneous";
   private static final Random RANDOM = new Random();

    private static final String[] JAVA_QUOTES = {
      "Java is good for your health.",
      "I want coffee beans, not Java Beans.",
      "Java is not my cup of coffee.",
      "Bill Gates hates Java.",
      "Two cups of Java and call me in the morning."
   };

    private static final String[] PERL_QUOTES = {
      "No more Perl!",
      "I'd rather have pearls, then Perl.",
      "Perl gives you good head massage.",
      "No Perl, no grade."
   };

   private static final String[] MISC_QUOTES = {
      "Make no mistakes. I like Coke.",
      "Don't drink Coke today.",
      "Mama Jones lives here.",
      "Eat your own dog food!"
   };

   private String quoteType = JAVA;

   public void setQuoteType (String quoteType) {
      this.quoteType = quoteType;
   }

   public String getQuoteType () {
      return quoteType;
   }

   public String getQuote () {
      if (quoteType.equalsIgnoreCase(JAVA))
         return JAVA_QUOTES[Math.abs(RANDOM.nextInt())
            % JAVA_QUOTES.length];
      else if (quoteType.equalsIgnoreCase(PERL))
         return PERL_QUOTES[Math.abs(RANDOM.nextInt())
            % PERL_QUOTES.length];
      else
         return MISC_QUOTES[Math.abs(RANDOM.nextInt())
            % MISC_QUOTES.length];
   }
}

quote.jsp:

<jsp:useBean id="quote" class="test.RandomQuote"
        scope="request">
<jsp:setProperty name="quote" property="quoteType"
        param="quoteType" />
</jsp:useBean>
<BODY BGCOLOR=#CCCCFF>
Hello, your random quote on
<SPAN STYLE="COLOR:BLUE"><%= quote.getQuoteType() %></SPAN> is:
<P>
<jsp:getProperty name="quote" property="quote" />
</BODY>