EJB 3.1 Servlet Web Application

EJB 3.1 Servlet Web Application

In this tutorial you will see how to develop Hello World web application using EJB 3.1 with servlet. Below is new feature of EJB 3.1:

  • Main feature of EJB 3.1 is no interfaces are needed. When you develop Stateful or Stateless bean just you need to annotate with @Stateless or @Stateful. This minimizes headache of creating interfaces every time and define all methods from the bean class.
  • Also if you are using this version you don’t need to package your application inside EAR file and you could include your EJB inside WAR file and deploy on the server. You will see in this demo we will deploy EJB as web application.

Below are needed:

  • JBoss Application Server 6.1 community version
  • Eclipse 3.3 or up (For this demo we have used Kepler)

Steps:

  • Create Dynamic web project name: EJB. Below are steps:

2014-12-21_13092014-12-21_2203 2014-12-21_2203_001 2014-12-21_2204

  •  Final project structure:

2014-12-21_2205

  • EJBNoInterfaceBean.java
package ejb;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class NoInterfaceBean
 */
@Stateless
@LocalBean
public class EJBNoInterfaceBean {

    /**
     * Default constructor. 
     */
    public EJBNoInterfaceBean() {
        // TODO Auto-generated constructor stub
    }
    
    public String ejb3_1HelloWorld(String name)
    {
       return "Hello World EJB 3.1 " + name;
    }

}
  • EJBHelloServlet.java
package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;

import ejb.EJBNoInterfaceBean;

/**
 * Servlet implementation class EJBHelloServlet
 */
@WebServlet(description = "EJBHelloServlet", urlPatterns = { "/EJBHelloServlet" })
public class EJBHelloServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@EJB(mappedName = "EJBNoInterfaceBean/no-interface")
	EJBNoInterfaceBean eJBNoInterfaceBean;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EJBHelloServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		try {
			out.println(eJBNoInterfaceBean.ejb3_1HelloWorld("using JBoss 6 Application server"));
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
  • To run this project you will have to configure JBoss server to eclipse and if are not sure how to do it please use this tutorial. Now to run project on JBoss server –> Right click Project –> Run As –> Run on Server
  • Once server is started please enter below URL of servlet to get output from EJB:

2014-12-21_2210

  • For more information on EJB 3 please use this link

download Download Project: EJB

Leave a Reply

Your email address will not be published. Required fields are marked *