84
Lesson 2 JSP Tutorial
Creating a home page JSP
JSPs let you create dynamic pages containing a combination of HTML, Java, and
scripting code. JSPs deliver high performance because the first time that a client requests
a JSP, the page is translated into a Java Servlet and compiled. At runtime, you execute the
compiled servlet.
Compass Travel tutorial application
In the Compass Travel tutorial application, you can browse adventure trip descriptions,
booking information, and make trip reservations. You use HTML, servlets, JSPs,
JavaBeans, and Enterprise JavaBeans (EJBs) to create the static and dynamic portions of
the application.
To get started with the tutorial lessons, see
Lesson 1, “Servlet Tutorial” on page 69
. In
this chapter, you add JSP code to the tutorial home page and use JSPs to access a
JavaBean that retrieves information from a database.
In the home page of the tutorial application, home.jsp, you code the following JSP
elements:
•
JSP directives to define page-wide attributes and insert text into a JSP page.
•
JSP scripting elements to query a database and define statements evaluated on the
server before sending the page output to the client.
Home.jsp displays a list of trips a customer can book. This lesson shows how to build a
JSP with embedded Java code to access database information.
To add JSP code to the Compass Adventures home page:
1 In a text editor or a Java IDE, open the file
home.jsp
located in jrun_root/servers/
tutorial/compass-ear/compass-war.
2 Where indicated by comments in the file, add the following code:
•
JSP page
import
directive
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*"%>
•
JSP
include
action which calls the header and welcome pages
<jsp:include page="header.jsp" flush="true"/>
<jsp:include page="welcome.jsp" flush="true"/>
•
JSP
include
directive to insert the footer file text
<%@ include file="footer.htm"%>
•
JSP scriptlet code to query a database for information about adventure trips
<%
java.text.NumberFormat nf = java.text.NumberFormat.getCurrencyInstance();
String sql="SELECT trip_id, name, teaser, price FROM trip";
Connection connection=null;
Statement stmt=null;
ResultSet trips=null;
try {
InitialContext ctx=new InitialContext();
DataSource ds=(DataSource) ctx.lookup("compass");
connection=ds.getConnection();
Содержание 38000382 - Macromedia JRun - Mac
Страница 1: ...Getting Started with JRun...
Страница 16: ......
Страница 68: ...54 Chapter 5 Introduction to EJB...
Страница 82: ......
Страница 110: ...96 Lesson 3 EJB Tutorial...
Страница 128: ...114 Lesson 4 Web Services Tutorial...
Страница 132: ...118 Index...