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;
   }
}