Session-based connection (one database connection
per session).
Code from MainPage.jsp
...
<%@ page import="HotelBean" %>
...
<jml:when condition = "<%= l_reqType.equals(\"HOTELS\") %>" > <%-- If the value of the parameter REQ_TYPE is "HOTELS" then, return the source for the right frame of the main page. Right frame contains the list of all Hotels available. For getting the hotels list the method getHotels() in the HotelBean class is called. --%> <BODY bgcolor="#bfbfbb"> <SCRIPT LANGUAGE="JavaScript1.1"> function putVal(val1) { document.forms[0].HOTEL_ID.value =val1; } </SCRIPT> <CENTER> <FORM> <%= hrsBean.getHotels() %> <BR> <input type=hidden name=HOTEL_ID value=""> </FORM> </CENTER> </BODY> </jml:when>
...
Simulate a hotel reservation system using JSPs and EJBs accessed
via Custom Tag Libraries.
Remarks
This sample illustrates EJB features including:
Locating and instantiating EJB components.
Creating and using custom tag libraries from a JSP client to call methods
on an EJB component.
A JSP client that accesses EJBs from OC4J(Oracle9iAS
Containers for J2EE).
Code from EjbHotels.java
...
/** * This method is executed at the start of the Tag. This calls the * method to initialize the EJB remote and home interface and also * processes the requests passed from the JSPs. Depending on the * Request Type, this method calls the other methods and prints out * the required HTML String using the JSP Writer **/ public int doStartTag() throws JspException { initEJB(); // Initialize EjbHrsBean l_ejbHrs = new EjbHrsBean(); // Instantiate the Bean JspWriter out = pageContext.getOut(); // Instantiate the JSP Writer try { if (m_reqType.equals("HOTELS")) { // Calling the getAllHotels() method to get all the hotels m_hotels = m_remoteInterface.getAllHotels() ; // Print the HTML String out.print(l_ejbHrs.generateAllHotelsHTML(m_hotels)); } else if (m_reqType.equals("HOTEL_DETAILS")) { // Get the Hotel Details using getHotelDetails method if (m_hotelDetails == null || m_hotelDetails.m_hotelID != m_hotelId){ m_hotelDetails = m_remoteInterface.getHotelDetails(m_hotelId); } // Print the HTML String out.print(l_ejbHrs.generateHotelDetailsHTML(m_hotelDetails)); } ... } catch (Exception e) { e.printStackTrace(); return SKIP_BODY; } return EVAL_BODY_INCLUDE; }
A JSP client that accesses EJBs from OC4J(Oracle9iAS
Containers for J2EE).
Code from MainPage.jsp
...
<jml:when condition = "<%= l_reqType.equals(\"HOTELS\") %>" > <%-- If the value of the parameter REQ_TYPE is "HOTELS" then, return the source for the right frame of the main page. Right frame contains the list of all Hotels available. --%> <BODY bgcolor="#bfbfbb"> <SCRIPT LANGUAGE="JavaScript1.1"> function putVal(val1) { document.forms[0].HOTEL_ID.value =val1; } </SCRIPT> <CENTER> <FORM> <%-- Instantiate the Session EJB for getting the hotel details --%> <ejb:useHome id="hotelSystemHome" type="HrsEjb.HotelSystem.ejb.HotelSystemHome" location="java:comp/env/ejb/HotelSystem" /> <ejb:useBean id="hotelSystemRemote" type="HrsEjb.HotelSystem.ejb.HotelSystem"
scope="session" > <ejb:createBean instance="<%=hotelSystemHome.create()%>" /> </ejb:useBean> <%-- Call EJB method getAllHotels() and pass the return value to the ejbHrs bean method to generate the HTML for the list of all hotels --%> <%= ejbHrs.generateAllHotelsHTML(hotelSystemRemote.getAllHotels())%> <BR> <input type=hidden name=HOTEL_ID value=""> </FORM> </CENTER> </BODY> </jml:when>
...