JavaServer Pages (JSP) Introduction Georgia Web Developers - - PowerPoint PPT Presentation

javaserver pages jsp introduction
SMART_READER_LITE
LIVE PREVIEW

JavaServer Pages (JSP) Introduction Georgia Web Developers - - PowerPoint PPT Presentation

JavaServer Pages (JSP) Introduction Georgia Web Developers Conference, July 25, 2001 2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 1 Who I Am Hans Bergsten hans@gefionsoftware.com President of Gefion software Member


slide-1
SLIDE 1

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 1

JavaServer Pages (JSP) Introduction

Georgia Web Developers Conference, July 25, 2001

slide-2
SLIDE 2

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 2

Who I Am

  • Hans Bergsten

– hans@gefionsoftware.com – President of Gefion software – Member of the JSP working group (JCP) – Author of JavaServer Pages (O’Reilly)

slide-3
SLIDE 3

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 3

Overview

  • JSP Background
  • Processing and Basic JSP 1.1 Elements
  • Extension Framework/Custom Tags
  • Flow Control and Scopes
  • Java 2 Enterprise Edition (J2EE)
  • Future Direction
slide-4
SLIDE 4

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 4

Background

  • Servlets part of JavaWebServer, 1997
  • Formalized as Servlet 2.1, Nov. 1998
slide-5
SLIDE 5

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 5

Code and HTML Mixed

