EJB 3 injection in Servlet


    Hi,

     

    This article is about a Servlet call to injected EJB3 Session Bean.

    The application in attachment works fine with JBoss-5.1.0.GA_JDK6/ JDK1.6.0_21.

     

    The application can be reached at http://<IPAddress>:8080/servletejb3
    Welcome file is a jsp file, which submits a form with user input to the servlet.
    The session bean is injected into the UserController servlet using  @EJB(name = "sessionBean") annotation.

    Note that the JNDI lookup codes are replaced by ejb injection.

     

    Schema of web.xml:

     

    <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">
         

    Schema of ejb-jar.xml:

     

    <ejb-jar 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/ejb-jar_3_0.xsd"
             version="3.0">

     

    Schema of application.xml:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <application 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/application_5.xsd"
                 version="5">

     

    Extract the attachment and run ant deploy after modifying the script
    to point to your environment's jboss home.

     

    Fire up the JBoss and pull up the app.

     

    Thanks

     


    Saravana Prasad