Web Application/EJB 3.0 "Hello, <USER>!"

This entry covers the "usual" first Java EE 5 application: Hello, World.

The first thing you need to do is create a new project that will be deployed onto the GlassFish instance that you registered earlier.

There are a couple different approaches you could take here. I recommend that you create an Enterprise Application, and let the wizard create the Web Application subproject. Part of the reason for this is lets you be lazy later on. While you may like creating servlets by hand, I like to let the wizards do it. Since the Servlet wizard is associated with Web Application projects, using an approach that doesn't use the Web Application project will mean you need to do more work.

One other thing to notice about the wizard is the fact that I have 'Set Source Level to 1.4' UNCHECKED. This will allow you to use annotation later on, so make sure you have it unchecked, too.

After the wizard finishes, create a java library project. It is going to be the project where you do the EJB 3.0 devlopment.

The Web Application and Java Library project needs to have access to the javaee.jar file, which is in <GlassFish-Install>/lib. It has the definition of the various Java EE 5 annotations that you'll use later on. You use the Project Properties dialog to add this jar file to the Web Application and Java Library projects.


This image is from the Web Application project.

The two Project Properties dialogs are very similar. You will see an additional column in the Web Application project's properties dialog. Make sure 'Package' is unchecked when you add javaee.jar to the Web Application project. Otherwise, the jar will be packaged in your web application's WEB-INF/lib directory, which is redundant....

You need to add the Java Library project to the compile properties of the Web Application, and the packaging properties of the Enterprise Application project. (Don't forget to uncheck the 'Package' attribute when you add the library project to the Web Application project).


Web Application Properties


Enterprise Application Properties

You need to add the Java Library that will implement your EJB 3.0 beans to the application.xml manually. Double clicking on the application.xml node, shown below, will open the file in an XML editor. It is easy to add the following:

    <module><ejb>Greeter.jar</ejb></module>
    
immediately following the module entry for PleasedToMeetYou.war.


Expanded Enterprise Application Project: application.xml visible

At this point the infrastructure is in place and it is time to start writing Java EE 5 code. The coding required to create an EJB 3.0 bean is wonderfully concise (compared to EJB 2.1).

First, we define the interface that says what a bean can do. This the text of the interface that I created.

    package beans;
    
    import javax.ejb.Local;
    
    @Local public interface FriendlyGreeting {   
        public String returnFriendlyGreeting(String name);    
    }
    

After defining the interface we need to create the implementation. That is relatively straight-forward, too.

    package impl;
    
    import beans.FriendlyGreeting;
    import javax.ejb.Local;
    import javax.ejb.Stateless;
    
    @Stateless public class GreetingImpl implements FriendlyGreeting {
        
        public String returnFriendlyGreeting(String name) {
            return "Well, good to see you again, "+name+"!";
        }
        
    }
    
Since NetBeans supports annotation and NetBeans 5.0 has some significant editor improvements, writing this code is easy. Since there are fewer interrelated files, many of the wizards that create EJB 2.1 artifacts seem heavy.
The one exception would probably be in the development of entity beans, which still require a lot of meta-data.

Once the bean is ready, it is time to call it. In my example, I am going to create a servlet to do that. Other folks have demonstrated other methods for calling your EJB 3.0 bean from a Web Application.

The easy way to create the servlet is to use the New item from the Web Application project's right-click menu.

Here is the text of my servlet.

    package lets;
    
    import beans.FriendlyGreeting;
    import java.io.\*;
    import java.net.\*;
    import javax.ejb.EJB;
    
    import javax.servlet.\*;
    import javax.servlet.http.\*; 
    
    public class EJB3Greeting extends HttpServlet {
        
        @EJB FriendlyGreeting bean;
        
        /\*\* Processes requests for both HTTP GET and POST methods.
         \* @param request servlet request
         \* @param response servlet response
         \*/
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            //\* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet EJB3Greeting</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet EJB3Greeting at " + request.getContextPath () + "</h1>"+
                    bean.returnFriendlyGreeting(request.getParameter("name")));
            out.println("</body>");
            out.println("</html>");
             //\*/
            out.close();
        }
    
I have left out the boilerplate code that NetBeans has put into a code fold. If you want to see it, click on the boxed-plus icon that appears along the left hand side of the editor (after you use the wizard to create servlet).

At this point we are almost done. There is still one thing that needs to be changed and NetBeans has been finicky lately when I do this edit. You need to change the text of the web.xml, so that it refers to a Java EE 5 schema, instead of the J2EE 1.4 schema. The thing to do is replace the initial web-app element (which will look something like:

<web-app version="2.4" 
         xmlns="http://java.sun. com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
with somehting like this
<web-app version="2.5" 
         xmlns="http://java.sun. com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
The easiest way to make this stick is to copy the text above and paste it over the "old" web-app element in the web.xml file.

At this point you are ready to run the application. The thing to do is make sure that the project SampleOne is the Main Project. You can use the 'Set Main Project' item from the Enterprise Application right click menu to make it so. If it is the main project, then you can use the F6 key, to trigger the 'Run Main Project' item of the Run menu.

The IDE will start the server, deploy the application and open the browser. It will trigger the default welcome-file, index.jsp. If you append "/EJB3Greeting?name=foobar" to the URL, you will see this:

After going through this initial exercise, I would say that the Java EE 5 has done a good job of simplifying 'Hello, World!'. Java EE 5 covers a lot more technologies that what I've used here. I will look at some of these other technologies and see wheher they have the improved their ease of development.

Comments:

Post a Comment:
Comments are closed for this entry.
About


Vince Kraemer writes the entries in this blog.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
News
Blogroll

No bookmarks in folder

automarks