Oracle JavaServer Pages Developer's Guide and Reference
Release 8.1.7

Part Number A83726-01

Library

Product

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Basic Samples

This section provides JSP samples that are fairly basic but also exemplify use of the Oracle JML datatypes. This includes an elementary "hello" sample, a sample of using a JavaBean, and a more intermediate shopping cart example. The following samples are provided:

These examples could use standard datatypes instead, but JML datatypes offer a number of advantages, as described in "JML Extended Datatypes". JML datatypes are also portable to other JSP environments.

Hello Page--hellouser.jsp

This sample is an elementary JSP "hello" page. Users are presented with a form to enter their name. After they submit the name, the JSP page redisplays the form with the name at the top.

<%-----------------------------------------------------------
   Copyright (c) 1999, Oracle Corporation. All rights reserved.
------------------------------------------------------------%>

<%@page session="false" %>

<jsp:useBean id="name" class="oracle.jsp.jml.JmlString" scope="request" >
   <jsp:setProperty name="name" property="value" param="newName" />
</jsp:useBean>

<HTML>
<HEAD>
<TITLE>
Hello User
</TITLE>
</HEAD>

<BODY>

<% if (!name.isEmpty()) { %>
<H3>Welcome <%= name.getValue() %></H3>
<% } %>

<P>
Enter your Name:
<FORM METHOD=get>
<INPUT TYPE=TEXT name=newName size = 20><br>
<INPUT TYPE=SUBMIT VALUE="Submit name">
</FORM>

</BODY>
</HTML>

Usebean Page--usebean.jsp

This page uses a simple JavaBean, NameBean, to illustrate usage of the jsp:useBean tag. Code for the bean is provided as well as code for the page.

Code for usebean.jsp

<%-----------------------------------------------------------
   Copyright (c) 1999, Oracle Corporation. All rights reserved.
------------------------------------------------------------%>

<%@ page import="beans.NameBean"  %>

<jsp:useBean id="pageBean" class="beans.NameBean" scope="page" />
<jsp:setProperty name="pageBean" property="*" />

<jsp:useBean id="sessionBean" class="beans.NameBean" scope="session" />
<jsp:setProperty name="sessionBean" property="*" />

<HTML>
<HEAD> <TITLE> The UseBean JSP </TITLE> </HEAD>
<BODY BGCOLOR=white>

<H3> Welcome to the UseBean JSP </H3>
<P><B>Page bean: </B>
<% if (pageBean.getNewName().equals("")) { %>
  I don't know you. 
<% } else { %>
  Hello <%= pageBean.getNewName() %> !
<%  } %> 
  
<P><B>Session bean: </B>
<% if (sessionBean.getNewName().equals("")) { %>
  I don't know you either. 
<% } else {
       if ((request.getParameter("newName") == null) || 
           (request.getParameter("newName").equals(""))) { %>
         Aha, I remember you.   
<%     } %>   
       You are <%= sessionBean.getNewName() %>.
<% } %>

<P>May we have your name?
<FORM METHOD=get>
<INPUT TYPE=TEXT name=newName size = 20>
<INPUT TYPE=SUBMIT VALUE="Submit name">
</FORM>
</BODY>
</HTML>

Code for NameBean.java

package beans;

public class NameBean {

  String newName="";

  public void NameBean() {  }

  public String getNewName() {
    return newName;
  }
  public void setNewName(String newName) {
    this.newName = newName;
  }
} 

Shopping Cart Page--cart.jsp

This sample shows how to use session state to maintain a shopping cart. The user chooses a T-shirt or sweatshirt to order and the order is then redisplayed. If shopping continues and the order is changed, the page redisplays the order, striking out the previous choices as appropriate.

The cart.jsp file is the primary source file; it references index.jsp. Code for both pages is provided.

Code for cart.jsp

<%-----------------------------------------------------------
   Copyright (c) 1999-2000, Oracle Corporation. All rights reserved.
------------------------------------------------------------%>
<jsp:useBean id="currSS" scope ="session" class="oracle.jsp.jml.JmlString" />
<jsp:useBean id="currTS" scope ="session" class="oracle.jsp.jml.JmlString" />

<HTML>

<HEAD>
        <TITLE>Java Store</TITLE>
