Servlet

Java Primer
Incremental J2EE
by Vince Huston
Listing 4. Servlet.


// Purpose.  Servlet

package article.servlet;

import java.util.*;
import java.io.*;
import javax.rmi.*;      // PortableRemoteObject
import javax.naming.*;   // InitialContext
import javax.servlet.*;
import javax.servlet.http.*;

public class StoreClientServlet extends javax.servlet.http.HttpServlet {
   private RIStore   theStore;
   private ArrayList theItems;

   public void init( ServletConfig sc ) {
      try {
         super.init( sc );
         InitialContext ic = new InitialContext();
         Object obj = ic.lookup( "StoreServer" );
         theStore   = (RIStore) PortableRemoteObject.narrow( obj, RIStore.class );
         theItems   = theStore.getItems();
      } catch (ServletException ex) { ex.printStackTrace();
      } catch (NamingException ex ) { ex.printStackTrace();
      } catch (java.rmi.RemoteException ex ) { ex.printStackTrace();
   }  }

   public void doGet( HttpServletRequest req, HttpServletResponse resp )
            throws ServletException, IOException {
      PrintWriter pw = resp.getWriter();
      pw.println( "<head><title>StoreClientServlet</title></head><body>" );

      HttpSession session = req.getSession();
      // get the array of choices stored in the session object
      ArrayList theChoices = (ArrayList) session.getAttribute( "theChoices" );
      // get the text input element named "choice"
      String choice = req.getParameter( "choice" );

      // if this is the first request for this page
      if (theChoices == null) {
         theChoices = new ArrayList();
         session.setAttribute( "theChoices", theChoices );

      // remember the current selection
      } else if ( ! choice.equals("0")) {
         theChoices.add( new Integer( Integer.parseInt(choice) - 1 ) );
         session.setAttribute( "theChoices", theChoices );

      // time to place the order
      } else {
         StringBuffer response =
              new StringBuffer( theStore.placeOrder( theChoices ) );
         formatOrderConfirmation( response );
         pw.println( response.toString() );
         pw.println( "<p><a href=StoreAlias>Top</a></body>" );
         session.invalidate();
         return;
      }

      // always output the base form
      pw.println( "<form method=post action=StoreAlias>" );
      pw.println( "Select from (0 when done):<ol>" );
      Iterator it = theItems.iterator();
      for (int i=1; it.hasNext(); i++)
         pw.println( "<li>" + it.next() );
      pw.println( "</ol>Choice:" );
      pw.println( "<input type=text name=choice>" );
      pw.println( "<input type=submit value=Select>" );

      // add an extra <UL> to display the history of previous choices
      if (theChoices.size() > 0) {
         pw.println( "<p>Selections are:<ul>" );
         it = theChoices.iterator();
         for (int i=1; it.hasNext(); i++)
            pw.println( "<li>" + theItems.get(((Integer)it.next()).intValue()) );
         pw.println( "</ul>" );
      }

      pw.println( "</form></body>" );
   }

   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,"       ");
   }     }
   public void doPost( HttpServletRequest req, HttpServletResponse resp )
                             throws ServletException, IOException {
      doGet( req, resp );
}  }