| 
	 
  
	 The Developer's Resource & Community Site
	
	 |  
	 
		
		
        
		Developing Servlet Clients to our Enterprise JavaBeans 
		Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Servlet Clients 
		Pre-required Reading: Entity Java beans, Developing Entity Beans, Developing JB Clients. 	
	To work with any of these samples, you will need the following:
	 
	
	To work with the EJB Servlet client, you will need: 
	
The Steps involved in developing and deploying a Servlet client to our Enterprise Beans are: 
- Develop the client code
 - Define the servlet properties
 - Startup the EJB Server and the servlet runner
 - Connect to the servlet from any standard web browser
  
  A four-tier Architecture for a Typical Bank 
1. Develop the servlet client code 
    
        | Accounts.java | 
     
    
        import
        java.io.*;  
        import java.util.*;  
        import javax.servlet.*;  
        import javax.servlet.http.*;import java.rmi.*;  
        import java.util.*;  
        import Bank.*; public class Accounts
        extends HttpServlet {  
         TellerHome home = null;  
         Teller teller = null;  
         public
        void init() {  
          try{  
           home =
        (TellerHome)Naming.lookup("Teller");  
           if( home == null )  
           {  
            System.out.println( "null
        TellerHome returned..." );  
           }  
           else  
           {  
            teller = home.create();
         
            System.out.println(
        "Naming.lookup successful..." );  
            System.out.println( "home.create
        successful..." );  
           }  
          } catch(Exception e){System.out.println( e );}  
         }  
         public
        void doGet(HttpServletRequest req, HttpServletResponse
        res)  
          throws ServletException, IOException  
         {  
          displayForm(res, "Welcome");  
         }  
         public
        void doPost(HttpServletRequest req, HttpServletResponse
        res)  
          throws ServletException, IOException  
         { double initialBalance = 0,
        transferAmount = 0;  
           String customerName = null;  
           int fromAccount = 0, toAccount = 0;  
           boolean isCreate = false;  
           if
        ("application/x-www-form-urlencoded".equals(req.getContentType()))
        {  
            Enumeration enum =
        req.getParameterNames();  
            while (enum.hasMoreElements()) {  
             String name = (String)
        enum.nextElement(); System.out.println("name =
        " + name);  
            
        String values[] = req.getParameterValues(name);  
             if (values != null) {  
              if(
        name.equals("initialBalance") )  
              { initialBalance =
        (Double.valueOf(values[0])).doubleValue();  
              }  
              if(
        name.equals("customerName") )  
              { customerName =
        values[0];  
              } if(
        name.equals("transferAmount") )  
                {
        transferAmount =
        (Double.valueOf(values[0])).doubleValue();  
                }  
                if(
        name.equals("fromAccount") )  
                { fromAccount
        = Integer.parseInt( values[0]);  
                }  
                if(
        name.equals("toAccount") )  
                { toAccount =
        Integer.parseInt( values[0]);  
                } if(
        name.equals("createButton") )  
                  {
        isCreate = true;  
                  }  
                 
        if( name.equals("transferButton") )  
                  {
        isCreate = false;  
                  }  
             }  
            }  
            System.out.println("
        initialBalance = " + initialBalance +  
                              
        " customerName = " + customerName+  
                              
        " transferAmount = " + transferAmount+  
                              
        " fromAccount = " + fromAccount+  
                              
        " toAccount = " + toAccount+  
                              
        " isCreate = " + isCreate);  
           
        /**  
            * Talk to the bean here  
            */ 
           
        try{  
                if( ( home ==
        null ) || (teller == null) )  
                {  
                
        home = (TellerHome)Naming.lookup("Teller");  
                 teller =
        home.create();  
                }  
                if( isCreate
        == true ) { teller.createCheckingsAccount(customerName,
        initialBalance);  
                }  
                else  
                {  
                
        teller.TransferMoney( transferAmount, fromAccount,
        toAccount);  
                }  
               } catch(Exception
        e){ System.out.println( e ); }  
           }  
           displayForm(res, "Welcome");  
         }  
         private
        void displayForm(HttpServletResponse response, String
        message)  
          throws IOException  
         {  
          if( ( home == null ) || (teller == null))  
          {  
           try{  
            home =
        (TellerHome)Naming.lookup("Teller");  
            teller = home.create();  
           } catch(Exception e){ System.out.println( e
        ); }  
          }  
          response.setContentType("text/html");  
          PrintWriter out = response.getWriter();  
          out.println("<HTML><BODY>");
         
          out.println("<H1> List of Checking
        Accounts... </H1>");  
          out.println("<body
        bgcolor=\"#FFFFFF\">");  
          out.println("<table border=\"2\"
        width=\"100%\">");  
          out.println("<tr><td
        align=\"center\"
        bgcolor=\"#C0C0C0\"><font
        size=\"3\"");  
         
        out.println("face=\"Verdana\"><strong>Teller
        Number</strong></font></td>");  
          out.println("<td
        align=\"center\"
        bgcolor=\"#C0C0C0\"><font
        size=\"3\"");  
         
        out.println("face=\"Verdana\"><strong>Customer
        Name </strong></font></td>");  
          out.println("<td
        align=\"center\"
        bgcolor=\"#C0C0C0\"><font
        size=\"3\"");  
         
        out.println("face=\"Verdana\"><strong>Teller
        Balance
        </strong></font></td></tr>");
         
          System.out.println( "Checking List..."
        );  
          int i = 0;  
          Vector checkingList = null;  
          try{  
           checkingList = teller.getCheckingsList();  
          }catch(Exception e){ System.out.println(e);}  
          if(checkingList != null)  
           for(i = 0; i < checkingList.size(); i++)  
           {  
            try{  
             CheckingsPK key =
        (CheckingsPK)checkingList.elementAt(i);  
             if(key == null)  
              break;  
             Checkings found =
        (Checkings)teller.getCheckings(key.account_number);  
             if(found == null)  
              break;  
             System.out.println( "
        Account No = "+key.account_number+  
                                
        " Name = "+found.getCustomerName()+  
                                
        " Balance = " + found.getBalance() );  
             out.println(
        "<tr><td><font size=\"1\"
        face=\"Verdana\"><strong>" +
        key.account_number
        +"</strong></font></td>" +  
                         
        "<td><font size=\"1\"
        face=\"Verdana\"><strong>"+found.getCustomerName()
        +"</strong></font></td>" +  
                         
        "<td><p
        align=\"right\"><font
        size=\"1\"
        face=\"Verdana\"><strong>"+
        " $ " +found.getBalance()
        +"</strong></font></td>");  
           
        }catch(Exception e){ System.out.println(e); break;}  
           }  
         
        out.println("</tr></table>");  
          out.println("<p
        align=\"center\">");  
          out.println("<font size=\"3\"
        face=\"Verdana\"><strong>Total Number
        of Checking Accounts = "+ i +"
        </strong></font></p>");  
          System.out.println( "Listed all the "+ i
        + " records..." );  
         
        out.println("<hr>");  
          out.println("<H1> List of Savings
        Accounts... </H1>");  
          out.println("<body
        bgcolor=\"#FFFFFF\">");  
          out.println("<table border=\"2\"
        width=\"100%\">");  
          out.println("<tr><td
        align=\"center\"
        bgcolor=\"#C0C0C0\"><font
        size=\"3\"");  
         
        out.println("face=\"Verdana\"><strong>Teller
        Number</strong></font></td>");  
          out.println("<td
        align=\"center\"
        bgcolor=\"#C0C0C0\"><font
        size=\"3\"");  
         
        out.println("face=\"Verdana\"><strong>Customer
        Name </strong></font></td>");  
          out.println("<td
        align=\"center\"
        bgcolor=\"#C0C0C0\"><font
        size=\"3\"");  
         
        out.println("face=\"Verdana\"><strong>Teller
        Balance
        </strong></font></td></tr>");
         
          System.out.println( "Savings List..." );
         
          int j = 0;  
          Vector savingsList = null;  
          try{  
           savingsList = teller.getSavingsList();  
          }catch(Exception e){ System.out.println(e);}  
          if(savingsList != null)  
           for(j = 0; j < savingsList.size(); j++)  
           {  
            try{  
             SavingsPK key =
        (SavingsPK)savingsList.elementAt(j);  
             if(key == null)  
              break;  
             Savings found =
        (Savings)teller.getSavings(key.account_number);  
             if(found == null)  
              break;  
             System.out.println( "
        Account No = "+key.account_number+  
                                
        " Name = "+found.getCustomerName()+  
                                
        " Balance = " + found.getBalance() );  
             out.println(
        "<tr><td><font size=\"1\"
        face=\"Verdana\"><strong>" +
        key.account_number
        +"</strong></font></td>" +  
                         
        "<td><font size=\"1\"
        face=\"Verdana\"><strong>"+found.getCustomerName()
        +"</strong></font></td>" +  
                         
        "<td><p
        align=\"right\"><font
        size=\"1\"
        face=\"Verdana\"><strong>"+
        " $ " +found.getBalance()
        +"</strong></font></td>");  
           
        }catch(Exception e){ System.out.println(e); break;}  
           }  
         
        out.println("</tr></table>");  
          out.println("<p
        align=\"center\">");  
          out.println("<font size=\"3\"
        face=\"Verdana\"><strong>Total Number
        of Savings Accounts = "+ j +"
        </strong></font></p>");  
          System.out.println( "Listed all the "+ j
        + " records..." );  
         
        out.println("<hr>");  
         
        out.println("<p
        align=\"center\"><font
        size=\"4\"
        face=\"Verdana\">");  
          out.println("Create a New
        Teller</font></p>" );  
          out.println("<form
        method=\"POST\">");  
          out.println("<p
        align=\"center\"><font
        size=\"3\"
        face=\"Verdana\">");  
          out.println("<strong>Enter Name
        <input type=\"text\" size=\"20\"
        name=\"customerName\">");  
          out.println("Enter Initial Deposit
        </strong></font><font
        size=\"2\">");  
          out.println("<strong><input
        type=\"text\" size=\"20\"
        name=\"initialBalance\"></strong></font></p>");
         
          out.println("<p
        align=\"center\"><font
        size=\"2\"><input
        type=\"submit\"");  
          out.println("name=\"createButton\"
        value=\"Create\"></font></p>");
         
          out.println("</form><hr>");  
         
        if((checkingList == null) || (savingsList == null))  
          {  
          
        out.println("</BODY></HTML>");  
           return;  
          }  
          out.println("<p
        align=\"center\">");  
          out.println("<font size=\"4\"
        face=\"Verdana\">");  
          out.println("Transfer Money between
        Accounts</font></p>");  
         
        out.println("<form
        method=\"POST\">");  
          out.println("<p
        align=\"center\"><font
        size=\"3\"
        face=\"Verdana\">");  
          out.println("<strong>Enter amount
        <input type=\"text\" size=\"20\"
        name=\"transferAmount\">");  
          out.println("From Teller<select
        name=\"fromAccount\"
        size=\"1\">");  
          System.out.println("Listing checking
        from");  
          for(int k = 0; k < checkingList.size(); k++)  
          {  
           try{  
            CheckingsPK found =
        (CheckingsPK)checkingList.elementAt(k);  
            if(found == null)  
             break;  
            out.println("<option>"
        + found.account_number+ "</option>");  
           }catch(Exception e){ System.out.println(e);
        break;}  
          }  
          System.out.println("Listing saving
        from");  
          for(int k = 0; k < savingsList.size(); k++)  
          {  
           try{  
            SavingsPK found =
        (SavingsPK)savingsList.elementAt(k);  
            if(found == null)  
             break;  
            out.println("<option>"
        + found.account_number+ "</option>");  
           }catch(Exception e){ System.out.println(e);
        break;}  
          }  
         
        out.println("</select> To Teller<select
        name=\"toAccount\"
        size=\"1\">");  
          System.out.println("Listing checking
        to");  
          for(int k = 0; k < checkingList.size(); k++)  
          {  
           try{  
            CheckingsPK found =
        (CheckingsPK)checkingList.elementAt(k);  
            if(found == null)  
             break;  
            out.println("<option>"
        + found.account_number+ "</option>");  
           }catch(Exception e){ System.out.println(e);
        break;}  
          }  
          System.out.println("Listing saving to");
         
          for(int k = 0; k < savingsList.size(); k++)  
          {  
           try{  
            SavingsPK found =
        (SavingsPK)savingsList.elementAt(k);  
            if(found == null)  
             break;  
            out.println("<option>"
        + found.account_number+ "</option>");  
           }catch(Exception e){ System.out.println(e);
        break;}  
          }  
         
        out.println("</select></strong></font><font
        size=\"2\">");  
          out.println("<strong>
        </strong></font></p>");  
          out.println("<p
        align=\"center\"><font
        size=\"2\"><input
        type=\"submit\"");  
         
        out.println("name=\"transferButton\"
        value=\"Transfer\"></font></p>");
         
          out.println("</form>");  
         
        out.println("<hr>");  
         
        out.println("</BODY></HTML>");  
         }  
        }  
         
         
         | 
     
 
2. Define the servlet properties 
    
        | servlet.properties | 
     
    
        #
        @(#)servlets.properties 1.86 97/11/14 
        # 
        # Servlets Properties 
        # 
        # servlet.<name>.code=class name (foo or foo.class) 
        # servlet.<name>.initArgs=comma-delimited list of
        {name, value} pairs 
        # that can be accessed by the servlet using the 
        # servlet API calls 
        # 
        # session servlet 
        servlet.accounts.code=Accounts 
         
         | 
     
 
3. Make sure the EJBServer is up and running and Startup the servlet runner 
    
         
        E:\MyProjects\AccountEJB> 
         
        E:\MyProjects\AccountEJB>servletrunner -p 6060 -d
        E:\MyProjects\AccountEJB -s servlet.properties 
        servletrunner starting with settings: 
        port = 6060 
        backlog = 50 
        max handlers = 100 
        timeout = 5000 
        servlet dir = E:\MyProjects\AccountEJB 
        document dir = .\examples 
        servlet propfile = servlet.properties 
         
         | 
     
 
4. Open up your web browser - be it IE4.0x or Netscape 4.0x - and connect to the URL by typing the following into the Address 
    
         
        http://localhost:6060/servlet/Accounts 
         
         | 
     
 
 
What do you think of this article? 
Have your say about the article. You can make your point about the article by mailing [email protected] (If you haven't allready joined, you can join by going to https://www.onelist.com/community/dev-java). 
You can also write a review. We will publish the best ones here on this article. Send your review to [email protected]. Please include the title of the article you are reviewing. 
Further Reading 
	The Enterprise JavaBeans Series: 
	 Enterprise Java Beans By Gopalan Suresh Raj. 
	In this introduction to Enterprise Java Beans, Gopalan covers the bases then goes on to demonstrate how to build server side business object components. This article is the introduction to Gopalans series of Enterprise JavaBeans articles. (This series of articles is courtesy of Gopalan Suresh Raj) 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 6th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Enterprise JavaBeans, EJB Server, EJB Architecture, Java Naming and Directory Interface, Java Transaction Service. 
		Pre-required Reading: None 
	 Enterprise Java Beans Series - Components at the Server By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Server side components, CORBA 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Model By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Enterprise JavaBeans, EJB Server, EJB Containers, EJB Clients. 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Naming Services and JNDI By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Naming Services, Java Naming Directory Interface 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Transactions and JTS By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Java Transaction Service, Two-phase commits 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Lifecycle By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: EJB server provider, EJB container provider, EJB developer, EJB deployer, Application developer. 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Servers By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Server Infrastructure 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Containers By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: EJB Containers, EJB Servers 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Components By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: EJB Object, Home Object, Remote Object 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Session Beans By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Stateful Session Beans, Stateless Session Beans 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - EJB Entity Beans By Gopalan Suresh Raj. 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Container-Managed Persistence, Bean-Managed Persistence 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - Writing an Entity Bean By Gopalan Suresh Raj. 
	Part 1 of a four part series: A four tier bank account example 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Developing Entity Beans, Home & Remote Interfaces, Data Sources. 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - Writing a Session Bean By Gopalan Suresh Raj. 
	Part 2 of a four part series: A four tier bank account example 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: Developing Session Beans, Home & Remote Interfaces, Deployment Descriptors. 
		Pre-required Reading: Enterprise JavaBeans 	
	 Enterprise Java Beans Series - Writing an EJB Client By Gopalan Suresh Raj. 
	Part 3 of a four part series: A four tier bank account example 
	Author: Gopalan Suresh Raj 
		Date Submitted: January 11th 2000 
		Level of Difficulty: Advanced 
		Subjects Covered: EJB Clients. 
		Pre-required Reading: Enterprise JavaBeans 	
 
Author: Gopalan Suresh Raj 
Gopalan has his own site at Author Central (visit him. He also maintains his own site at https://www.execpc.com/~gopalan/) - Contribute to iDevResource.com and you can have one too! 
© Copyright 1997-2000 Gopalan Suresh Raj. Reproduced with Permission 
	 |