Calling EJBs from JSP


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Calling EJBs from JSP

  1. #1
    Mark Bower Guest

    Calling EJBs from JSP

    I just read in Ed Roman's (excellent) Mastering Enterprise Java Beans book
    that JSPs cannot call EJBs directly. In order to access EJBs they must
    call via a JavaBean or servlet proxy.

    Is this still the case for the JSP 1.1 spec? or was this a limitation of
    earlier JSP versions only?

    Thanks,
    Mark.




  2. #2
    Charles P. Cuozzo Guest

    Re: Calling EJBs from JSP

    Mark:

    Must have been a limitation of an earlier JSP spec.

    I have successfully called EJBs from scriptlets in a JSP. (1.1 JSP - using
    Weblogic 5.10) However, because of maintainability problems inherent in the
    mixing of script and HTML, we have opted to use custom tags instead. (These
    are Java classes, extending TagSupport and other classes, that allow us to
    incorporate our own tags within the JSP. So, HTML coders/designers are not
    confused by a lot of Java code mixed into the page.)

    If there is an interest, I can post some sample code to the newsgroup of
    both examples.

    Charlie Cuozzo


    "Mark Bower" <mark.bower@travelstore.com> wrote in message
    news:3961ff08@news.devx.com...
    > I just read in Ed Roman's (excellent) Mastering Enterprise Java Beans book
    > that JSPs cannot call EJBs directly. In order to access EJBs they must
    > call via a JavaBean or servlet proxy.
    >
    > Is this still the case for the JSP 1.1 spec? or was this a limitation of
    > earlier JSP versions only?
    >
    > Thanks,
    > Mark.
    >
    >
    >




  3. #3
    Mark Bower Guest

    Re: Calling EJBs from JSP

    Hi Charlie,

    We're just getting started here with WLS 5.1, developing some prototypes in
    preparation for a wholesale move away from Microsoft technologies.

    It's just the sort of maintainability issues you describe that we've been
    thinking about. We have considered giving the web designers access to
    EJB-Proxy objects limited to get methods only, and we also plan to
    experiment with returning XML result-sets from our EJB server and have the
    web designers write XSL to generate HTML on the server for delivery to the
    client.

    Your solution sounds like another interesting alternative though. I'd be
    interested in seeing your sample code.

    Cheers,
    Mark.

    Charles P. Cuozzo <ccuozzo@hotmail.com> wrote in message
    news:39648851$1@news.devx.com...
    > Mark:
    >
    > Must have been a limitation of an earlier JSP spec.
    >
    > I have successfully called EJBs from scriptlets in a JSP. (1.1 JSP - using
    > Weblogic 5.10) However, because of maintainability problems inherent in

    the
    > mixing of script and HTML, we have opted to use custom tags instead.

    (These
    > are Java classes, extending TagSupport and other classes, that allow us to
    > incorporate our own tags within the JSP. So, HTML coders/designers are not
    > confused by a lot of Java code mixed into the page.)
    >
    > If there is an interest, I can post some sample code to the newsgroup of
    > both examples.
    >
    > Charlie Cuozzo
    >
    >
    > "Mark Bower" <mark.bower@travelstore.com> wrote in message
    > news:3961ff08@news.devx.com...
    > > I just read in Ed Roman's (excellent) Mastering Enterprise Java Beans

    book
    > > that JSPs cannot call EJBs directly. In order to access EJBs they must
    > > call via a JavaBean or servlet proxy.
    > >
    > > Is this still the case for the JSP 1.1 spec? or was this a limitation

    of
    > > earlier JSP versions only?
    > >
    > > Thanks,
    > > Mark.
    > >
    > >
    > >

    >
    >




  4. #4
    Charles P. Cuozzo Guest

    Re: Calling EJBs from JSP

    Mark:

    Here was our first attempt...using Java scriptlets mixed with HTML. This
    addresses your original question regarding the use of an EJB from a JSP.
    (The bean, however, is a stateless session bean. I haven't tried to access
    an entity bean. It has methods that access the database via JDBC to return
    data to be displayed in a SELECT tag.)
    ===========================================================================
    <%@ LANGUAGE="Java" @%>
    <HTML>
    <HEAD>
    <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE>Document Title</TITLE>
    </HEAD>
    <BODY>
    <%@ page import=
    "javax.naming.*, javax.ejb.*, java.rmi.RemoteException,
    java.rmi.Remote, java.util.*"
    %>
    <%!
    String url = "t3://localhost:7001";
    String sOut = null;

    // Declaring a Java class
    public Context getInitialContext() throws Exception {
    Properties p = new Properties();
    p.put(Context.INITIAL_CONTEXT_FACTORY,
    "weblogic.jndi.WLInitialContextFactory");
    p.put(Context.PROVIDER_URL, url);
    return new InitialContext(p);
    }

    String getStackTraceAsString(Exception e)
    {
    // Dump the stack trace to a buffered stream, then send it's contents
    // to the JSPWriter.
    ByteArrayOutputStream ostr = new ByteArrayOutputStream();
    e.printStackTrace(new PrintWriter(ostr));
    return(ostr.toString());
    }
    %>
    <%
    try
    {
    // Get the bean...
    Context ctx = getInitialContext();
    CentralOfficeHome coHome = (CentralOfficeHome)
    ctx.lookup("CentralOffice");
    CentralOffice co = coHome.create();
    String[] sCities = co.listCities();
    %>

    <!-- Insert HTML here -->
    <h1>Hello from JSP!</h1>
    <br>
    <FORM NAME="CitiesForm" ENCODE="application/x-www-form-urlencoded">
    <SELECT NAME="Cities" SIZE=5>
    <%
    for(int i=0; i<sCities.length; i++)
    {
    %>
    <OPTION><%= sCities[i] %></OPTION>
    <%
    }
    }
    catch(Exception ex)
    {
    sOut = getStackTraceAsString(ex);
    }
    %>
    </SELECT>
    </FORM>
    <%= sOut %>
    </BODY>
    </HTML>
    ============================================================================
    ==

    Then, we decided to try custom tags. Below is the JSP that calls one of our
    custom tags....the tag is:

    <colo:selectcities
    selectname="lstCities"
    selectsize="7"
    region='<%= request.getParameter("region") %>'>
    </colo:selectcities>

    Notice that we implemented some familiar attributes such as "name" and
    "size" that would normally be seen in an HTML SELECT tag. Then we put in the
    "region" attribute. That is so we know what list of cities to retrieve from
    the database. We are using the request object to dynamically pass the region
    from a previous page.

    This custom tag basically gets data from the database and formats it as a
    SELECT-OPTION HTML tag. Each OPTION tag within the SELECT is a city.
    ============================================================================
    ===
    <!--
    <%@ LANGUAGE="Java" %>
    <%@ taglib uri="colo-taglib.tld" prefix="colo" %>
    <%@ page errorPage="coloerrors.jsp" %>
    -->
    <HTML>
    <HEAD>
    <META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
    <TITLE>Cities with Custom Tag</TITLE>
    </HEAD>
    <BODY text="Navy">
    <h2>Testing Custom Tags</h2>
    <br>
    <FORM NAME="CitiesForm" method="get" action="AddressTest.jsp">
    <colo:selectcities
    selectname="lstCities"
    selectsize="7"
    region='<%= request.getParameter("region") %>'>
    </colo:selectcities> <br>
    <br>
    <input type="submit" name="cmdTest" value="Testing...">
    </form>
    <br><br>
    <h3>Just to demonstrate that we can get parameters from a page...</h3>
    <%= request.getParameter("cityname") %>
    </BODY>
    </HTML>
    ============================================================================
    ==
    Finally, here are the two Java classes that implement our custom tag. We
    have a parent class, SelectTag that implements the selectname and selectsize
    attributes. It also has two methods for retrieving the EJB.
    ============================================================================
    ==
    The parent class. Notice that it extends TagSupport.
    ============================================================================
    ==
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    import java.io.*;
    import javax.naming.*;
    import javax.ejb.*;
    import java.rmi.*;
    import java.rmi.server.*;
    import java.util.Properties;

    public class SelectTag extends TagSupport
    {
    private final String sURL = "t3://localhost/7001";
    protected String sName = null;
    protected int iSize;

    public void setSelectName(String sName)
    {
    this.sName = sName;
    }

    public String getSelectName()
    {
    return this.sName;
    }

    public void setSelectSize(String sSize)
    {
    try
    {
    this.iSize = Integer.parseInt(sSize);
    }
    catch(NumberFormatException e)
    {
    this.iSize = 5;
    }
    }

    protected CentralOffice getBean()
    {
    CentralOffice co = null;

    try
    {
    // Get the bean...
    Context ctx = getInitialContext();
    CentralOfficeHome coHome = (CentralOfficeHome)
    ctx.lookup("CentralOffice");
    co = coHome.create();
    }
    catch(Exception ex)
    {
    ex.printStackTrace();
    }
    finally
    {
    return co;
    }
    }

    private Context getInitialContext() throws NamingException
    {
    try
    {
    Properties props = new Properties();
    props.put(Context.INITIAL_CONTEXT_FACTORY,
    "weblogic.jndi.WLInitialContextFactory");
    props.put(Context.PROVIDER_URL, sURL);
    return new InitialContext(props);
    }
    catch(NamingException ne)
    {
    System.out.println("Unable to connect to WebLogic server");
    throw ne;
    }
    }
    }
    ========================================================================
    Then the actual SelectCities implementation. Where the HTML is produced...
    ========================================================================
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    import java.io.*;
    import javax.naming.*;
    import javax.ejb.*;
    import java.rmi.*;
    import java.rmi.server.*;

    /**
    * The class that implements the custom tag <colo:selectcities/> in
    * JSP pages for the CO-Location project.
    *
    * @author Charles P. Cuozzo
    */
    public class SelectCitiesTag extends SelectTag
    {
    private int iRegion;

    public void setRegion(String sRegion)
    {
    try
    {
    this.iRegion = Integer.parseInt(sRegion);
    }
    catch(NumberFormatException e)
    {
    this.iRegion = 34;
    }
    }

    public int doStartTag() throws JspException
    {
    String[] sCities = null;

    try
    {
    // get the bean from superclass...
    CentralOffice co = getBean();
    sCities = co.listCities(this.iRegion);

    JspWriter out = pageContext.getOut();

    System.out.println("<SELECT name=\"" + sName + "\" SIZE=" + iSize +
    ">");
    System.out.println("Region=" + this.iRegion);

    out.println("<SELECT name=\"" + sName + "\" SIZE=" + iSize + ">");

    for(int i=0; i<sCities.length; i++)
    {
    out.println("<OPTION value=\"" + sCities[i] + "\">" + sCities[i] +
    "</OPTION>");
    }

    out.println("</SELECT>");
    out.flush();
    }
    catch(IOException ie)
    {
    throw new JspException("I/O exception " + ie.getMessage());
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    return(SKIP_BODY);
    }

    public void release()
    {
    super.release();
    this.sName = "Cities";
    this.iRegion = 34;
    this.iSize = 5;
    }
    }
    ======================================================================
    I've taken out some stuff, like package name dependencies that are specific
    to the client I'm working for. Also, anything that identifies the client or
    is proprietary.
    ======================================================================

    I realize that this is quite a long post due to the code listings. Sorry.

    Charlie C.


    "Mark Bower" <mark.bower@travelstore.com> wrote in message
    news:3964b4d7@news.devx.com...
    > Hi Charlie,
    >
    > We're just getting started here with WLS 5.1, developing some prototypes

    in
    > preparation for a wholesale move away from Microsoft technologies.
    >
    > It's just the sort of maintainability issues you describe that we've been
    > thinking about. We have considered giving the web designers access to
    > EJB-Proxy objects limited to get methods only, and we also plan to
    > experiment with returning XML result-sets from our EJB server and have the
    > web designers write XSL to generate HTML on the server for delivery to the
    > client.
    >
    > Your solution sounds like another interesting alternative though. I'd be
    > interested in seeing your sample code.
    >
    > Cheers,
    > Mark.
    >
    > Charles P. Cuozzo <ccuozzo@hotmail.com> wrote in message
    > news:39648851$1@news.devx.com...
    > > Mark:
    > >
    > > Must have been a limitation of an earlier JSP spec.
    > >
    > > I have successfully called EJBs from scriptlets in a JSP. (1.1 JSP -

    using
    > > Weblogic 5.10) However, because of maintainability problems inherent in

    > the
    > > mixing of script and HTML, we have opted to use custom tags instead.

    > (These
    > > are Java classes, extending TagSupport and other classes, that allow us

    to
    > > incorporate our own tags within the JSP. So, HTML coders/designers are

    not
    > > confused by a lot of Java code mixed into the page.)
    > >
    > > If there is an interest, I can post some sample code to the newsgroup of
    > > both examples.
    > >
    > > Charlie Cuozzo
    > >
    > >
    > > "Mark Bower" <mark.bower@travelstore.com> wrote in message
    > > news:3961ff08@news.devx.com...
    > > > I just read in Ed Roman's (excellent) Mastering Enterprise Java Beans

    > book
    > > > that JSPs cannot call EJBs directly. In order to access EJBs they

    must
    > > > call via a JavaBean or servlet proxy.
    > > >
    > > > Is this still the case for the JSP 1.1 spec? or was this a limitation

    > of
    > > > earlier JSP versions only?
    > > >
    > > > Thanks,
    > > > Mark.
    > > >
    > > >
    > > >

    > >
    > >

    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center