Part 6: Creating a Session Enterprise Java Bean

  1. From the Explorer, select or mount a new directory.

    In this example, the directory name is "EJB".

  2. Right click on the new EJB directory and select New Package.
  3. In the Name field of the Create New Package dialog, enter "salesTax".

    You must create your EJB within a Package in order to properly deploy it to the iPlanet Application Server.

    A question dialog appears as follows:

  4. Select Yes to dismiss the dialog.
  5. From the Explorer, select the salesTax directory.
  6. Right click and select New -> EJB -> Session Bean.

    The Session EJB wizard appears.

  7. Accept the default settings and select Next.
  8. In the Name field, enter "Sales".

    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.

  9. Select Finish.

    Four new nodes appear under the salesTax node.

  10. Select the EJB Sales node and expand it.
  11. Select each subnode and expand it.

    The following EJB structure is displayed:

  12. Select the EJB Sales node and right click to display the context menu.
  13. Select New Business Method...
  14. Enter the following data for each field:

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

    Type: double
    Name: amount

    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).

  15. Select OK.

    The Bean class and the Remote Interface are modified accordingly.

    To view the new business methods, double click on the Sales and SalesBean Java files.

  16. Select the EJB Sales node.
  17. Right click to display the context menu and select Properties.

  18. Select the Application Servers field to display the button and select it.

    The Application Servers Property Editor appears:

  19. Be sure the iPlanet Application Server checkbox is checked.
  20. Select OK to dismiss the dialog.

    You have now targeted your session Enterprise Java Bean to the iPlanet Application Server.

    Notice that an iPlanet AS tab appears in the Properties window.

    Use the iPlanet AS tab to define IAS 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.

  21. Close the property window.
  22. From the Explorer, select the EJB Sales node.
  23. Right click to display the context menu and select Compile EJB Classes...

    A compile error appears in the Output Window since you added a business method, but did not identify a return statement.

  24. From Output Window, double click on the error message.

    The Source Editor displays with the error location highlighted in green.

  25. Enter return amount * 0.08; as the return statement.
  26. Select the EJB Sales node once more and right click to select Compile EJB Classes... from the context menu once more.

    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.jar package.

  27. Select the EJB Sales node and right click to select New EJB Module from the context menu.

    This shortcut creates a new EJB module containing the currently selected EJB.

    Notice that an EJBModule_Sales node appears in the explorer.

  28. Expand the EJBModule_Sales node to view the list of EJBs contained in this module.

  29. Right click on the EJBModule_Sales node and select Properties from the context menu.
  30. Select the iPlanet AS tab if you wish to define the iAS mapped security roles for the module:

    For more information on setting up the security roles see the iPlanet Integration Tool online help Advanced Features section.

  31. Right click on the EJBModule_Sales node and select Export EJB Jar File... from the context menu.

  32. Navigate to the directory where you wish to save the EJB Jar file and save it.

    You can also deploy the EJB Jar file to a default iPlanet Application Server instance.

  33. Right click on the EJBModule_Sales node and select Deploy EJB Module to iPlanet App Server... from the context menu.

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

  34. From the File menu, select New.
  35. From the Template Chooser, expand the JSP & Servlet node and select WebModule.
  36. Select Next.

  37. Select the button to create a new directory.
  38. Create a directory named Calculator and select Add.
  39. Select Finish, then OK.
  40. Right click on the Calculator node to select Tools -> Switch Web Execution to iPlanet Application Server from the context menu.

  41. From the Explorer, expand the Calculator node and the WEB-INF node.
  42. Right click on the Classes node and select New Package from the context menu.
  43. In the Create New Package Name field enter taxApp and select OK.

    A question dialog appears similar to the following:

  44. Select Yes to dismiss the dialog.
  45. Right click on the taxApp node and select New -> JSP & Servlet -> Servlet.

  46. Enter Calculator in the name field and select Finish.
  47. Add the following imports to the start of the Calculator.java file.

    import javax.ejb.*;
    import javax.naming.*;
    

  48. Edit the doGet and doPost methods as follows:

    	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/Calculator/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>");
    	}
    

  49. Right click on the Web node and select Open using the iPlanet App Server customizer from the context menu.

  50. Select the References tab.
  51. Select the Add button (at the bottom of the window) to add a reference to to an EJB defined elsewhere.
  52. Enter the following data in each field:

    Reference The name of the EJB as specified in your code. In this example, the name is Sales.
    Linked to Bean The name of the bean used to generate the JNDI name. In this Example the name is EJBModule_Sales/Sales.
    Bean Type The type of bean you are creating is a session bean.
    Bean Home Interface The actual class name of the Home Interface. In this example the name is salesTax.SalesHome.
    Bean Remote Interface The actual class name of the Remote Interface. In this example the name is salesTax.Sales.

    Once you enter the data in each field, be sure to press Enter so your entry can take effect.

  53. Select OK.
  54. Right click on the Calculator file and select Compile from the context menu.

    You must compile before deploying for the first time.

  55. Right click on the Calculator file once more and select Execute from the context menu.

    The WAR is packaged and deployed. The Forte Web Browser launches and displays the following screen.

  56. Enter a number to calculate the tax.

    The browser displays the result.

    You can also debug into the servlet and EJB. Just set a breakpoint in the servlet and EJB and select start debugging from the servlet.

See also
  Creating a Container Managed Persistence Entity Java Bean


Legal Notices