Skip to main content

TOTD #81: Getting Started with Servlet 3.0 and EJB 3.1 in Java EE 6 using NetBeans 6.7

Posted by arungupta on May 19, 2009 at 11:00 AM PDT



EJB 3.1 (JSR
318
) and Servlet 3.0 ( href="http://jcp.org/en/jsr/detail?id=315">JSR 315)
are the two new JSRs in Java EE 6 ( href="http://jcp.org/en/jsr/detail?id=316">JSR 316).



The EJB 3.1 specification provides multiple new features such as href="http://blogs.sun.com/arungupta/entry/ejbs_in_a_war_simplified">WAR
packaging, href="http://blogs.sun.com/kensaks/entry/optional_local_business_interfaces">Optional
Local Business Interfaces, EJB.lite, href="http://blogs.sun.com/kensaks/entry/portable_global_jndi_names">Portable
Global JNDI Names, href="http://blogs.sun.com/MaheshKannan/entry/singleton_ejb_support_in_glassfish">Singleton
Session Beans
(Container-managed and Bean-managed concurrency), Application
Initialization and Shutdown events, Timer Service enhancements,
Simple/Light-weight Asynchrony, and many other features defined in the href="http://jcp.org/en/jsr/detail?id=318">specification.



The
Servlet 3.0 specification is an update to Servlet 2.5 and focuses on
ease-of-use. It also adds several new features such as href="http://blogs.sun.com/swchan/entry/servlet_3_0_annotations">"web.xml"
free
deployment (mostly), Dynamic Registration of
servlets/filters,
Pluggability
of frameworks using "web-fragment.xml"
, Asynchronous API,
href="http://blogs.sun.com/swchan/entry/servlet_3_0_security_annotations">Security
enhancements (Constraints via annotations, programmatic
container authentication and logout), and several other miscellaneous
additions like default error page, file upload, etc.



