Solutions Products Services

Search
Download Contact Us
Documentation Site map
Support About Us
E-commerce SolutionsiPlanet



Creating a Session Enterprise Java Bean

In this section of the tutorial you will create an EJB that calculates the sales tax on the price of an item. This provides you with a simple example that takes you through the features provided by the iPlanet Application Server Integration Module.

To create a session EJB you must:

To create a valid session EJB:

  1. Create a directory named SalesTaxApp and two sub-directories named salestax and SalesClient.
  2. Mount a new directory SalesTaxApp\salestax
  3. Right click on the new directory and select New Package.
  4. Enter the name salesTax in the Create New Package window and select OK.
  5. NOTE: You must create your EJB within a Package in order to properly deploy it to the iPlanet Application Server.
  6. Select the salesTax node.
  7. Right click and select New | EJB | Session Bean.
  8. When the New Template wizard appears make sure Stateless and Container Managed Transaction type are selected and select Next.
  9. In the Name field, enter Sales.
  10. NOTE: If you have existing Java object files you wish to add to this EJB you can specify them by selecting the Modify button and navigate to the existing Java object file.
  11. Select Finish.
  12. Four new nodes appear under the salesTax node.

  13. Right click on the Sales (EJB) node and select New Business Method...
  14. Enter the following data for each field:

  15. Name calculateTax
    Return Type double
    Method Parameters Select the Add button to display the Enter Method Parameter dialog.

    Field Name: Amount
    Type: double


    NOTE: When entering Java classes as return types, you must specify the fully qualified class name. For example, if the return type is "String" then you must identify the Java class as (java.lang.String).

  16. Select OK.
  17. The Bean class and the Remote Interface class are modified accordingly.

    To view the new business methods, double click on the Sales and SalesEJB Bean Class Java files.

  18. Right click on the Sales (EJB) node and select Properties.

  19. Use the iPlanet AS tab to define iPlanet Application Server specific information for this session Enterprise Java Bean.

    Use the Properties and References tabs to edit J2EE standard meta data for this session Enterprise Java Bean.

  20. Right click on the Sales (EJB) node and select Compile EJB Classes.
  21. A compile error appears in the Output Window and in the status bar since you added a business method, but did not identify a return statement.

  22. From Output Window, double click on the error message.
  23. The Source Editor displays with the error location highlighted in green.

  24. Enter return Amount * 0.08; as the return statement.
  25. Right click on the Sales (EJB) node and select Compile EJB Classes once more.
  26. This time the compile should complete without any error messages. Look for a finished message in the status bar.

    You now have a valid session EJB and are ready to create the EJB module.

To create the EJB module:

  1. Right click on the Sales (EJB) node and select New EJB Module...
  2. Accept the default settings and click OK to create an EJB module named EJBModule_Sales.
  3. Expand the EJBModule_Sales node to view the EJB contained in this module.
  4. Right click on the EJBModule_Sales node and select Properties.
  5. Select the iPlanet AS tab if you wish to define iPlanet Application Server mapped security roles for the module.
  6. For more information on setting up the security roles see the iPlanet Application Server Integration Module online help Using Advanced Features section.

    You can export this EJB module (EJB jar file) or deploy this EJB module to the iPlanet Application Server. However, for purposes of this tutorial, you will deploy this EJB module as part of J2EE application.

    You are now ready to create a servlet that accesses this EJB module.
     
     