</HEAD>

<BODY BACKGROUND=images/bg.gif BGCOLOR=#FFFFFF>

<jsp:useBean id="sweatShirtSize" scope="page" class="oracle.jsp.jml.JmlString" >
   <jsp:setProperty name="sweatShirtSize" property="value" param="SS" />
</jsp:useBean>
<jsp:useBean id="tshirtSize" scope="page" class="oracle.jsp.jml.JmlString" >
   <jsp:setProperty name="tshirtSize" property="value" param="TS" />
</jsp:useBean>

<jsp:useBean id="orderedSweatshirt" scope="page" 
class="oracle.jsp.jml.JmlBoolean" >
   <jsp:setProperty name="orderedSweatshirt" property="value" 
      value= '<%= !(sweatShirtSize.isEmpty() || 
sweatShirtSize.getValue().equals("none")) %>'  />
</jsp:useBean>

<jsp:useBean id="orderedTShirt" scope="page" class="oracle.jsp.jml.JmlBoolean" >
   <jsp:setProperty name="orderedTShirt" property="value"
      value='<%= !(tshirtSize.isEmpty() || tshirtSize.getValue().equals("none")) 
%>'  />
</jsp:useBean>

<P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100% HEIGHT=553>
        <TR>
                <TD WIDTH=33% HEIGHT=61>&nbsp;</TD>
                <TD WIDTH=67% HEIGHT=61>&nbsp;</TD>
        </TR>
        <TR>
                <TD WIDTH=33% HEIGHT=246>&nbsp;</TD>
                <TD WIDTH=67% HEIGHT=246 VALIGN=TOP BGCOLOR=#FFFFFF>

   <% if (orderedSweatshirt.getValue() || orderedTShirt.getValue()) { %>
      Thank you for selecting our fine JSP Wearables!<P>
      
      <% if (!currSS.isEmpty() || !currTS.isEmpty()) { %>
      You have changed your order:
            <UL>
         <% if (orderedSweatshirt.getValue()) { %>
            <LI>1 Sweatshirt
            <% if (!currSS.isEmpty()) { %> 
               <S>(size: <%= currSS.getValue().toUpperCase() %>)</S>&nbsp
            <% } %>
            (size: <%= sweatShirtSize.getValue().toUpperCase() %> )
         <% } else if (!currSS.isEmpty()) { %>
               <LI><S>1 Sweatshirt (size: <%= currSS.getValue().toUpperCase()
               %>)</S>
         <% } %>

            <% if (orderedTShirt.getValue()) { %>
            <LI>1 Tshirt 
            <% if (!currTS.isEmpty()) { %> 
               <S>(size: <%= currTS.getValue().toUpperCase() %>)</S>&nbsp
            <% } %>
            (size: <%= tshirtSize.getValue().toUpperCase() %>)
         <% } else if (!currTS.isEmpty()) { %>
               <LI><S>1 Tshirt (size: <%= currTS.getValue().toUpperCase()
               %>)</S>
            <% } %>
      </UL>
      <% } else { %>
      You have selected: 
      <UL>
         <% if (orderedSweatshirt.getValue()) { %>
            <LI>1 Sweatshirt (size: <%= sweatShirtSize.getValue().toUpperCase()
                                  %>)
         <% } %>

         <% if (orderedTShirt.getValue()) { %>
            <LI>1 Tshirt (size: <%= tshirtSize.getValue().toUpperCase() %>)
         <% } %>

      </UL>
      <% } %>
   <% } else { %>
      Are you sure we can't interest you in something?
   <% } %>

   <CENTER>
      <FORM ACTION="index.jsp" METHOD="GET"
            ENCTYPE="application/x-www-form-urlencoded">
      <INPUT TYPE="IMAGE" SRC="images/shop_again.gif" WIDTH="91" HEIGHT="30"
            BORDER="0">
      </FORM>
   </CENTER>
   </TD></TR>
</TABLE>

</BODY>

</HTML>

<% 
if (orderedSweatshirt.getValue()) { 
   currSS.setValue(sweatShirtSize.getValue());
} else {
   currSS.setValue("");
}

if (orderedTShirt.getValue()) { 
   currTS.setValue(tshirtSize.getValue());
} else {
   currTS.setValue("");
}
%>

