How to Compile Home Remote Interface In EJB Set Class Path for jdk1.4

Perform the arithmetic operations by using EJB.
Step by Step by Procedure
Before going to compile and deploy your java file you need to set path and class path for corresponding jdk1.4 using the following command
>>set path=x:\bin;%path%;
>>java -version
// The above command is used to display the jdk version.
>>set classpath=y:\sun\appserver\lib\j2ee.jar;
Start the j2ee deploy tool using the j2ee1 command

 • Compile the home interface, remote interface, bean and servlet files using the following command.
•    Add the server into to your machine
•    Give the username and password.
•    username-admin
•    password-123456789
EJB module add server
EJB connect to server
•    The window will be opened.


•    Select anew application.


•    Click the browse button in the New application dialog box to display another New Application. Select a directory where you want to save the application (.ear file) from the LOOIN combobox.Enter the name of the ear file.
•    Select enterprise application



•    This wizard will help you to create a new Enterprise Bean module. You must begin with implementation and interface classes for the bean.
The wizard will then package the selected files into an EJB JAR module and will create the deployment descriptor required.
•    Click next button, then select new jar module in application radio button from the jar location
•    Click the edit button, the select the home interface class ,remote interface class and bead class files and click add button. The following dialog box will be displayed.
•    Click next button
•    Choose the enterprise bean class and indicate the bean type (stateless session), you must also choose the remote and home interface classes.
•    Click next button
•    Click next and finish button.
•    The following window will display
•    Select new web component
•    Click edit button, then select html file and servlet class file
•    Click ok
•    Click next button
•    Please choose the servlet class and provide a name for it.
Optionally, you can define the relative position in which this component will be loaded when the web application is started
•    click next and finish button
•    Select the ArithServlet in web application, then select the Aliases Tab ,Click the add button ,give the Aliases Name as arithservlet.
•    Select the webApp .give the context root as  EJBN(User defined)
•    Select the Application EJBN in left pane and select the JNDI name tab in the right pane .give the JDNI Name as ejb/EJBN (ejb/Folder name) in the application pane.
•    Select deploy from tool menu
•    For complete successful deployment the following window will be displayed .
•     Finally Output was displayed in the window
Source code Programming
Home:
import javax.ejb.*;
import java.rmi.*;
public interface ArithHome extends EJBHome
{
     public ArithRemote create()throws RemoteException,CreateException;
}

Remote:
import javax.ejb.EJBObject;
public interface ArithRemote extends EJBObject
{
    public int add(int a,int b)throws java.rmi.RemoteException;
    public int sub(int a,int b)throws java.rmi.RemoteException;
    public int mul(int a,int b)throws java.rmi.RemoteException;
    public int div(int a,int b)throws java.rmi.RemoteException;
}

Bean:
import javax.ejb.*;
public class ArithBean implements SessionBean
{
    SessionContext ctx;
    public void setSessionContext(SessionContext ctx)
    {
        this.ctx=ctx;
    }
    public void ejbCreate()throws java.rmi.RemoteException,javax.ejb.CreateException
    {}
    public void ejbRemove()
    {}
    public void ejbActivate()
    {}
    public void ejbPassivate()
    {}
    public int add(int a,int b)throws java.rmi.RemoteException
    {
        return(a+b);
    }
    public int sub(int a,int b)throws java.rmi.RemoteException
    {
        return(a-b);
    }
    public int mul(int a,int b)throws java.rmi.RemoteException
    {
        return(a*b);
    }
    public int div(int a,int b)throws java.rmi.RemoteException
    {
        return(a/b);
    }
   
}

Servlet:

import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.io.*;
import javax.ejb.*;
public class ArithServlet extends HttpServlet
{
     int a,b,c;
    String opr;
    ArithRemote remote=null;
    ArithHome home=null;
    InitialContext initctx=null;
    PrintWriter out;
   
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
        out=res.getWriter();
        res.setContentType("text/html");
        a=Integer.parseInt(req.getParameter("a"));   
        b=Integer.parseInt(req.getParameter("b"));   
        opr=req.getParameter("r1");
        try
        {
            initctx=new InitialContext();
           

Object obj=initctx.lookup("ejb/Arithmetic");
                         home=(ArithHome)PortableRemoteObject.narrow(obj,ArithHome.class);
            remote=home.create();
            if(opr.equals("add"))
                c=remote.add(a,b);
            else if(opr.equals("sub"))
                c=remote.sub(a,b);
            else if(opr.equals("mul"))
                c=remote.mul(a,b);
            else if(opr.equals("div"))
                c=remote.div(a,b);
            out.println("


The result after "+opr+" is "+c+"


");
            out.close();
        }
        catch(CreateException ce){}
        catch(Exception e){}
           
    }
}

Related post