public void doGet(…) { ResultSet rs = getCustomers(“VIP”);

  • ut.println(“<ul>”);

while (rs.next()) {

  • ut.println(“<li>” + rs.getString(“Name”);

}

slide-6
SLIDE 6

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 6

Background

  • Servlets part of JavaWebServer, 1997
  • Formalized as Servlet 2.1, Nov. 1998
  • JHTML, HTML with Java code snippets
  • JSP 1.0, June 1999
  • JSP 1.1, December 1999
slide-7
SLIDE 7

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 7

Code and HTML Separated

<foo:getVIPCust id=“vips” /> <ul> <foo:loop name=“vips” loopId=“cust” > <li> <jsp:getProperty name=“cust” property=“name” /> </foo:loop>

slide-8
SLIDE 8

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 8

JSP Elements

  • Directives: <%@ dirname attr=value %>
  • Actions: <pref:name attr=value … />
  • Scripting:

– Scriptlet: <% scripting code %> – Expression: <%= expression %> – Declaration: <%! declaration %>

slide-9
SLIDE 9

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 9

JSP Page Example

<%@ page contentType=“text/html” %> <%@ include file=“header.jsp” %> <h1>Hello World!</h1> <jsp:useBean id=“date” class=“java.util.Date” /> It’s <%= date %> <%@ include file=“footer.jsp” %>

slide-10
SLIDE 10

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 10

JSP Processing

hello.jsp hello.java hello.class request response read generate execute compile Web Server/ Web Container

slide-11
SLIDE 11

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 11

JSP Elements Example

slide-12
SLIDE 12

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 12

Directive Elements

  • page: Page-dependent attributes

– contentType, errorPage, buffer

  • include: Include another source file
  • taglib: Tag library declaration
slide-13
SLIDE 13

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 13

Directives Example

<%@ page contentType=“text/html” %> <%@ include file=“header.jsp” %> <h1>Hello World!</h1> <jsp:useBean id=“date” class=“java.util.Date()” /> It’s <%= date %> <%@ include file=“footer.jsp” %>

slide-14
SLIDE 14

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 14

Action Elements

  • useBean: Declare a bean
  • getProperty: Add property value to page
  • setProperty: Set property value
  • include: Include response from page
  • forward: Continue processing at page
  • plugin: Add HTML for Java Plugin
slide-15
SLIDE 15

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 15

JavaBeans Model

  • A “thing” with public properties
  • A JavaBeans component is a regular

Java class

  • Naming conventions for property access

methods

slide-16
SLIDE 16

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 16

JavaBeans Class Example

public class UserInfoBean { private String firstName; public void setFirstName(String fName) { firstName = fName; } public String getFirstName() { return firstName; } }

slide-17
SLIDE 17

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 17

useBean Example

<jsp:useBean id=“user” class=“com.foo.UserInfoBean” />

slide-18
SLIDE 18

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 18

setProperty Example

<jsp:setProperty name=“user” property=“firstName” value=“Hans” /> <jsp:setProperty name=“user” property=“*” /> <jsp:setProperty name=“user” property=“firstName” param=“fName” />

slide-19
SLIDE 19

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 19

getProperty Example

<jsp:getProperty name=“user” property=“firstName” />

slide-20
SLIDE 20

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 20

Using Actions and a Bean for Input Validation

<jsp:useBean id=“user” class=“com.foo.UserInfoBean” /> <jsp:setProperty name=“user” property=“*” /> Valid input? <jsp:getProperty name=“user” property=“valid” />

slide-21
SLIDE 21

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 21

include Example

<jsp:include page=“foo.jsp” flush=“true”> <jsp:param name=“user” value=“Bob” /> </jsp:include>

slide-22
SLIDE 22

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 22

Reusing Page Fragments

slide-23
SLIDE 23

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 23

Directive vs. Action

  • Directive

– In the translation phase – File merged into including page

  • Action

– In the request phase – Response added to including page’s response

slide-24
SLIDE 24

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 24

Using the Directive and the Action

<%@ include file=“header.jsp” %> <jsp:useBean id=“user” class=“…” /> <jsp:include page=“portfolio.jsp” flush=“true”> <jsp:param name=“user” value=“<%= user.getID() %>” /> </jsp:include>

slide-25
SLIDE 25

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 25

Scripting Elements

  • Scriptlet: <% scripting code %>
  • Expression: <%= expression %>
  • Declaration: <%! declaration %>
slide-26
SLIDE 26

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 26

Scriptlet Example

<% if (user.isValid()) { %> Do something <% } else { %> Do something else <% } %>

slide-27
SLIDE 27

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 27

Expression Example

First Name: <%= user.getFirstName() %> 1 + 1 = <%= 1 + 1 %>

slide-28
SLIDE 28

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 28

Declaration Example

<%! int pageCounter = 0; %> <% int requestCounter = 0; %> <h1>Counters</h1> Instance variable counter: <%= pageCounter++ %> Local variable counter: <%= requestCounter++ %>

slide-29
SLIDE 29

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 29

Implicit Scripting Variables

  • request
  • response
  • session
  • application
  • pageContext
  • and more
slide-30
SLIDE 30

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 30

Accessing Request Data

Method: <%= request.getMethod() %> URI: <%= request.getRequestURI() %> Server:<%= request.getServerName() %> Client: <%= request.getRemoteHost() %> Browser: <%=request.getHeader(“User-Agent”)%>

slide-31
SLIDE 31

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 31

Accessing Request Data

slide-32
SLIDE 32

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 32

Setting Response Data

<% Cookie c = new Cookie(“foo”, “bar”); c.setMaxAge(2592000); // Seconds response.addCookie(c); %>

slide-33
SLIDE 33

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 33

Custom Actions

  • Used the same as standard actions
  • Regular Java classes; JavaBeans with

a few extra methods

  • Invoked automatically by the container
  • Access to request and application data
  • Reduces the need for scripting code!
slide-34
SLIDE 34

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 34

Custom Actions Example

<%@ taglib uri=“/foolib” prefix=“foo” %> <foo:ifEq name=“user” property=“valid” value=“true”> <foo:save name=“user” /> </foo:ifEq>

slide-35
SLIDE 35

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 35

Custom Action Example

<%@ taglib uri=“/foolib” prefix=“foo” %> <foo:setCookie name=“foo” value=“bar” maxAge=“2592000” />

slide-36
SLIDE 36

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 36

MVC Pattern

  • Model: business data and logic
  • View: user’s view of business data
  • Controller: user interaction
slide-37
SLIDE 37

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 37

MVC Pattern for Web Apps

  • Model: JavaBeans
  • View: JSP page
  • Controller: JSP page or servlet
slide-38
SLIDE 38

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 38

MVC for Web Apps

input.jsp process.jsp confirm.jsp UserInfoBean View Controller Model View

slide-39
SLIDE 39

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 39

Page Flow Control

input.jsp process.jsp confirm.jsp UserInfoBean forward forward

slide-40
SLIDE 40

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 40

forward Example

<%@ taglib uri=“/foolib” prefix=“foo” %> <foo:ifEq name=“user” property=“valid” value=“true”> <foo:save name=“user” /> <jsp:forward page=“confirm.jsp” /> </foo:ifEq>

slide-41
SLIDE 41

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 41

JSP Data Scopes

input.jsp process.jsp confirm.jsp Page scope Request scope Session scope (one user)/Application scope (all users) Page scope Page scope

slide-42
SLIDE 42

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 42

Saving Data in a Scope

<useBean id=“user” class=“...” scope=“request” /> Or with a scriptlet: <% UserInfoBean u = new UserInfoBean(); request.setAttribute(“user”, u); %>

slide-43
SLIDE 43

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 43

Request Scope

input.jsp process.jsp confirm.jsp JavaBeans Request scope

slide-44
SLIDE 44

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 44

Session scope

prodlist.jsp addtocart.jsp cartcontents.jsp CartBean Session scope

slide-45
SLIDE 45

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 45

Application scope

prodlist.jsp addtocart.jsp cartcontents.jsp CartBean Session scope ProdBeans Application scope

slide-46
SLIDE 46

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 46

Web Container

Java 2 Enterprise Edition (J2EE)

Application Browser EJB Container Database

Servlet JSP EJB JSP EJB JDBC JTA/JTS JMS JNDI XML

Client Tier Middle Tier EIS Tier

slide-47
SLIDE 47

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 47

J2EE Application Example

htmlview.jsp Servlet wmlview.jsp EJB EJB IIOP/RMI HTTP WAP

Database

slide-48
SLIDE 48

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 48

Future Direction

  • JSP 1.2/Servlet 2.3

– include without flush – Listeners – Filters – Custom tag development enhancements

  • JSP Standard Tag Library (JSPTL)

– Conditionals, Iterators, XML/XSLT, Database access, etc.

slide-49
SLIDE 49

2001-07-25 GWDC: JavaServer Pages (JSP) Introduction 49

More Info

  • JSP Specification

http://java.sun.com/products/jsp/

  • JavaServer Pages book

http://TheJSPBook.com/

  • Jakarta Tomcat, Taglibs & Struts

http://jakarta.apache.org/

  • InstantOnline Basic

http://www.gefionsoftware.com/InstantOnline/