Chapter 24 JavaServer Pages


Application logic in JSPs

The application logic in JSPs can be provided by components such as servlets, JavaBeans, and EJBs, customized tag libraries, scriptlets and expressions. Scriptlets and expressions hold the components and tags together in the page.

JavaBeans

You can easily use JavaBeans components in a JSP page with the useBean directive. For more information, see "<jsp:useBean>".

Enterprise JavaBeans

To use an EJB component, write a scriptlet that uses JNDI to establish an initial naming context for the EJB's home interface. For more information about establishing the naming context and calling remote methods on the EJB's home interface, see "Creating Enterprise JavaBeans clients" in the Jaguar CTS Programmer's Guide. This example, HotSpots.jsp, uses an EJB called HotSpots to return a list of places to go that fit a category and date requirement passed in the HTTP request:

<HTML>
<HEAD></HEAD><BODY>
<%@ page language="java" import="hotspots.*"
    session="true" errorPage="ErrorPage.jsp" %>
<%@ include file="header.htm" %>
<h1>HotSpots</h1>
<%-- GET SEARCH PARAMETERS FROM REQUEST OBJECT --%>
<%
    String category =
        request.getParameter("category");
    String date = request.getParameter("date");
%>
<%-- CREATE FORM WITH SEARCH PARAMETERS --%>
<form action="HotSpots.jsp">
    <table border=0>
    <tr><td>Category:</td>    <td>
    <input name="category"     value="<%= category %>">
    </td></tr>
    <tr><td>Date:</td>    <td><input name="date"
        value="<%= date %>"></td>
    </tr>
    </table>
    <br><input type="submit" value="Search">
</form>
<%-- INSERT TABLE TO SHOW RESULTS AND USE SCRIPTLET TO GET A REFERENCE TO THE HOTSPOTS HOME INTERFACE AND GET A RESULT SET--%>
<p><table border=1 cellpadding=4>
<tr><th>Book</th><th>Place</th><th>Date</th>
    <th>Price</th></tr>
<%
if ( category !=null &&  date!=null) {
 try {
     java.util.Properties 
        p = new java.util.Properties();
    p.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
        "com.sybase.ejb.InitialContextFactory");
    p.put(javax.naming.Context.PROVIDER_URL,
        "iiop://localhost:9000");
    p.put(javax.naming.Context.SECURITY_PRINCIPAL,
        "jagadmin");            
    p.put(javax.naming.Context.SECURITY_CREDENTIALS,
         "");
    javax.naming.InitialContext ctx = 
        new javax.naming.InitialContext(p);
    HotSpotsHome home = (HotSpotsHome)
        ctx.lookup("HotSpots");
    HotSpots hotSpots = home.create();
    java.sql.ResultSet rs =
        com.sybase.helper.IDL.getResultSet(
            hotSpots.getList(category, date) );
    while (rs.next()) {
%>
<%-- POPULATE TABLE WITH RESULT SET --%>
<tr><td><a href=Payment.jsp?trip=
    <%= rs.getInt("trip_id") %>
    &amount=<%= rs.getDouble("price") %> >
    <img src="images/bookit.gif"></a></td>
<td><%= rs.getString("place") %></td>
<td><%= rs.getDate("date") %></td>
<td><%= rs.getDouble("price") %></td>
</tr>

<%-- CLOSE WHILE LOOP AND TRY CATCH BLOCK --%>
<%
    }
 } catch (Exception e) {
        out.println(e);
 }
}%>
</table>
</BODY></HTML>

Customized tag libraries

Customized tag libraries, also called tag extensions, extend the capabilities of JSPs. Tag libraries define a set of actions to be used within a JSP page for a specific purpose, such as handling SQL requests.

JSP page authors can use tag libraries whether they are editing a page manually or using an authoring tool. To associate a tag library with the page, the page author uses a taglib directive that identifies the tag library's URI (see "Taglib directive"). The URI identifying the tag library is associated with a Tag Library Descriptor (TLD) file and with tag handler classes. Tag libraries are usually packaged as JAR files with a tag library descriptor file named META-INF/taglib.tld.

A tag handler is a Java class that defines the semantics of an action. The implementation class for the JSP page instantiates a tag handler object for each action in the page. Tag handler objects implement the javax.servlet.jsp.tagext.Tag interface which defines basic methods required by all tag handlers, including doStartTag and doEndTag. The BodyTag interface extends the Tag interface by adding methods that enable the handler to manipulate its body.

 


Copyright © 2000 Sybase, Inc. All rights reserved.