Code for index.jsp

<%-----------------------------------------------------------
   Copyright (c) 1999-2000, Oracle Corporation. All rights reserved.
------------------------------------------------------------%>
<jsp:useBean id="currSS" scope ="session" class="oracle.jsp.jml.JmlString" />
<jsp:useBean id="currTS" scope ="session" class="oracle.jsp.jml.JmlString" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
 <TITLE>untitled</TITLE>
</HEAD>

<BODY BACKGROUND="images/bg.gif" BGCOLOR="#FFFFFF">

<FORM ACTION="cart.jsp" METHOD="POST" 
ENCTYPE="application/x-www-form-urlencoded">
<P>
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%" HEIGHT="553">
 <TR>
  <TD WIDTH="33%" HEIGHT="61">&nbsp;</TD>
  <TD WIDTH="67%" HEIGHT="61">&nbsp;</TD>
 </TR>
 <TR>
  <TD WIDTH="33%" HEIGHT="246">&nbsp;</TD>
  <TD WIDTH="67%" HEIGHT="246" VALIGN="TOP" BGCOLOR="#FFFFFF">
   <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="81%">
    <TR>
     <TD WIDTH="100%" BGCOLOR="#CCFFFF">
      <H4>JSP Wearables
     </TD>
    </TR>
    <TR>
     <TD WIDTH="100%" BGCOLOR="#FFFFFF">

      <BLOCKQUOTE>
      Sweatshirt
      <SPACER TYPE="HORIZONTAL" SIZE="10">($24.95)<BR>
      <SPACER TYPE="HORIZONTAL" SIZE="30">
       <INPUT TYPE="RADIO" NAME="SS" VALUE="xl" 
          <%= currSS.getValue().equals("xl") ? "CHECKED" : "" %> >XL
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="SS" VALUE="l" <%= currSS.getValue().equals("l")
         ? "CHECKED" : "" %> >L
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="SS" VALUE="m" <%= currSS.getValue().equals("m")
         ? "CHECKED" : "" %> >M
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="SS" VALUE="s" <%= currSS.getValue().equals("s")
         ? "CHECKED" : "" %> >S
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="SS" VALUE="xs" 
          <%= currSS.getValue().equals("xs") ? "CHECKED" : "" %> >XS
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="SS" VALUE="none" 
          <%= currSS.getValue().equals("none") || currSS.isEmpty()  ? 
          "CHECKED" : "" %> >NONE
      <BR>
      <BR>
      T-Shirt<SPACER TYPE="HORIZONTAL" SIZE="10"> (14.95)<BR>
      <SPACER TYPE="HORIZONTAL" SIZE="30">
       <INPUT TYPE="RADIO" NAME="TS" VALUE="xl" 
          <%= currTS.getValue().equals("xl") ? "CHECKED" : "" %> >XL
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="TS" VALUE="l" <%= currTS.getValue().equals("l")
         ? "CHECKED" : "" %> >L
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="TS" VALUE="m" <%= currTS.getValue().equals("m")
         ? "CHECKED" : "" %> >M
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="TS" VALUE="s" <%= currTS.getValue().equals("s")
         ? "CHECKED" : "" %> >S
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="TS" VALUE="xs" 
          <%= currTS.getValue().equals("xs") ? "CHECKED" : "" %> >XS
      <SPACER TYPE="HORIZONTAL" SIZE="10">
       <INPUT TYPE="RADIO" NAME="TS" VALUE="none" 
          <%= currTS.getValue().equals("none") || currTS.isEmpty()  ? 
          "CHECKED" : "" %> >NONE
      </BLOCKQUOTE>
     </TD>
    </TR>
    <TR>
     <TD WIDTH="100%">
      <DIV ALIGN="RIGHT">
      <P><INPUT TYPE="IMAGE" SRC="images/addtobkt.gif" WIDTH="103" HEIGHT="22"
         ALIGN="BOTTOM" BORDER="0">
      </DIV>
     </TD>
    </TR>
   </TABLE>
  </TD>
 </TR>
</TABLE>

</FORM>

</BODY>

</HTML>



Go to previous page
Go to beginning of chapter
Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index