Java Server Pages JSP Anatomy of a JSP

  • Slides: 25
Download presentation
Java Server Pages (JSP) Anatomy of a JSP page JSP Components Directives page include

Java Server Pages (JSP) Anatomy of a JSP page JSP Components Directives page include taglib Standard Actions <jsp: include> <jsp: forward>, <jsp: param> <jsp: plugin> <jsp: use. Bean>, <jsp: set. Property>, <jsp: get. Property> Scripting Scriptlets (delimited by <% and %>) Comments (delimited by <%-- and --%>) Expressions (delimited by <%= and %>) Declarations (delimited by <%! and %>) Escape sequences Tag Libraries Implicit Objects Java. Beans 2002 Prentice Hall. All rights reserved.

Anatomy of a JSP page 2002 Prentice Hall. All rights reserved.

Anatomy of a JSP page 2002 Prentice Hall. All rights reserved.

JSP Components • • Directives Standard Actions Scriptlets Tag libraries 2002 Prentice Hall. All

JSP Components • • Directives Standard Actions Scriptlets Tag libraries 2002 Prentice Hall. All rights reserved.

Directives • A message to the container that lets you specify: – Page settings

Directives • A message to the container that lets you specify: – Page settings – Content to include from other resources – Custom tag libraries used in the JSP • Delimited by <%@ and %> 2002 Prentice Hall. All rights reserved.

page Directive • Specifies JSP’s global settings in JSP container • First line of

page Directive • Specifies JSP’s global settings in JSP container • First line of a JSP file • Example: <%@ page language=“java” content. Type=“text/html” %> 2002 Prentice Hall. All rights reserved.

page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

page Directive (cont. ) 2002 Prentice Hall. All rights reserved.

include Directive • Includes content of another resource at JSP translation time • Not

include Directive • Includes content of another resource at JSP translation time • Not as flexible as <jsp: include> action • Example: <tr> <td style = "width: 160 px"> <%-- include toc. html in this JSP --%> <%@ include file = "toc. html" %> </td> <td style = "vertical-align: top"> <%-- include clock 2. jsp in this JSP --%> <%@ include file = "clock 2. jsp" %> </td> </tr> 2002 Prentice Hall. All rights reserved.

Standard Actions • Predefined JSP tags that encapsulate functionality – Provide access to common

Standard Actions • Predefined JSP tags that encapsulate functionality – Provide access to common tasks performed in a JSP • Including content from other resources • Forwarding requests to other resources • Interacting with Java. Beans – JSP containers process actions at request time – Delimited by <jsp: action> and </jsp: action> • Often performed based on information from client request • Can be used to create Java objects for use in scriptlets 2002 Prentice Hall. All rights reserved.

Standard Actions (cont. ) 2002 Prentice Hall. All rights reserved.

Standard Actions (cont. ) 2002 Prentice Hall. All rights reserved.

Standard Actions (cont. ) 2002 Prentice Hall. All rights reserved.

Standard Actions (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: include> Action • Enables dynamic content to be included in a JSP –

<jsp: include> Action • Enables dynamic content to be included in a JSP – More flexible than include directive • Requires more overhead when page contents change frequently • Example: <jsp: include page="banner. html" flush="true" /> 2002 Prentice Hall. All rights reserved.

<jsp: forward> & <jsp: param> Actions • <jsp: forward> action – Enables JSP to

<jsp: forward> & <jsp: param> Actions • <jsp: forward> action – Enables JSP to forward request to different resource • <jsp: param> action – Specifies name/value pairs of information to pass to other actions • Example: <jsp: forward page=“forward 2. jsp"> <jsp: param name = "date" value = "<%= new java. util. Date() %>" /> </jsp: forward> 2002 Prentice Hall. All rights reserved.

<jsp: plugin> Action • Adds an applet or Java. Bean to a Web page

<jsp: plugin> Action • Adds an applet or Java. Bean to a Web page <html> <head> <title>Using jsp: plugin to load an applet</title> </head> <body> <jsp: plugin type = "applet" code = "com. deitel. advjhtp 1. jsp. applet. Shapes. Applet" codebase = "/advjhtp 1/jsp" width = "400" height = "400"> <jsp: params> <jsp: param name = "red" value = "255" /> <jsp: param name = "green" value = "255" /> <jsp: param name = "blue" value = "0" /> </jsp: params> </jsp: plugin> </body> </html> 2002 Prentice Hall. All rights reserved.

<jsp: plugin> Action (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: plugin> Action (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean>, <jsp: set. Property> & <jsp: get. Property> Actions • <jsp: use.

<jsp: use. Bean>, <jsp: set. Property> & <jsp: get. Property> Actions • <jsp: use. Bean> action – Enables JSP to manipulate Java object – Creates Java object or locates an existing object for use in JSP – See below for more on Java. Beans 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean>, <jsp: set. Property> & <jsp: get. Property> Actions (cont. ) 2002

<jsp: use. Bean>, <jsp: set. Property> & <jsp: get. Property> Actions (cont. ) 2002 Prentice Hall. All rights reserved.

<jsp: use. Bean>, <jsp: set. Property> & <jsp: get. Property> Actions 2002 Prentice Hall.

<jsp: use. Bean>, <jsp: set. Property> & <jsp: get. Property> Actions 2002 Prentice Hall. All rights reserved.

Scripting • How JSP programmers can insert Java code and logic • JSP scripting

Scripting • How JSP programmers can insert Java code and logic • JSP scripting components – – – Scriptlets (delimited by <% and %>) Comments (delimited by <%-- and --%>) Expressions (delimited by <%= and %>) Declarations (delimited by <%! and %>) Escape sequences 2002 Prentice Hall. All rights reserved.

Scripting Escape Sequences 2002 Prentice Hall. All rights reserved.

Scripting Escape Sequences 2002 Prentice Hall. All rights reserved.

Implicit Objects – Implicit Objects make standard servlet objects (such as Http. Servlet. Request

Implicit Objects – Implicit Objects make standard servlet objects (such as Http. Servlet. Request and Http. Servlet. Response) available to a JSP – Four scopes for Implicit Objects & Java. Beans • 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 when 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 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Implicit Objects (cont. ) 2002 Prentice Hall. All rights reserved.

Java. Beans – Not to be confused with Enterprise. Java. Beans (EJB) – When

Java. Beans – Not to be confused with Enterprise. Java. Beans (EJB) – When is an object a Bean? “A simple Java object is a Java bean when all of the object’s data fields are private and are only accessible through accessor methods. That’s it! These requirements should be followed in object-oriented programming anyway. It’s just that Java. Beans forces you to use proper object-oriented programming techniques. ” – Other constraints • No-arg constructor – If you do not include a no-arg constructor and the container needs to instantiate & initialize an object, the container will use the superclass no-arg constuctor (Object if no other class is explicitly extended) • Must be in a package for use by the container – Differences between beans & EJBs • Ordinary beans normally reside on same machine; EJBs support a distributed, transactional multitier architecture • EJBs managed by EJB container • Ordinary beans can be created using a “Bean box” tool that implements the java. beans API. 2002 Prentice Hall. All rights reserved.