Struts EJB

download Struts EJB

of 22

Transcript of Struts EJB

  • 8/6/2019 Struts EJB

    1/22

    The EJB specification provides Enterprise-level services. Typical services

    provided by the EJB Container are as follows.

    1.Persistence

    2.Declarative transactions

    3.Security

    4.Connection pooling

    5.Support for messaging services

    The most famous EJB-Servers in the market are:

    y BEA ..WebLogic Servery JBoss Application servery IBM.WebSphere-5y Oracle AS

    Of these , JBoss 3.2 & 4 are OpenSource and Free However, BEA has the

    longest record and used by many enterprises and hence a standard..Therefore, I am

    now writing about WebLogic-7 and later, I will cover JBoss3.2 in a separate

    lesson.( The latest version of Weblogic is WL-9).

    Different kinds of Enterprise Beans are

    1. Session Bean

    2. Entity Bean

    3. MessageDriven Bean (MDB)

    Session Bean

    Session Bean can be defined as function bean called in RMI-IIOP style from

    Enterprise container. Two types of session bean are stateless session

    bean, stateful session bean.

  • 8/6/2019 Struts EJB

    2/22

    A stateless session bean does not maintain conversation state but stateful session

    bean maintains conversation state

    Entity Bean

    Entity Bean is a value bean with setter and getter methods representing one row

    of data in a table, invoked in RMI style from Enterprise container in Enterprise

    data store. Two types of Entity Beans areCMP (Container Managed

    Persistence), BMP (Bean Managed Persistence). In CMP, persistence code need

    not be written by the programmer. But In BMP Persistence code has to be written

    by the programmer .CMP is not satisfactory for really complex enterprise

    situations. ( CMP is an ORM technology ( Object-Relational Mapping Technology

    like Hibernate. and Hibernate is considered to be better than CMP).

    Message Driven Bean

    Session Bean and Entity bean are based on RPC mechanism. But,

    Message Driven Bean is based on Asynchronous messaging . MDB have no

    conversational state with the client and can be pooled like stateless session bean.

    Unlike other types of EJB, MDB have no home or remote interfaces.

    Messaging is like email whereas RPC is like phone call. In enterprise environmentthere will be heavy traffic and RPC may fail to create connection. In such

    situations messaging is more reliable.

    What is Struts?

    Struts is a very famous ready made framework which implements MVC (Model

    View Controller) Architecture. It is open source and free. Struts frame work was

    developed by Craig MacLanahan in 2002 subsequently handed over to Apache

    Jakarta projects group. Struts is a frame work for building really complexEnterprise level applications

    Struts provide its own Controller component and integrates with other technologies

    to provide the Model and the View. For the Model, Struts can interact with

    standard data access technologies, like JDBC and EJB, as well as most any third-

    party packages, like Hibernate, iBATIS, orObject Relational Bridge. For the

  • 8/6/2019 Struts EJB

    3/22

    View, Struts works well with JavaServer Pages, including JSTL and JSF, as well

    as Velocity Templates, XSLT, and other presentation systems

    Purpose of the Struts

    When the user fills the form and submits the form, if the values entered by the user

    are wrong then the user must be presented with the same page with suitable error

    messages and the values already entered by the user should be preserved.

    If there is no error, ( validation test is passed), then the business logic is executed..

    Installation

    Struts1.1 is zip file. Extract it. In Struts1.1 folder, there is a folder name called lib.

    There are many war files. One of the war file names, is struts-blank.war. Copy

    that struts-blank.war file to c:\tomcat5\webapps. Start the tomcat server. Now

    struts-blank folder is created .

    Copy that struts-blank folder and paste it in desktop. Rename the folder as kala..

    Now, place that folder(kala) in c:\tomcat5\webapps.( Assuming that we have

    installed tomcat5 in C drive). Here kala folder is ourcontext. Restart the tomcat

    server.. Open the Browser, type the URL as http://localhost:8080/kala. Now the

    welcome page will appear. This means that our installation process is correct.

    We require weblogic7 and JDK1.4.2 for developing this exercise

    /---------------------------------------------------------------------------------------------------

    --------

    The following lesson illustrates the method of connecting to a stateless session

    bean in weblogic7 server through a Struts-based program.

    We have installed weblogic7 and JDK1.4.2 in D:\drive . Create a folder

    strutsejbdemo in D drive. This is our working directory.

    We have already created the context kala in tomcat5 as mentioned above. .

  • 8/6/2019 Struts EJB

    4/22

    Copy that kala context folder to D:\bea\user_projects\mydomain\applications

    In this lesson, we have created two packages namely demo and demo1. For this we

    have created the two folders namely demo and demo1 in

    :D\bea\user_projects\mydomain\applications\kala\web-inf\classes\ to copy theclass files

    D:\>cd strutsejbdemo

    D:\strutsejbdemo>

    When we use struts the concept of package is very important.

    D:\strutsejbdemo>md demo1

    Here demo1 is our package

    D:\strutsejbdemo>cd demo1

    Next we create a batch file to set path and classpath

    D:\strutsejbdemo\demo1>edit setpath.bat

    set path=c:\windows\system32;d:\jdk1.4.2\bin;d:\bea\weblogic700\server\bin

    ( I am using WindowsXP. If you are using Windows-2000, the path to edit

    program should be :c:\winnt\system32.)

    Usually we give a class path to working directory. When we use package we

    giveclasspath to parent directory. This is very important.

    D:\strutsejbdemo\demo1\edit setcpath.bat

    set classpath=d:\strutsejbdemo\demo1;d:\strutsejbdemo;

    d:\bea\weblogic700\server\lib\weblogic.jar

    After creating batch files in the working folder,

  • 8/6/2019 Struts EJB

    5/22

    Give the following command:

    d:\strutsejbdemo\demo1>setpath

    This will set the path for the window

    d:\strutsejbdemo\demo1>setcpath

    ( This will set the classpath for the window ).

    Checkup as follows:

    D:\strutsejbdemo\demo1>path

    D:\strutsejbdemo\demo1>echo %classpath%

    Now edit the following files

    1.cityguide.java

    2.ciyRemote.java

    3.cityHome.java

    4.cityBean.java

    D:\strutsejbdemo\demo1>edit cityguide.java

    package demo1;

    import java.util.*;

    public class cityguide

    {

    public String listcities(String a)

    {

  • 8/6/2019 Struts EJB

    6/22

    Vector vector1=new Vector();

    if(a.equals("TAMILNADU"))

    {

    vector1.addElement("Madras");

    vector1.addElement("Trichy");

    vector1.addElement("Madurai");

    }

    if(a.equals("KERALA"))

    {

    vector1.addElement("Cochin");

    vector1.addElement("Quicon");

    vector1.addElement("Trivandrum");

    }

    if(a.equals("KARNATAKA"))

    {

    vector1.addElement("Mysore");

    vector1.addElement("Bangalore");

    vector1.addElement("Hubli");

    }

    String r="";

  • 8/6/2019 Struts EJB

    7/22

    int n=vector1.size();

    for(int j=0;jedit cityRemote.java

    package demo1;

    import javax.ejb.*;

    import java.rmi.*;

    public interface cityRemote extends EJBObject

    {

    public String showcities(String s)

    throws RemoteException;

    }

  • 8/6/2019 Struts EJB

    8/22

    //--------------------------------------------------------------------------------------------------

    --------

    D:/strutsejbdemo/demo1>edit cityHome.java

    package demo1;

    import javax.ejb.*;

    import java.rmi.*;

    public interface cityHome extends EJBHome

    {

    public cityRemote create()

    throws CreateException, RemoteException;

    }

    //---------------------------------------------------------------------------------

    D:/strutsejbdemo/demo1>edit cityBean.java

    package demo1;

    import javax.ejb.*;

    public class cityBean implements SessionBean

    {

    public String showcities(String s)

    {

    cityguide guide1= new cityguide();

    String r = guide1.listcities(s);

  • 8/6/2019 Struts EJB

    9/22

    return r;

    }

    public void ejbCreate() {}

    public void ejbRemove() {}

    public void ejbActivate() {}

    public void ejbPassivate() {}

    public void setSessionContext(SessionContext sc) {}

    }

    //--------------------------------------------------------------------------------------------------

    --------

    We have already set the specified path and classpath. Compile all the above files

    D:\strutsejbdemo\demo1>javac *.java

    D:\ejbstrutsdemo\demo1>copy *.class

    d:\bea\user_projects\mydomain\applications\kala\web-inf\classes\demo1

    D:\strutsejbdemo>demo1\cd..

    D:\strutsejbdemo>md META-INF

    When we use package META-INF folder is created in parent folder. It should be

    in uppercase ie.,META-INF not meta-inf

    Now edit the ejb-jar.xml and weblogic-jar.xml in META-INF

    D:\strutsejbdemo\META-INF>edit ejb-jar.xml

  • 8/6/2019 Struts EJB

    10/22

    city

    demo1.cityHome

    demo1.cityRemote

    demo1.cityBean

    Stateless

    Container

    //--------------------------------------------------------------------------------------------------

    D:\strutsejbdemo\META-INF >edit weblogic-jar.xml

  • 8/6/2019 Struts EJB

    11/22

    "http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd">

    city

    cityJndi

    //----------------------------------------------------------------------------------

    Now, cd back to D:\strutsejbdemo\

    Create the jar file as follows:

    D:\strutsejbdemo\>jar cf city.jar demo1\*.class META-INF\*.xml

    Carefully note that the jar file is created in parent directory.

    Now we check up the jar file as follows

    D:\strutsejbdemo>java weblogic.ejbc city.jar city1.jar

    If there are no problems , we get the ejbc successful message

    Now, we copy the jar file to the WebLogic folder as shown below.

    D:\strutsejbdemo>copy city1.jar d:\bea\user_projects\mydomain\applications

    Start the weblogic server as start->programfiles->BEA weblogic->user_projects-

    >mydomain->start server

    The city1.jar will be deployed by the server. The development of the EJB and its

    deployment in WebLogic server is over.

  • 8/6/2019 Struts EJB

    12/22

    //--------------------------------------------------------------------------------------------------

    --------Now we develop the Struts client program for the above EJB.

    D:\ejbstrutsdemo>md demo

    D:\ejbstrutsdemo>demo

    D:\strutsejbdemo\demo>edit setcpath.bat

    set classpath=%classpath%;d:\strutsejbdemo;e:\struts1.1\lib\struts.jar;

    D:\strutsejbdemo\demo>setcpath

    We use the following six files, in this demo package,

    1. kalaSubmit.jsp

    2. kalaForm.java(derived from ActionForm)

    3. kalaAction.java(derived from Action)

    4. kalahelper.java ( a utility bean)

    5. kalaresult.java ( a value bean)

    6. kalaOutput.jsp

    ( besides the web.xml ,struts-config.xml files & application.properties file)

    D:\ejbstrutsdemo\demo1>edit kalaSubmit.jsp

    STRUTS CLIENT FOR STATELESS

  • 8/6/2019 Struts EJB

    13/22

    State

    Whick State Do you want to tour?

    TAMILNADU?KERALA?KANADA?


    //--------------------------------------------------------------------------------------------------

    --------

    When the user submits the kalaSubmit.jsp, the formbean is automatically filled up

    with the values from the jsp-page and the flow goes to the ActionServlet.

    tag is used to displaying error messages.

    D:\strutsejbdemo\demo>edit kalaForm.java

    package demo;

  • 8/6/2019 Struts EJB

    14/22

    import javax.servlet.http.*;

    import org.apache.struts.action.*;

    public class kalaForm extends ActionForm

    {

    String state =null;

    public String getState()

    {

    return state;

    }

    public void setState(String a)

    {

    state=a;

    }

    public void reset(ActionMapping mapping,

    HttpServletRequest request)

    {

    state=null;

    }

    public ActionErrors validate (ActionMapping mapping, HttpServletRequest

    request)

    {

  • 8/6/2019 Struts EJB

    15/22

    ActionErrors errors = new ActionErrors();

    if( state.length()= =0)

    {

    errors.add("state", new ActionError("errors.nullstate"));

    }

    return errors;

    }

    }

    //--------------------------------------------------------------------------------------------------

    --------

    D:\strutsejbdemo\demo>edit kalahelper.java

    package demo;

    import java.util.*;

    import javax.ejb.*;

    import javax.rmi.*;

    import javax.naming.*;

    import java.rmi.*;

    import java.io.*;

    public class kalahelper

    {

    public String callejb(String s)

  • 8/6/2019 Struts EJB

    16/22

    {

    String r;

    try

    {

    System.out.println("Please Wait...");

    Properties props=new Properties();

    props.put(Context.INITIAL_CONTEXT_FACTORY,

    "weblogic.jndi.WLInitialContextFactory");

    String url="t3://127.0.0.1:7001";

    props.put(Context.PROVIDER_URL,url);

    Context context=new InitialContext(props);

    System.out.println("ConnectionOK");

    demo1.cityHome home=

    (demo1.cityHome)context.lookup("cityJndi");

    System.out.println("HOME LOCATED");

    demo1.cityRemote remote= home.create();

    System.out.println("REMOTE READY");

    r=remote.showcities(s);

    }

    catch(Exception e1){ r=""+e1; }

  • 8/6/2019 Struts EJB

    17/22

    return r;

    }

    }

    //-------------------------------------------------------------------------------------------------

    D:\strutsejbdemo\demo>edit kalaresult.java

    package demo;

    public class kalaresult

    {

    String value;

    public kalaresult()

    {

    value=" ";

    }

    public String getValue(){

    return value;

    }

    public void setValue(String v){

    value=v;

    }

  • 8/6/2019 Struts EJB

    18/22

    }

    //--------------------------------------------------------------------------------------------------

    --

    The Action Class is part of the Model and is a wrapper around the business logic.

    The purpose of Action Class is to translate the HttpServletRequest to the business

    logic

    In our case, the action class instance is using a helper to perform the business logic.

    This is the bettter and recommended practice.

    D:\strutsejbdemo\demo>edit kalaAction.java

    package demo;

    import java.io.*;

    import java.util.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    import org.apache.struts.action.*;

    public class kalaAction extends Action

    {

    public ActionForward execute(ActionMapping mapping,

    ActionForm form,

    HttpServletRequest request,

    HttpServletResponse response)

    throws IOException, ServletException

  • 8/6/2019 Struts EJB

    19/22

    {

    kalaForm kalaform =(kalaForm) form;

    String a = kalaform.getState();

    if(a.equals("TAMILNADU") || a.equals("KERALA") ||

    a.equals("KARNATAKA"))

    {

    kalahelper helper = new kalahelper();

    String r = helper.callejb(a);

    kalaresult kr = new kalaresult();

    kr.setValue(r);

    String m = kr.getValue();

    System.out.println(m);

    HttpSession session= request.getSession();

    session.setAttribute("result",kr);

    return (mapping.findForward("success"));

    }

    else

    {

    ActionErrors errors = new ActionErrors();

    errors.add (ActionErrors.GLOBAL_ERROR, new

    ActionError("errors.wrongname"));

  • 8/6/2019 Struts EJB

    20/22

    saveErrors(request,errors);

    return(mapping.findForward("failure"));

    }

    }

    }

    D:\strutsejbdemo\demo>edit kalaOutput.jsp


    Now we are ready to compile the above programs.class path are already specified

    D:\strutsejbdemo\demo>javac *.java

    D:\strutsejbdemo\demo>copy *.jsp

    d:\bea\user_projects\mydomain\applications\kala

  • 8/6/2019 Struts EJB

    21/22

    D:\strutsejbdemo\demo>copy *.class

    d:\bea\user_projects\mydomain\applications\kala\web-inf\classes\demo

    The deployment process is over

    Go to another window. cd to

    D:\bea\user_projects\mydomain\applications\kala\web-inf>edit struts-config.xml

    In the struts-config.xml file, we make two entries. One entry is for the instance

    ofkalaForm and the other entry is for the instance ofkalaAction class.

    ------------------------------------------

    ( this is the part dealing with the formbean)

    The next segment of mapping in

    struts-config.xml deals with the action mapping for QueryAction.

  • 8/6/2019 Struts EJB

    22/22

    . There is a file known as 'Application.properties' in 'WEB-INF\classes\resources'

    and at the end of that file we will add the following lines.

    errors.nullstate= ENTER YOUR STATE

    errors.wrongname=SORRY.., SERVICE NOT PROVIDED FOR

    THIS STATE

    Therefore, we will get the specified error messages.

    Start the weblogic server as usual.

    Open the browser and type URL as http://localhost:7001/kala/kalaSubmit.jsp

    Type the name of the state ( TAMILNADU/KERALA/KARNATAKA)in the

    text box & then click submit. If the name of the state is correct then result will be

    displayed. Otherwise the home page remains with the values entered and also with

    the error message. As the main purpose of this note is just to explain how to

    connect to stateless ejb using Struts, I am leaving out Validation Framework

    details.