Posting Form Data to a JSP page

Discussions

EJB design: Posting Form Data to a JSP page

  1. Posting Form Data to a JSP page (4 messages)

    Posting Form Data to a JSP page

    I have an HTML form with 2 text boxes. This form is submitted to a jsp page. n that jsp page i am invoking an EJB component which takes 2 parameters.

    I am using request.getparameter() to retrieve the form values on the JSP page and storing them in variables.

    How should I pass these form values that are stored in variables as parameters to my EJB on the JSP page.??
  2. from the jsp where you are calling ejb method ,pass there
    string (request.getparameter) as passing parameters,I mean to say arguments for a method.

    If I am properly understanding your question then it is correct,otherwise plz revert back
  3. what madhavi gave shud work.... but its not really good design practice to invoke an ejb from a jsp.
    A good pattern to follow in ejb lookups is Service Locator Pattern. (A simple google search will throw up lots of egs.)
  4. Do something like this (modify names as appropriate):

    <%@ page import="javax.naming.*,your.ejb.package.*" %>

    <%
    String field1 = request.getParameter("field1");
    int field2 = Integer.parseInt(request.getParameter("field2"));

    InitialContext ctx = new InitialContext();
    Object ejbHomeRef = ctx.lookup("Bank/AccountEJB");
    YourEJBHome ejbHome = (YourEJBHome)
      javax.rmi.PortableRemoteObject.narrow(ejbHomeRef,
        YourEJBHome.class);

    YourEJBRemote ejb = ejbHome.create();
    ejb.yourEJBMethod(field1, field2);
    %>



    The above assumes:

    1. Your EJB is a session bean. If it is an entity bean, modify as appropriate.

    2. The first EJB method parameter is a String and the second is an int. If your EJB methods are other types, modify as appropriate.

    Finally, in the long run, this kind of logic is inappropriate for JSP. At the very least, you ought to split the EJB lookup and execution logic into an simple Java class. Look at patterns like Model-View-Controller and Business Delegate to get ideas for how to organize your components.
  5. Here is a simple code to start with (later followed by a design pattern):
    Code snippet uses the following:
    1. Lets assume your EJB Name is com.example.TaxEJB (which is a Session Bean).
    2. Lets assume you are invoking the doSomething(String param1, String param2) method on the TaxEJB.
    3. Lets assume your EJBs are deployed on an Application Server (Jboss in this case) running on the same machine (localhost).

    Code Snippet:
    //import the api.
    import javax.ejb.*;
    import javax.naming.*;
    import javax.rmi.PortableRemoteObject;
    import com.example.TaxHome;
    import com.example.Tax;

    //method variable declaration.
    Context contextObj = null;
    Object objRef = null;
    Tax taxCompObj = null;
    TaxHome taxHomeObj = null;
    Properties envObj = null;

    try{
    //prepare the environment settings in the properties.
    envObj = new Properties();
    envObj.setProperty("java.naming.factory.initial",
                            "org.jnp.interfaces.NamingContextFactory");
    envObj.setProperty("java.naming.provider.url", "localhost:1099");
    envObj.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
    contextObj = new InitialContext(envObj);
    //lookup the EJB by JNDI name.
    objRef = contextObj.lookup("TaxEJB");
    //type cast the EJB.
    taxHomeObj = (TaxHome)PortableRemoteObject.narrow(objRef, TaxHome.class);
    taxCompObj = taxHomeObj.create();
    //invoke the method on the EJB.
    taxCompObj.doSomething(param1, param2);
    }
    catch(Exception e){
       //handle exceptions here.
    }
    finally{
       //
    }

    NOTE:
    1. The line envObj.setProperty("java.naming.provider.url", "localhost:1099"); may be changed depending on which port your application server listens for the remote calls (1099 in this case).
    2. The line envObj.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming"); is specific to JBOSS, you may change it to some other class.


    Once you have this EJB invocation up and running, please make sure you remove the codes from JSP, try to use a design pattern.
    Front Controller + Business Delegate + Service Locator would be advisable here.
    The JSP (ideally should be without any scriptlets/tag libs) can call the Controller(Servlet), which delegates the EJB invocatin to the Business Delegate (core java class). The Business Delegate (can encapsulate the transactionality aspects) and uses the Service Locator class (core java class) to look up the EJB . The usage of the design pattern would abstract functionality at various levels, enchancing reuse and scalability.


    Hope this helps,
    Kind Regards,
    Shafique Razzaque,
    Singapore.