 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 25: JavaServer Pages Outline 25.1 Introduction 25.2 JavaServer Pages Overview 25.3 First JavaServer.

Slides:



Advertisements
Similar presentations
8 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: JavaServer Pages.
Advertisements

CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)
Java Server Pages Jeffrey Jongko. Introduction Java Server Pages (JSP) technology was created by Sun Microsystems and is built on top of Sun’s Java Servlet.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Michael Brockway Application Integration JavaServer Pages l Introduction & Overview l Implicit Objects l Scripting l Standard Actions l Directives l References.
Chapter 51 Scripting With JSP Elements JavaServer Pages By Xue Bai.
Chapter 25: JavaServer Pages Outline 25.1 Introduction 25.2 JavaServer Pages Overview 25.3 First JavaServer Page Example 25.4 Implicit Objects 25.5 Scripting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Pertemuan 10 Pemrograman JSP Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 37 - JavaServer Pages (JSP): Bonus for Java Developers Outline 37.1 Introduction 37.2 JavaServer.
11. Java-Based Web: JSP, JSF. 2 Motto: Rule 1: Our client is always right Rule 2: If you think our client is wrong, see Rule 1. - Anonymous.
 2002 Prentice Hall. All rights reserved. JavaServer Pages.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 25: JavaServer Pages Outline 25.1 Introduction 25.2 JavaServer Pages Overview 25.3 First JavaServer.
Java Server Pages CS-422. What are JSPs A logical evolution of java servlets –most servlets dynamically create HTML and integrate it with some computational.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
CSC 2720 Building Web Applications Using Java Beans, Custom Tags and Tag Libraries in JSP pages.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 30 – Bookstore Application: Client Tier Introducing.
 2002 Prentice Hall, Inc. All rights reserved.2 Chapter 2 — Introduction to HyperText Markup Language 4: Part I Outline 2.1Introduction 2.2Markup Languages.
Introduction to Java Server Pages (JSPs) Robert Thornton.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 25: JavaServer Pages Outline 25.1 Introduction 25.2 JavaServer Pages Overview 25.3 First JavaServer.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 31 - JavaServer Pages (JSP): Bonus for Java™ Developers Outline 31.1 Introduction 31.2 JavaServer.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
Java Server Pages Lecture July Java Server Pages Java Server Pages (JSPs) provide a way to separate the generation of dynamic content (java)
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
JSTL Lec Umair©2006, All rights reserved JSTL (ni) Acronym of  JavaServer Pages Standard Tag Library JSTL (like JSP) is a specification, not an.
Java Stream 1 Unit 10 – Java Server Pages Introduction A First JavaServer Page Example Implicit Objects Scripting Standard Actions Directives Custom Tag.
intro JSP Pertemuan 8 Matakuliah: Web Programming Tahun: 2009.
 2005 Pearson Education, Inc. All rights reserved JavaServer Pages (JSP)
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
1 Introduction to XHTML: Part 1 Outline Introduction Elements and Attributes Editing XHTML Common Elements W3C XHTML Validation Service Headers Linking.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Copyright © 2002 ProsoftTraining. All rights reserved. JavaServer Pages.
Java Server Pages An introduction to JSP. Containers and Components Several clients – one system.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CSC 2720 Building Web Applications JavaServer Pages (JSP) JSP Directives and Action Elements.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
JSP. Types of JSP Scripting Elements Expressions of the form, which are evaluated and inserted into the servlet's output. Scriptlets of the form, which.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
JSP BASICS AND ARCHITECTURE. Goals of JSP Simplify Creation of dynamic pages. Separate Dynamic and Static content.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
 2002 Prentice Hall. All rights reserved. Chapter 10: JavaServer Pages Outline 10.1 Introduction 10.2 JavaServer Pages Overview 10.3 A First JavaServer.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
CSI 3125, Preliminaries, page 1 JSP (Java Server Pages)
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
10 Copyright © 2004, Oracle. All rights reserved. Building ADF View Components.
 2002 Prentice Hall. All rights reserved. Chapter 10: JavaServer Pages Outline 10.1 Introduction 10.2 JavaServer Pages Overview 10.3 A First JavaServer.
