Introduction
|
- To improve performance, you might choose a stateless session bean if it has any of these traits:
-
The bean’s state has no data for a specific client.
-
In a single method invocation, the bean performs a generic task for all clients. For example, you might use a stateless session bean to send an email that confirms an online order.
-
The bean fetches from a database a set of read-only data that is often used by clients. Such a bean, for example, could retrieve the table rows that represent the products that are on sale this month.
- Types of Enterprise Beans are
-
- Session Beans
- Stateless Session Beans
- Stateful Session Beans
- Entity Beans
- Bean Managed Persistent (BMP) Entity Beans
- Container Managed Persistent (CMP) Entity Beans
- Message-Driven Bean
CLICK HERE to download this complete example (zip file)
|
|
Create the Stateless Session Bean Home Interface (LoanHome.java)
|
- import javax.ejb.EJBHome;
-
public interface LoanHome extends EJBHome
-
{
-
public Loan create() throws java.rmi.RemoteException, javax.ejb.CreateException;
-
}
Download: LoanHome.java
|
Create the Stateless Session Bean Remote Interface (Loan.java)
|
- import javax.ejb.EJBObject;
-
public interface Loan extends EJBObject
-
{
-
public float calculateInterest(float rate, float time, float amount) throws java.rmi.RemoteException;
-
}
Download: Loan.java
|
Create the Stateless Session Bean Class (LoanBean.java)
|
- import java.util.*;
-
import java.io.*;
-
import javax.ejb.SessionBean;
-
import javax.ejb.SessionContext;
-
public class LoanBean implements SessionBean
-
{
-
private javax.ejb.SessionContext m_ctx = null;
-
public void setSessionContext(SessionContext ctx)
-
{
-
m_ctx = ctx;
-
}
-
public void ejbCreate() throws java.rmi.RemoteException, javax.ejb.CreateException
-
{
-
System.out.println("ejbCreate() on obj " + this);
-
}
-
public void ejbRemove()
-
{
-
System.out.println("ejbRemove() on obj " + this);
-
}
-
public void ejbActivate()
-
{
-
System.out.println("ejbActivate() on obj " + this);
-
}
-
public void ejbPassivate()
-
{
-
System.out.println("ejbPassivate() on obj " + this);
-
}
-
public float calculateInterest(float rate, float time, float amount) throws java.rmi.RemoteException
-
{
-
float interest = time * amount * (rate / 100);
-
return interest;
-
}
-
}
Download: LoanBean.java
|
Create the Web Client HTML File (Loan.html)
|
-
<HTML>
-
<BODY>
-
<FONT SIZE= 10 ALIGN=CENTER> <B> Calculate Interest </B> </FONT>
-
<FORM ACTION = "http://127.0.0.1:8080/loanctx/servlet/LoanServlet">
-
<TABLE>
-
<TR>
-
<TD> RATE </TD>
-
<TD> <INPUT TYPE = TEXT NAME = "rate"> </TD>
-
</TR>
-
<TR>
-
<TD> TIME </TD>
-
<TD> <INPUT TYPE = TEXT NAME = "time"> </TD>
-
</TR>
-
<TR>
-
<TD> AMOUNT </TD>
-
<TD><INPUT TYPE = TEXT NAME = "amount"> </TD>
-
</TR>
-
<TR>
-
<TD><INPUT TYPE = SUBMIT VALUE = "Calculate"></TD>
-
</TR>
-
</TABLE>
-
</FORM>
-
</BODY>
-
</HTML>
Download: Loan.html
|
Create the Web Client Servlet (LoanServlet.java)
|
-
import java.io.*;
-
import javax.servlet.*;
-
import javax.naming.*;
-
import javax.servlet.http.*;
-
import javax.rmi.PortableRemoteObject;
-
import javax.ejb.*;
-
public class LoanServlet extends HttpServlet
-
{
-
public void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
-
{
-
PrintWriter out = response.getWriter();
-
response.setContentType("text/html");
-
float rate = Float.valueOf(request.getParameter("rate")).floatValue();
-
float time = Float.valueOf(request.getParameter("time")).floatValue();
-
float amount = Float.valueOf(request.getParameter("amount")).floatValue();
-
Loan myLoanRemote = null;
-
LoanHome myLoanHome = null;
-
InitialContext initContext = null;
-
try
-
{
-
initContext = new InitialContext();
-
}atch (Exception e)
-
{
-
out.println("First " + e.toString());
-
}
-
try
-
{
-
String JNDIName = "ejb/SimpleLoan";
-
Object obj = initContext.lookup(JNDIName);
-
myLoanHome = (LoanHome)PortableRemoteObject.narrow(obj, LoanHome.class);
-
}catch(Exception e)
-
{
-
out.println("Second " + e.toString());
-
}
-
try
-
{
-
myLoanRemote = myLoanHome.create();
-
}catch(CreateException e)
-
{
-
out.println("Third " + e.toString());
-
}
-
float interest = myLoanRemote.calculateInterest(rate, time, amount);
-
out.println("<B>Interest : " + interest + " </B>");
-
}
-
}
Download: LoanServlet.java
|
|
Click Next To Continue ...
|
|