Servlets and EJBs






In this section you will learn how to create Servlet and Ejb classes in your Class Diagram.
If you want to use the J2ee profile on existing Servlet or Ejb in your Class Diagram, you must ensure that servlet classes have the servlet stereotype and Ejb classes have the bean stereotype.
No other names are currently allowed if you wish to activate Servlet and Ejb XDoclet tab in the class properties dialog.


This section covers the following elements of the tutorial:
  1. Create a Package
  2. Create a Servlet
  3. Create an Ejb
  4. Create an Ejb method

1. Create a Package


Create a new Package.
Click on the src folder and open the popup menu->New->Package





Enter the name of the Package and click on the Finish button.





Create a class diagram inside the tutorial package.
Select Tutorial and open the popup menu->UML->Class diagram editor





2. Create a Servlet


Now that the Tutorial.ucd class diagram has been created in the Package explorer and in the UML editor, we are going to work inside this class diagram and model our Servlets and EJBs.
Create a new Servlet.
Use the Class Diagram popup menu->New->Servlet





Open the Servlet Creation wizard and set up Class properties.
Enter the Class name HelloServlet and click on the Next button.





We are going to customize the default XDoclet tags. These default tags come from the J2ee XDoclet template preferences.
Double clicking on the name tag property will allow you to give it a value.





Enter the value of the tag.
Enter hello and click on the OK button.





Enter the url-pattern name.
Enter /hello and click on the Finish button.





Our HelloServlet Class has been created and its stereotype is servlet.





We want to have a look at the Java code.
Double click on the class and get the following picture.





Copy the Servlet method from below and paste it in the Eclipse Java editor.

protected void doGet(HttpServletRequest arg0, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><head><title>");
        out.println("J2ee tutorial");
        out.println("</title></head>");
        out.println("<body>");
        out.println("Servlet says hello.");
        out.println("</body>");
        out.println("</html>");
        out.close();
}



Save the Java code and have a look at errors. Each bulb light represents an error.



Correct errors by double clicking on the bulb and selecting the answer in the contextual menu.
Please correct all bugs before processing the end of the tutorial.
The easiest way is to use the organize imports function.
Open the source code popup menu->source->Organize Imports.





3. Create an Ejb


Create a new Ejb.
Use the Class Diagram popup menu->New->Ejb





Open the Ejb Creation wizard and set up Class properties.
Enter the Class name HelloBean, select the EJB type and add interfaces accordingly.
You will not be able to click on the next button till the correct interface has been chosen.
We need to click on the add button.





Select all required interfaces, by typing the first letter of the name in the Choose interfaces field and click on the Ok button.
We selected SessionBean.





The new Ejb Class is almost created, but we are still not ready to click on the Finish button.
We decide that HelloBean will be a Stateless Session.
We click on the Next button.





We can customize the default tags. These default tags come from J2ee XDoclet template preferences.
Double clicking on tag properties will allow you to give them a value.
Please give a name to all tags, as shown below.





We have finally created our EJB and our Servlet inside our Class Diagram Editor.





4. Add 'business' and 'create' methods to Ejb


First add a 'create' method. Select a Class with bean as a stereotype and use the Class Diagram popup menu->New->Ejb create method





The method stereotype should be create in order to have access to the XDoclet properties. If you change the stereotype, then the XDoclet tab will disappear.





We are going to create an Ejb business method.
Select the HelloBean class and open the popup menu.
Select New->Ejb Method





The method stereotype should be business in order to have access to the XDoclet properties. If you change the name of this specific stereotype, then the XDoclet tab will disappear.





Select the Implementation Tab and type the following instruction:





We have successfully created our bean. We can see the following methods inside the class:
  • EjbCreate method with create stereotype
  • SayHello method with business stereotype









 
Last update Sat Nov 29 11:19:25 CEST 2003 Valid XHTML 1.0 Valid CSS All text, graphics © 2003 by Omondo