To create a servlet that accesses the EJB module:
  1. From the File menu, select New.
  2. From the Template Wizard, expand the JSP & Servlet node and select Web Module.
  3. Select Next.
  4. Select the directory "SalesTaxApp\SalesClient" that you created earlier for the Web Module.
  5. Select Finish.
  6. Select OK to dismiss the Mount Web Module dialog.
  7. Expand the SalesClient node and the WEB-INF node.
  8. Right click on the Classes node and select New Package.
  9. Enter taxApp in the name field and select OK.
  10. Expand the Classes Node to display the taxApp package you just created.
  11. Right click on the taxApp node and select New | JSP & Servlet | Servlet.
  12. Enter Calculator in the name field and select Finish.
  13. Add the following imports to the start of the Calculator.java file.
  14. import javax.ejb.*;
    import javax.naming.*;
  15. Edit the doGet and doPost methods to appear as follows:
  16.         protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
             response.setContentType("text/html");
             java.io.PrintWriter out = response.getWriter();
             out.println("<html>");
             out.println("<head>");
             out.println("<title>Input</title>");
             out.println("</head>");
             out.println("<body>");
             out.println("<form method=post action=\"/NASApp/SalesClient/Calculator\">");
             out.println("<h1>California sales tax calculator</h1>");
             out.println("Amount of sale is <input type=input name=newVal>");
             out.println("<input type=submit value=Submit name=submitButton>");
             out.println("</body>");
             out.println("</html>");
            }
            /** Handles the HTTP <code>POST</code> method.
            * @param request servlet request
            * @param response servlet response
            */
            protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
              String newVal = "0";
              String vals[] = request.getParameterValues("newVal");
              if (vals != null && vals.length > 0) {
                 newVal = vals[0];
              }
              Double salesAmount = new Double(newVal);
              response.setContentType("text/html");
              java.io.PrintWriter out = response.getWriter();
              out.println("<html>");
              out.println("<head>");
              out.println("<title>Output</title>");
              out.println("</head>");
              out.println("<body>");
              try {
                java.util.Properties p = new java.util.Properties();
                Context ctx = new InitialContext(p);
                Object beanObject=ctx.lookup("java:comp/env/Sales");
                salesTax.SalesHome home = (salesTax.SalesHome) beanObject;
                salesTax.Sales remote = (salesTax.Sales) home.create();
                double result = remote.calculateTax(salesAmount.doubleValue());
                out.println("<h1>The sales tax on "+salesAmount+" is "+result+"</h1>");
              }
              catch (Exception ex) {
                out.println("<pre>");
                out.println("<h1>Error</h1>");
                ex.printStackTrace(out);
                out.println("</pre>");
              }
              out.println("</body>");
              out.println("</html>");
            }
  17. Save the Calculator.java file.>
  18. Right click on the Calculator node and select Properties.
  19. Specify the URI field as "/Calculator".
  20. Right click on the web.xml node and select Properties.
  21. Select the Servlets field to display the Property Editor: Servlets dialog.
  22. Select the entry and select Edit to display the Edit Servlet dialog.
  23. Enter the following field data:
  24. Servlet Name Calculator
    Display Name Calculator
    Servlet ClasstaxApp.Calculator

  25. Select to display the Edit Servlet Mappings dialog.
  26. Select the entry and click the Edit button to display the Edit Servlet Mapping dialog.
  27. Enter the following field data:
  28. Servlet Name Calculator
    URL Pattern /Calculator

  29. Select OK to accept changes for both dialogs.
  30. Select OK on both the Edit Servlet and Property Editor: Servlets dialogs.
  31. Select the References tab on the Properties of web.xml window.
  32. Select the EJB references field.
  33. Select the  to open the EJB References Property Editor.
  34. Select Add to enter the following data in each field:
  35. Reference Name: Sales
    Description: Sales Tax Calculator
    Referenced EJB Name: Sales
    Type: Session
    Home Interface: salesTax.SalesHome
    Remote Interface: salesTax.Sales

  36. Select iPlanet App Server tab.
  37. Specify the following for the field JNDI Name: ejb/EJBModule_Sales/Sales
  38. Select OK.
  39. The EJB Reference Property Editor now appears with the edits you just made as follows:

  40. Select OK.
  41. Select WEB-INF node and specify the Context Root field as SalesClient on Properties sheet.
  42. Expand the taxApp node to display the Calculator node.
  43. Right click on the Calculator node and select Compile.
    1.  
    Look for the message Finished Calculator in the status bar to identify the compilation was successful.

To create J2EE application:
  1. Mount directory SalesTaxApp.
  2. Select the SalesTaxApp directory node, right click and select New | J2EE | Application.
  3. Enter name as SalesTaxApp and then select Finish.
  4. Select the SalesTaxApp application node, right click and select Add Module.... Navigate to the salestax/salesTax/EJBModule_Sales node, select and click OK to add EJB module.
  5. Repeat the previous step to add SalesClient web module by selecting the \SalesTaxApp\SalesClient\WEB-INF node and click OK to add web module
  6. Select SalesTaxApp application node, right click and select Deploy.

  7. The Progress Monitor dialog is displayed. It will disappear when the deployment process is finished.
     

  8. Right click on the Calculator servlet node of the SalesClient web module and select Execute.

  9. The Web Browser launches and displays the following screen.

  10. Enter a number to calculate the tax.

  11. The browser displays the result.

The next step in the iPlanet Application Server Integration Module Tutorial is creating a container managed persistence Enterprise JavaBean.

 
   

Legal Notices


Search