Stateless Session Bean

Below example is demostration of converting Currency from dollar to rupees, euro, pond, yen or rubble.
The following components are used :
  • Component (Remote) Interface - Defines the client view of the Bean and corresponding business methods for remote client (different JVM).
  • Component (Local) Interface - Defines the client view of the Bean and corresponding business methods for local client (same JVM). [ You deploy EJB (.jar) and web (.war) in the form of .ear on Application Server ]
  • Bean Class - It must contain the bean callback methods, implementation of Component (Remote/Local) interface methods and any optional life-cycle methods.
The implementation of the callback methods is different depending on whether it is a stateful or stateless.
[ Note : No need Deployment descriptor EJB Configuration XML file ]

Below steps are to create Stateless session bean :

  • Install Oracle WebLogic Server 10.3.2
  • Create Domain on Orcale WebLogic Server 10.3.2
  • Configure Domain in Eclipse
  • Create Enterprise Application StatelessDemo1.ear.
  • Create Stateless Bean Local interface in EJB project
  • Create Stateless Bean class in EJB project
  • Create index.jsp in Web project
  • Create Servlet in Web project
  • Run the Application

Step 1: Install Oracle WebLogic Server 10.3.2 [ Click here - How to install WebLogic Server ]

Step 2: Create Domain on Orcale WebLogic Server 10.3.2 [ Click here - How to create Domain on WebLogic Server ]

Step 3: Configure Domain in Eclipse [ Click here - How to Configure Domain in Eclipse ]

Step 4: Create Enterprise Application StatelessDemo1
File -> New -> Enterprise Appliction Project :
create Enterprise APplication Project
Specify the Enterprise Project name StatelessDemo1 and click Next.

Below screen is used to create EJB and Web module in Enterprise Application project. Click New Module :
create Enterprise APplication Project

Check EJB Module and Web Module and Click Finish :
Create EJB and Web Module in Enterprise APplication Project

You will get below screen and again click Finish :
Create EJB and Web Module in Enterprise APplication Project
This step will create Enterprise project with EJB and Web project.

Step 5: Create Stateless Session Bean in EJB project
Right click on EJB project -> New -> Session Bean (EJB3.X) :
Stateless Session Bean

Specify the below information and click Next:
Java Package : techmyguru.stateless
Class name : SessionConcerter
State Type : stateless
Local : techmyguru.stateless.SessionConverterLocal
Stateless Session Bean

Verify the information and click Finish

This step will create two java file. One will be local interface SessionConverterLocal.java and other will be bean class SessionConverter.java.

Edit bothe class java file as below :
SessionConverterLocal.java
package techmyguru.stateless;
import javax.ejb.Local;
@Local
public interface SessionConverterLocal 
{
  public double convert(String currType, String currValue);
}

SessionConverter.java
package techmyguru.stateless;
import javax.ejb.Stateless;
@Stateless(mappedName = "statelessjndi")
public class SessionConverter implements SessionConverterLocal {
  /*
  rupees - 47
  euro - 42
  pond - 5
  yen- 10
  ruble - 50
  */
@Override
public double convert(String currType, String currValue) {
  double retValue = 0.0;
  Double d = new Double(currValue);
  if (currType.equals("rupees"))
    retValue = (double47 * d.doubleValue();
  if (currType.equals("euro"))
    retValue = (double42 * d.doubleValue();
  if (currType.equals("pond"))
    retValue = (double* d.doubleValue();
  if (currType.equals("yen"))
    retValue = (double10 * d.doubleValue();
  if (currType.equals("ruble"))
    retValue = (double50 * d.doubleValue();
  return retValue;
}
}

Step 7: Add EJB Project to Web project
Right Click on Web Project - > Properties - > Java Build Path -> Click Projects -> Add -> Select StatelessDemo1EJB. Add EJB Project to Web project

Step 8: Create index.jsp in Web project
<html>
<head>
<title>Access Stateless Session Bean</title>
</head>
<body>
<h3>Currency Convert Form (Dollar to selected currency)</h3>
<form action="Servlet1" method="POST">
 <table border="0">
   <tr>
    <td>Enter Currency ($)</td>
    <td><input type="text" name="currValue" value="0" /></td>
   </tr>
   <tr>
    <td>Select Desired currency  type</td>
    <td><select name="currType">
      <option value="SelectOne">Select One</option>
          <option value="rupees">rupees</option>
          <option value="euro">euro</option>
          <option value="pond">pond</option>
          <option value="yen">yen</option>
          <option value="ruble">ruble</option>
        </select></td>
      </tr>
      <tr>
          <td><input type="submit" value="Convert" /></td>
          <td><input type="reset" value="Reset" /></td>
      </tr>
</table>
</form>
</body>
</html>

Step 9: Create Servlet Servlet1.java in Web project
package web.techmyguru;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import techmyguru.stateless.SessionConverterLocal;

public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

@EJB
private SessionConverterLocal local;

protected void doPost(HttpServletRequest request,HttpServletResponse response
throws ServletException, IOException {
  double result = 0.0;
  String cType = request.getParameter("currType");
  String cValue = request.getParameter("currValue");
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<head>");
  out.println("<title>Servlet convertEJB</title>");
  out.println("</head>");
  out.println("<body>");
  out.println("<h1> The dollar is converted to " + cType);
  result = local.convert(cType, cValue);
  out.println("" + result);
  out.println("<br><a href=index.jsp>Try again</a>");
  out.println("</h1></body>");
  out.println("</html>");
  out.close();
}
}

Step 10: Verify the directory structure
Enterprise Application Project

Step 11: Run the Application
Right click on Web Project -> Run As -> Run on Server
Run Enterprise Application Project
Click Next

Run Enterprise Application Project
Click Next

Run Enterprise Application Project
Click Next

Run Enterprise Application Project
Click Finish

You will get below output :
Run Enterprise Application Project
Enter the number of dollar and select the desired currency type in which you want to convert and click Convert .

You will get below output :
Run Enterprise Application Project


Previous Download Source Code Next

Comment by : Juan October 3rd, 2015
Comment : Excellent read. I just passed this onto a buddy who was doing a lttlie research on that. He just bought me lunch since I found it for him! Thus let me rephrase: Thank you for lunch!

Comment by : Sudhir February 19th, 2015
Comment : Hi, Many thanks for the lovely tutorial. Unfortunately null pointer exception is coming up at line where bean method is accessed. Do we need to use any JNDI lookup?? Please help out.

Comment by : Ahmed October 22nd, 2012
Comment : I want to check the url content please

Comment by : shilpa October 22nd, 2012
Comment : need ejb3


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code