JSP

Java Primer
Incremental J2EE
by Vince Huston
Listing 5. JSP.


<head><title>StoreClientJsp</title></head><body>

<jsp:useBean id="store" class="article.jsp.StoreBean" scope="session"/>

<jsp:setProperty name="store" property="selection" param="choice"/>

<%
   if (store.isDone()) {
%>
      <jsp:getProperty name="store" property="response"/>
      <p><a href=StoreJspAlias>Top</a></body>
<%
      session.invalidate();
      return;
   }
%>

<form method=post action=StoreJspAlias>
Select from (0 when done):
<ol>
   <jsp:getProperty name="store" property="itemList" />
</ol>
Choice:
<input type=text name=choice>
<input type=submit value=Select>

<%= store.displayCurrentSelections() %>

</form></body>



package article.jsp;

import java.util.*;
import javax.rmi.*;              // PortableRemoteObject
import javax.naming.*;           // InitialContext, NamingException
import article.servlet.RIStore;

public class StoreBean implements java.io.Serializable {
   private RIStore   theStore;
   private ArrayList theItems;
   private ArrayList theChoices;
   private String    selection;

   public StoreBean() {
      theChoices = new ArrayList();
      try {
         InitialContext ic = new InitialContext();
         Object obj = ic.lookup( "StoreServer" );
         theStore   = (RIStore) PortableRemoteObject.narrow( obj, RIStore.class );
         theItems   = theStore.getItems();
      } catch (NamingException ex ) { ex.printStackTrace();
      } catch (java.rmi.RemoteException ex ) { ex.printStackTrace();
   }  }
   public void setSelection( String str ) {
      selection = str;
      if ( ! selection.equals("0")) {
         theChoices.add( new Integer( Integer.parseInt(selection) - 1 ) );
   }  }
   public boolean isDone() {
      if (selection == null) return false;
      return selection.equals( "0" );
   }
   public String getResponse() {
      StringBuffer response = null;
      try {
         response = new StringBuffer( theStore.placeOrder( theChoices ) );
      } catch (java.rmi.RemoteException ex ) { ex.printStackTrace(); }
      // massage the returned String to work with HTML conventions
      formatOrderConfirmation( response );
      return response.toString();
   }
   public String getItemList() {
      StringBuffer sb = new StringBuffer();
      Iterator it = theItems.iterator();
      for (int i=1; it.hasNext(); i++)
         sb.append( "<li>" + it.next() );
      return sb.toString();
   }
   public String displayCurrentSelections() {
      // add an extra <UL> to display the history of previous choices
      StringBuffer sb = new StringBuffer();
      if (theChoices.size() > 0) {
         sb.append( "<p>Selections are:<ul>" );
         for (Iterator it = theChoices.iterator(); it.hasNext(); )
            sb.append( "<li>" + theItems.get(((Integer)it.next()).intValue()) );
         sb.append( "</ul>" );
      }
      return sb.toString();
   }
   private void formatOrderConfirmation( StringBuffer response ) {
      for (int i=0; i < response.length()-4; i++)
         if (response.charAt(i) == '\n') {
            response.deleteCharAt(i);
            response.insert( i, "<br>" );
         } else if (response.substring(i,i+3).equals("   ")) {
            response = response.replace(i,i+3,"       ");
}  }