Developing JavaServer Pages
Pre-assessment Questions
JavaServer Pages (JSP)
MSIS 655 Advanced Business Applications Programming
Java Server Pages.
Chapter 31 - JavaServer Pages (JSP): Bonus for Java™ Developers
COP 4610L: Applications in the Enterprise Spring 2005
COP 4610L: Applications in the Enterprise Spring 2005
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 25: JavaServer Pages Outline 25.1 Introduction 25.2 JavaServer Pages Overview 25.3 First JavaServer Page Example 25.4 Implicit Objects 25.5 Scripting Scripting Components Scripting Example 25.6 Standard Actions Action Action Action 25.7 Directives page Directive include Directive 25.8 Custom Tag Libraries 25.9 Internet and World Wide Web Resources

 2003 Prentice Hall, Inc. All rights reserved Introduction JavaServer Pages –Extension of Servlet technology Web content delivery Reuse existing Java components –Without programming Java Create custom tags –Encapsulate complex functionality Classes and interfaces specific to JSP –Package javax.servlet.jsp –Package javax.servlet.jsp.tagext

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview Key components –Directives –Actions –Scriptlets –Tag libraries

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) Directive –Message to JSP container i.e., program that compiles/executes JSPs –Enable programmers to specify Page settings Content to include from other resources Custom tag libraries used in the JSP

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) Action –Predefined JSP tags that encapsulate functionality –Often performed based on information from client request –Can be used to create Java objects for use in scriptlets

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) Scriptlet –Also called “Scripting Elements” –Enable programmers to insert Java code in JSPs –Performs request processing Interacts with page elements and other components to implement dynamic pages

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) Custom Tag Library –JSP’s tag extension mechanism –Enables programmers to define new tags Tags encapsulate complex functionality –Tags can manipulate JSP content

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) JSPs –Look like standard HTML or XHTML Normally include HTML or XHTML markup –Known as fixed-template data –Used when content is mostly fixed-template data Small amounts of content generated dynamically Servlets –Used when small amount of content is fixed-template data Most content generated dynamically

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) Some servlets do not produce content –Invoke other servlets and JSPs JSPs execute as part of a Web server –JSP container JSP first request –JSP container translates a JSP into a servlet Handle the current and future requests Code that represents the JSP –Placed in servlet’s _jspService method

 2003 Prentice Hall, Inc. All rights reserved JavaServer Pages Overview (cont.) JSP errors –Translation-time errors Occur when JSPs are translated into servlets –Request-time errors Occur during request processing Methods jspInit and jspDestroy –Container invokes when initializing and terminating a JSP Methods are defined in JSP declarations –Part of the JSP scripting mechanism

 2003 Prentice Hall, Inc. All rights reserved A First JavaServer Page Example Simple JSP example (Fig. 25.1) –Demonstrates Fixed-template data (XHTML markup) Creating a Java object ( java.util.Date ) Automatic conversion of JSP expression to a String meta element to refresh Web page at specified interval –First invocation of clock.jsp Notice the delay while: –JSP container translates the JSP into a servlet –JSP container compiles the servlet –JSP container executes the servlet Subsequent invocations should not experience the same delay

 2003 Prentice Hall, Inc. All rights reserved. Outline Cock.jsp Line 10 Meta element refershes the Web page every 60 seconds. 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " A Simple JSP Example big { font-family: helvetica, arial, sans-serif; 16 font-weight: bold; 17 font-size: 2em; } Simple JSP Example 23 meta element refreshes the Web page every 60 seconds

 2003 Prentice Hall, Inc. All rights reserved. Outline Cock.jsp Line 30 Creates Date object Creates Date object that is converted to a String implicitly and displayed in paragraph ( p ) element

 2003 Prentice Hall, Inc. All rights reserved. Outline Clock.jsp

 2003 Prentice Hall, Inc. All rights reserved Implicit Objects Implicit Objects –Provide access to many servlet capabilities within a JSP –Four scopes Application scope –Objects owned by the container application –Any servlet or JSP can manipulate these objects Page scope –Objects that exist only in page in which they are defined –Each page has its own instance of these objects Request scope –Objects exist for duration of client request –Objects go out of scope after response sent to client Session scope –Objects exist for duration of client’s browsing session –Objects go out of scope when client terminates session or when session timeout occurs

 2003 Prentice Hall, Inc. All rights reserved. 16

 2003 Prentice Hall, Inc. All rights reserved Scripting Scripting –Dynamically generated content –Insert Java code and logic in JSP using scripting

 2003 Prentice Hall, Inc. All rights reserved Scripting Components JSP scripting components –Scriptlets (delimited by ) –Comments JSP comments (delimited by ) XHTML comments (delimited by ) Java’s comments (delimited by // and /* and */ ) –Expressions (delimited by ) –Declarations (delimited by ) –Escape sequences

 2003 Prentice Hall, Inc. All rights reserved Scripting Components (cont.) Literal Escape sequence Description <% <\% The character sequence <% normally indicates the beginning of a scriptlet. The <\% escape sequence places the literal characters <% in the response to the client. %> %\> The character sequence %> normally indicates the end of a scriptlet. The %\> escape sequence places the literal characters %> in the response to the client. ' " \ \' \" \\ As with string literals in a Java program, the escape sequences for characters ', " and \ allow these characters to appear in attribute values. Remember that the literal text in a JSP becomes string literals in the servlet that represents the translated JSP. Fig JSP escape sequences.

 2003 Prentice Hall, Inc. All rights reserved Scripting Example Demonstrate basic scripting capabilities –Responding to get requests

 2003 Prentice Hall, Inc. All rights reserved. Outline welcome.jsp Lines Line <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Processing "get" requests with data <% // begin scriptlet String name = request.getParameter( "firstName" ); if ( name != null ) { %> 24 Scriptlets used to insert Java code Use request implicit object to get parameter

 2003 Prentice Hall, Inc. All rights reserved. Outline welcome.jsp Line 26 Lines and Hello, 27 Welcome to JavaServer Pages! <% // continue scriptlet } // end if 33 else { %> Type your first name and press Submit <% // continue scriptlet } // end else %> JSP expressionScriptlets used to insert Java code

 2003 Prentice Hall, Inc. All rights reserved. Outline welcome.jsp

 2003 Prentice Hall, Inc. All rights reserved Standard Actions JSP standard actions –Provide access to common tasks performed in a JSP Including content from other resources Forwarding requests to other resources Interacting with JavaBeans –JSP containers process actions at request time –Delimited by and

 2003 Prentice Hall, Inc. All rights reserved Standard Actions

 2003 Prentice Hall, Inc. All rights reserved Action action –Enables content to be included in a JSP dynamically –More flexible than include directive Requires more overhead when page contents change frequently

 2003 Prentice Hall, Inc. All rights reserved Action (cont.)

 2003 Prentice Hall, Inc. All rights reserved. Outline banner.html Java(TM), C, C++, Visual Basic(R), 6 Object Technology, and Internet and 7 World Wide Web Programming Training 8 On-Site Seminars Delivered Worldwide Clock Tower Place, Suite 200, 14 Maynard, MA

 2003 Prentice Hall, Inc. All rights reserved. Outline toc.html Publications/BookStore What's New Downloads/Resources FAQ (Frequently Asked Questions) Who we are 22 23

 2003 Prentice Hall, Inc. All rights reserved. Outline toc.html Home Page Send questions or comments about this site to Copyright by Deitel & Associates, Inc. 33 All Rights Reserved. 34

 2003 Prentice Hall, Inc. All rights reserved. Outline clock2.jsp Lines <p class = "big" style = "color: cyan; font-size: 3em; 8 font-weight: bold;"> <% 13 // get client locale 14 java.util.Locale locale = request.getLocale(); // get DateFormat for client's Locale 17 java.text.DateFormat dateFormat = 18 java.text.DateFormat.getDateTimeInstance( 19 java.text.DateFormat.LONG, 20 java.text.DateFormat.LONG, locale ); %> Use Locale to format Date with specified DateFormat

 2003 Prentice Hall, Inc. All rights reserved. Outline include.jsp 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Using jsp:include body { 14 font-family: tahoma, helvetica, arial, sans-serif; 15 } table, tr, td { 18 font-size:.9em; 19 border: 3px groove; 20 padding: 5px; 21 background-color: #dddddd; 22 }

 2003 Prentice Hall, Inc. All rights reserved. Outline include.jsp Lines Use JSP action to include banner.html. Line 48 Use JSP action to include toc.html <img src = "images/logotiny.png" 31 width = "140" height = "93" 32 alt = "Deitel & Associates, Inc. Logo" /> <jsp:include page = "banner.html" 39 flush = "true" /> Use JSP action to include banner.html Use JSP action to include toc.html

 2003 Prentice Hall, Inc. All rights reserved. Outline include.jsp Lines Use JSP action to include clock2.jsp <jsp:include page = "clock2.jsp" 56 flush = "true" /> Use JSP action to include clock2.jsp

 2003 Prentice Hall, Inc. All rights reserved. Outline include.jsp

 2003 Prentice Hall, Inc. All rights reserved Action action –Enables JSP to forward request to different resources Can forwarded requests only resources in same context action –Specifies name/value pairs of information Name/Value pairs are passed to other actions

 2003 Prentice Hall, Inc. All rights reserved. Outline forward1.jsp Lines <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Forward request to another JSP <% // begin scriptlet String name = request.getParameter( "firstName" ); if ( name != null ) { %> <jsp:param name = "date" 24 value = " " /> Forward request to forward2.jsp

 2003 Prentice Hall, Inc. All rights reserved. Outline forward1.jsp 27 <% // continue scriptlet } // end if 30 else { %> Type your first name and press Submit <% // continue scriptlet } // end else %>

 2003 Prentice Hall, Inc. All rights reserved. Outline forward2.jsp Lines <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " <html xmlns = " Processing a forwarded request big { 14 font-family: tahoma, helvetica, arial, sans-serif; 15 font-weight: bold; 16 font-size: 2em; 17 } Hello, 24 Your request was received and forwarded at Receive request from forward1.jsp, then get firstName parameter from request

 2003 Prentice Hall, Inc. All rights reserved. Outline forward2.jsp Line Get date parameter from request

 2003 Prentice Hall, Inc. All rights reserved Action action –Enables JSP to manipulate Java object Creates Java object or locates an existing object for use in JSP

 2003 Prentice Hall, Inc. All rights reserved Action (cont.)

 2003 Prentice Hall, Inc. All rights reserved. Outline Rotator.java 1 // Fig : Rotator.java 2 // A JavaBean that rotates advertisements. 3 package com.deitel.jhtp5.jsp; 4 5 public class Rotator { 6 private String images[] = { "images/advjHTP1.jpg", 7 "images/cppHTP4.jpg", "images/iw3HTP2.jpg", 8 "images/jwsFEP1.jpg", "images/vbnetHTP2.jpg" }; 9 10 private String links[] = { 11 " "deitelassociatin", 13 " "deitelassociatin", 15 " "deitelassociatin", 17 " "deitelassociatin", 19 " "deitelassociatin" }; private int selectedIndex = 0; 23

 2003 Prentice Hall, Inc. All rights reserved. Outline Rotator.java Lines Lines Lines // returns image file name for current ad 25 public String getImage() 26 { 27 return images[ selectedIndex ]; 28 } // returns the URL for ad's corresponding Web site 31 public String getLink() 32 { 33 return links[ selectedIndex ]; 34 } // update selectedIndex so next calls to getImage and 37 // getLink return a different advertisement 38 public void nextAd() 39 { 40 selectedIndex = ( selectedIndex + 1 ) % images.length; 41 } 42 } Update Rotator so subsequent calls to getImage and getLink return information for different advertisements Return image file name for book cover image Return hyperlink to book at Amazon.com

 2003 Prentice Hall, Inc. All rights reserved. Outline adrotator.jsp Lines 7-8 Line <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " <jsp:useBean id = "rotator" scope = "application" 8 class = "com.deitel.jhtp5.jsp.Rotator" /> AdRotator Example big { font-family: helvetica, arial, sans-serif; 17 font-weight: bold; 18 font-size: 2em } Use jsp:useBean action to obtain reference to Rotator object Invoke Rotator ’s nextAd method

 2003 Prentice Hall, Inc. All rights reserved. Outline adrotator.jsp Lines AdRotator Example <a href = "<jsp:getProperty name = "rotator" 30 property = "link" />"> <img src = "<jsp:getProperty name = "rotator" 33 property = "image" />" alt = "advertisement" /> Define hyperlink to Amazon.com site

 2003 Prentice Hall, Inc. All rights reserved Action (cont.)

 2003 Prentice Hall, Inc. All rights reserved Directives JSP directives –Messages to JSP container –Enable programmer to: Specify page settings Include content from other resources Specify custom-tag libraries –Delimited by

 2003 Prentice Hall, Inc. All rights reserved Directives (cont.)

 2003 Prentice Hall, Inc. All rights reserved page Directive JSP page directive –Specifies JSP’s global settings in JSP container

 2003 Prentice Hall, Inc. All rights reserved page Directive (cont.)

 2003 Prentice Hall, Inc. All rights reserved page Directive (cont.) Attribute Description buffer Specifies the size of the output buffer used with the implicit object out. The value of this attribute can be none for no buffering, or a value such as 8kb (the default buffer size). The JSP specification indicates that the buffer used must be at least the size specified. autoFlush When set to true (the default), this attribute indicates that the output buffer used with implicit object out should be flushed automatically when the buffer fills. If set to false, an exception occurs if the buffer overflows. This attribute’s value must be true if the buffer attribute is set to none. isThreadSafe Specifies if the page is thread safe. If true (the default), the page is considered to be thread safe, and it can process multiple requests at the same time. If false, the servlet that represents the page implements interface java.lang.SingleThreadModel and only one request can be processed by that JSP at a time. The JSP standard allows multiple instances of a JSP to exists for JSPs that are not thread safe. This enables the container to handle requests more efficiently. However, this does not guarantee that resources shared across JSP instances are accessed in a thread-safe manner. info Specifies an information string that describes the page. This string is returned by the getServletInfo method of the servlet that represents the translated JSP. This method can be invoked through the JSP’s implicit page object. Fig Attributes of the page directive.

 2003 Prentice Hall, Inc. All rights reserved page Directive (cont.)

 2003 Prentice Hall, Inc. All rights reserved include Directive JSP include directive –Includes content of another resource at JSP translation time Not as flexible as action

 2003 Prentice Hall, Inc. All rights reserved. Outline includeDirectiv e.jsp 1 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Using the include directive body { 14 font-family: tahoma, helvetica, arial, sans-serif; 15 } table, tr, td { 18 font-size:.9em; 19 border: 3px groove; 20 padding: 5px; 21 background-color: #dddddd; 22 }

 2003 Prentice Hall, Inc. All rights reserved. Outline includeDirectiv e.jsp Line 38 Line <img src = "images/logotiny.png" 31 width = "140" height = "93" 32 alt = "Deitel & Associates, Inc. Logo" /> Use include directive to include banner.html Use include directive to include toc.html

 2003 Prentice Hall, Inc. All rights reserved. Outline includeDirectiv e.jsp Line Use include directive to include clock2.jsp

 2003 Prentice Hall, Inc. All rights reserved Case Study: Guest Book Demonstrate –Action –JSP page directive –JSP error pages –Use of JDBC

 2003 Prentice Hall, Inc. All rights reserved. Outline GuestBean.java Line 6 1 // Fig : GuestBean.java 2 // JavaBean to store data for a guest in the guest book. 3 package com.deitel.jhtp5.jsp.beans; 4 5 public class GuestBean { 6 private String firstName, lastName, ; 7 8 // set the guest's first name 9 public void setFirstName( String name ) 10 { 11 firstName = name; 12 } // get the guest's first name 15 public String getFirstName() 16 { 17 return firstName; 18 } // set the guest's last name 21 public void setLastName( String name ) 22 { 23 lastName = name; 24 } 25 GuestBean defines three guest properties: firstName, lastName and

 2003 Prentice Hall, Inc. All rights reserved. Outline GuestBean.java 26 // get the guest's last name 27 public String getLastName() 28 { 29 return lastName; 30 } // set the guest's address 33 public void set ( String address ) 34 { 35 = address; 36 } // get the guest's address 39 public String get () 40 { 41 return ; 42 } 43 }

 2003 Prentice Hall, Inc. All rights reserved. Outline GuestDataBean.j ava Line 18 Line 21 Lines // Fig : GuestDataBean.java 2 // Class GuestDataBean makes a database connection and supports 3 // inserting and retrieving data from the database. 4 package com.deitel.jhtp5.jsp.beans; 5 6 // Java core packages 7 import java.io.*; 8 import java.sql.*; 9 import java.util.*; public class GuestDataBean { 12 private Connection connection; 13 private PreparedStatement addRecord, getRecords; // construct TitlesBean object 16 public GuestDataBean() throws Exception 17 { 18 System.setProperty( "db2j.system.home", "C:/Cloudscape_5.0" ); // load the Cloudscape driver 21 Class.forName( "com.ibm.db2j.jdbc.DB2jDriver" ); // connect to the database 24 connection = DriverManager.getConnection( 25 "jdbc:db2j:guestbook" ); 26 GuestDataBean connects to guestbook database Specify database locationLoad Cloudscape driver

 2003 Prentice Hall, Inc. All rights reserved. Outline GuestDataBean.j ava Lines statement = connection.createStatement(); 28 } // return an ArrayList of GuestBeans 31 public ArrayList getGuestList() throws SQLException 32 { 33 ArrayList guestList = new ArrayList(); // obtain list of titles 36 ResultSet results = statement.executeQuery( 37 "SELECT firstName, lastName, FROM guests" ); // get row data 40 while ( results.next() ) { 41 GuestBean guest = new GuestBean(); guest.setFirstName( results.getString( 1 ) ); 44 guest.setLastName( results.getString( 2 ) ); 45 guest.set ( results.getString( 3 ) ); guestList.add( guest ); 48 } return guestList; 51 } 52 GuestDataBean provides method getGuestList to manipulate database

 2003 Prentice Hall, Inc. All rights reserved. Outline GuestDataBean.j ava Lines // insert a guest in guestbook database 54 public void addGuest( GuestBean guest ) throws SQLException 55 { 56 statement.executeUpdate( "INSERT INTO guests ( firstName, " + 57 "lastName, ) VALUES ( '" + guest.getFirstName() + "', '" + 58 guest.getLastName() + "', '" + guest.get () + "' )" ); 59 } // close statements and terminate database connection 62 protected void finalize() 63 { 64 // attempt to close database connection 65 try { 66 statement.close(); 67 connection.close(); 68 } // process SQLException on close operation 71 catch ( SQLException sqlException ) { 72 sqlException.printStackTrace(); 73 } 74 } 75 } GuestDataBean provides methods addGuest to manipulate database

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- Login.jsp Line 8 Lines <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " <jsp:useBean id = "guest" scope = "page" 12 class = "com.deitel.jhtp5.jsp.beans.GuestBean" /> 13 <jsp:useBean id = "guestData" scope = "request" 14 class = "com.deitel.jhtp5.jsp.beans.GuestDataBean" /> Guest Book Login body { 23 font-family: tahoma, helvetica, arial, sans-serif; 24 } 25 page directive defines information that is globally available in JSP Use jsp:useBean actions to obtain references to GuestBean and GuestDataBean objects

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- Login.jsp Line table, tr, td { 27 font-size:.9em; 28 border: 3px groove; 29 padding: 5px; 30 background-color: #dddddd; 31 } <% // start scriptlet if ( guest.getFirstName() == null || 41 guest.getLastName() == null || 42 guest.get () == null ) { %> Enter your first name, last name and 48 address to register in our guest book. 49 Set properties of GuestBean with request parameter values

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- Login.jsp First name Last name

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- Login.jsp Line <input type = "submit" 78 value = "Submit" /> <% // continue scriptlet } // end if 87 else { 88 guestData.addGuest( guest ); %> <% // continue scriptlet } // end else %> Forward request to guestBookView.jsp

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- View.jsp Lines 9-10 Lines <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " <jsp:useBean id = "guestData" scope = "request" 14 class = "com.deitel.jhtp5.jsp.beans.GuestDataBean" /> Guest List body { 23 font-family: tahoma, helvetica, arial, sans-serif; 24 } 25 Use page directive import to specify Java classes and packages that are used in JSP context Use jsp:useBean action to obtain reference to GuestDataBean

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- View.jsp 26 table, tr, td, th { 27 text-align: center; 28 font-size:.9em; 29 border: 3px groove; 30 padding: 5px; 31 background-color: #dddddd; 32 } Guest List Last name 43 First name

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- View.jsp Lines <% // start scriptlet List guestList = guestData.getGuestList(); 53 Iterator guestListIterator = guestList.iterator(); 54 GuestBean guest; while ( guestListIterator.hasNext() ) { 57 guest = ( GuestBean ) guestListIterator.next(); %> "> Scriptlet displays last name, first name and address for all guests

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBook- View.jsp 72 <% // continue scriptlet } // end while %>

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBookError- View.jsp Line <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 " Error! bigRed { 19 font-size: 2em; 20 color: red; 21 font-weight: bold; 22 } Use page directive isErrorPage to specify that guestBookError- Page is an error page

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBookError- View.jsp Lines 31 and <% // scriptlet to determine exception type 30 // and output beginning of error message 31 if ( exception instanceof SQLException ) 32 %> An SQLException <% 37 else if ( exception instanceof ClassNotFoundException ) 38 %> A ClassNotFoundException <% 43 else 44 %> An exception Use implicit object exception to determine error to be displayed

 2003 Prentice Hall, Inc. All rights reserved. Outline guestBookError- View.jsp occurred while interacting with the guestbook database The error message was: Please try again later

 2003 Prentice Hall, Inc. All rights reserved Case Study: Guest Book (Cont.)

 2003 Prentice Hall, Inc. All rights reserved Case Study: Guest Book (Cont.)

 2003 Prentice Hall, Inc. All rights reserved Internet and World Wide Web Resources JSP at Sun Microsystems –java.sun.com/products/jsp Servlets at Sun Microsystems –java.sun.com/products/servlet J2EE at Sun Microsystems –java.sun.com/j2ee