GlassFish v3
provides the most complete implementation of EJB 3.1 and Servlet 3.0
along with other Java EE 6 specifications. This style="font-weight: bold;">Tip style="font-weight: bold;">Of style="font-weight: bold;">The style="font-weight: bold;">Day (TOTD) will show
how to create a simple EJB and invoke it from a Servlet, all in a
deployment-descriptor free way.

  1. Enable support for v3 Preview in NetBeans
    1. Using href="http://bits.netbeans.org/download/trunk/nightly/latest/">NetBeans
      6.7 latest nightly, enable support for recent GlassFish v3
      builds either using the href="http://blogs.sun.com/vkraemer/entry/7_0_and_glassfish_v3">command-line
      switch or href="http://blogs.sun.com/vkraemer/entry/experimental_features_marker">the
      marker module.
    2. Download and unzip href="http://download.java.net/glassfish/v3-preview/promoted/glassfish-v3-preview-b47b.zip">GlassFish
      v3 Preview 47b. The latest promoted builds are always
      available href="http://download.java.net/glassfish/v3-preview/promoted/">here.
    3. In the "Services" tab, right-click on "Servers" and click
      on "Add Server". Select "GlassFish v3" as shown below:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/images/nb67b-v3-registration.png">



      and click on "Next".
    4. Specify location of the previously unzipped bundle, click
      on "Next >", and press "Finish".
  2. Create
    a new Web project by right-click in the "Projects" pane, select "New
    Project", choose "Java Web" and "Web  Application" as
    categories
    and projects.
  3. Click on "Next >", choose "Java EE 5" as the
    Java EE version and click on "Finish". A future version of NetBeans
    will will provide direct support for Java EE 6.
  4. Add a POJO-based EJB
    1. Right-click on "Source Packages" and select "New", "Java
      Class..." as shown below:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/images/nb67-v3-new-class.png">



      Give the class name as "HelloEJB" and package as "server" as shown
      below:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/images/nb67-v3-new-class-ejb.png">



      and click on "Finish".
    2. Add
      "@Stateless" class-level annotation and press Shift+Command+I (default
      shortcut) to fix the imports. This annotation comes from the
      "javax.ejb" package.
    3. Add the following method:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
          public String
      sayHello(String name) {

             
      return "Hello " + name;

          }



      to
      the class. And can you believe it, that's your complete EJB ready to be
      deployed and that too in a WAR file - the beauty of Java EE 6. The
      complete class looks like:




      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      package server;



      import javax.ejb.Stateless;



      /**

       * @author arungupta

       */

      @Stateless

      public class HelloEJB {

          public String sayHello(String name) {

             
      return "Hello " + name;

          }

      }
  5. Add a Servlet to invoke this EJB
    1. Add a new class "HelloServlet" in the "server" package as
      explained above.
    2. Add
      "@WebServlet" class-level annotation and Shift+Command+I to fix the
      imports. This annotation comes from the "javax.servlet.annotation"
      package. And specify a URL pattern as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      @WebServlet(urlPatterns="/hello")
    3. According to the Servlet3 specification, the contract is
      inherited from the "javax.servlet.http.HttpServlet" interface. So add:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      extends HttpServlet



      to the class.

    4. Inject a local EJB reference using the code:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      @EJB HelloEJB ejbClient;
    5. Override the GET method as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
          @Override

          public void doGet(HttpServletRequest
      req, HttpServletResponse res) throws IOException {

             
      res.setContentType("text/html");

             
      res.getOutputStream().print("<h1>Hosted at: " +
      req.getContextPath() + "</h1>");

             
      res.getOutputStream().print("<h2>" +
      ejbClient.sayHello("Duke") +
      "</h2>");

          }



      and again Shift+Command+I to fix the imports. The complete class looks
      like:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      package server;



      import java.io.IOException;

      import javax.ejb.EJB;

      import javax.servlet.annotation.WebServlet;

      import javax.servlet.http.HttpServlet;

      import javax.servlet.http.HttpServletRequest;

      import javax.servlet.http.HttpServletResponse;



      /**

       * @author arungupta

       */

      @WebServlet(urlPatterns="/hello")

      public class HelloServlet extends HttpServlet {

          @EJB HelloEJB ejbClient;



          @Override

          public void doGet(HttpServletRequest
      req, HttpServletResponse res) throws IOException {

             
      res.setContentType("text/html");

             
      res.getOutputStream().print("<h1>Hosted at: " +
      req.getContextPath() + "</h1>");

             
      res.getOutputStream().print("<h2>" +
      ejbClient.sayHello("Duke") + "</h2>");

          }

      }

That
completes the project creation. Now lets make our application
deployment descriptor free by expanding "WEB-INF" directory and
deleting "sun-web.xml" and "web.xml". Java EE 6 makes the deployment
descriptors optional by introducing equivalent annotations.



Lets run the project by right-click on the
project and select "Run". The web application is deployed to GlassFish
v3 Preview 47b and "http://localhost:8080/WebApplication1" shows the
default "index.jsp" created by the IDE.



Our servlet is accessible at
"http://localhost:8080/WebApplication1/hello" and shows the output as:



alt=""
src="http://blogs.sun.com/arungupta/resource/images/nb67-v3-servlet3-output.png">



The directory of the generated WAR file looks like:



alt=""
src="http://blogs.sun.com/arungupta/resource/images/nb67-v3-servlet3-dir-structure.png">



As evident "WEB-INF/classes" has only two POJO classes and yet this is
a Java EE 6 application.



So we created a trivial Java EE 6 application using Servlet 3 and EJB
3.1 APIs and deployed successfully on GlassFish v3 Preview 47b using
NetBeans 6.7.



Please leave suggestions on other TOTD (Tip Of The Day) that
you'd like to see.
A complete archive of all the tips is available href="http://blogs.sun.com/arungupta/tags/totd">here.




Technorati: href="http://technorati.com/tags/totd">totd
href="http://technorati.com/tags/glassfish">glassfish
v3 href="http://technorati.com/tags/javaee6">javaee6 href="http://technorati.com/tags/servlet3">servlet3
ejb3.1
netbeans

Related Topics >>