SlideShare a Scribd company logo
Java/J2EE Programming Training
Including File and Applet in JSP Pages
Page 2Classification: Restricted
Agenda
• Including Files at Request Time: jsp:include
• Understanding jsp:include vs. <%@ include … %>
• Options for Deploying Applets
• Using jsp:plugin
• Attributes of
the jsp:plugin Element
• Using JavaBeans Components in JSP Documents
• Background: What Are Beans?
• Using Beans: Basic Tasks
• Setting Simple Bean Properties: jsp:setProperty
• JSP Page That Uses StringBean(Code)
• Conditional Bean Operations
• Sharing Beans in Four Different Ways
• Session-Based Sharing: Code
• Application-Based Sharing: Code
• Application-Based Sharing: Result
Page 3Classification: Restricted
• Format
– <jsp:include page="Relative URL" />
• Purpose
– To reuse JSP, HTML, or plain text content
– To permit updates to the included content without changing the main JSP
page(s)
• Notes
– JSP content cannot affect main page:
only output of included JSP page is used
– Don't forget that trailing slash
– Relative URLs that starts with slashes are interpreted relative to the Web
app, not relative to the server root.
– You are permitted to include files from WEB-INF
Including Files at Request Time: jsp:include
Page 4Classification: Restricted
• …
• <BODY>
• <TABLE BORDER=5 ALIGN="CENTER">
• <TR><TH CLASS="TITLE">
• What's New at JspNews.com</TABLE>
• <P>
• Here is a summary of our three
• most recent news stories:
• <OL>
• <LI><jsp:include page="/WEB-INF/Item1.html" />
• <LI><jsp:include page="/WEB-INF/Item2.html" />
• <LI><jsp:include page="/WEB-INF/Item3.html" />
• </OL>
• </BODY></HTML>
jsp:include Example: A News Headline Page (Main Page)
Page 5Classification: Restricted
A News Headline Page: Result
Page 6Classification: Restricted
• Use jsp:include whenever possible
– Changes to included page do not require any
manual updates
– Speed difference between jsp:include and the include directive
(@include) is insignificant
• The include directive (<%@ include …%>) has additional power, however
– Main page
• <%! int accessCount = 0; %>
– Included page
• <%@ include file="snippet.jsp" %>
• <%= accessCount++ %>
Which Should You Use?
Page 7Classification: Restricted
• Footer defined the accessCount field (instance variable)
•
• If main pages used accessCount, they would have to use @include
– Otherwise accessCount would be undefined
• In this example, the main page did not use accessCount
– So why did we use @include?
Understanding jsp:include vs. <%@ include … %>
Page 8Classification: Restricted
• Develop the applets with JDK 1.1 or even 1.02 (to support really old
browsers).
– Works with almost any browser
– Uses the simple APPLET tag
• Have users install version 1.4 of the Java Runtime Environment (JRE), then
use JDK 1.4 for the applets.
– Requires IE 5 or later or Netscape 6 or later
– Uses the simple APPLET tag
• Have users install any version of the Java 2 Plug-in, then use Java 2 for the
applets.
– Works with almost any browser
– Uses ugly OBJECT and EMBED tags
Options for Deploying Applets
Page 9Classification: Restricted
• Simple APPLET-like tag
– Expands into the real OBJECT and EMBED tags
• APPLET Tag
– <APPLET CODE="MyApplet.class"
WIDTH=475 HEIGHT=350>
</APPLET>
• Equivalent jsp:plugin
– <jsp:plugin type="applet"
code="MyApplet.class"
width="475" height="350">
</jsp:plugin>
• Reminder
– JSP element and attribute names are case sensitive
– All attribute values must be in single or double quotes
– This is like XML but unlike HTML
Using jsp:plugin
Page 10Classification: Restricted
<jsp:plugin type="applet"
code="SomeApplet.class"
width="300" height="200">
</jsp:plugin>
jsp:plugin: Source Code
Page 11Classification: Restricted
• …
• <BODY>
• <CENTER>
• <TABLE BORDER=5>
• <TR><TH CLASS="TITLE">
• Using jsp:plugin</TABLE>
• <P>
• <jsp:plugin type="applet"
• code="PluginApplet.class"
• width="370" height="420">
• </jsp:plugin>
• </CENTER></BODY></HTML>
jsp:plugin: Example (JSP Code)
Page 12Classification: Restricted
• import javax.swing.*;
• /** An applet that uses Swing and Java 2D
• * and thus requires the Java Plug-in.
• */
• public class PluginApplet extends JApplet {
• public void init() {
• WindowUtilities.setNativeLookAndFeel();
• setContentPane(new TextPanel());
• }
• }
• Where are .class files installed?
jsp:plugin: Example (Java Code)
Page 13Classification: Restricted
• type
– For applets, this should be "applet".
Use "bean" to embed JavaBeans elements in Web pages.
• code
– Used identically to CODE attribute of APPLET, specifying the top-level
applet class file
• width, height
– Used identically to WIDTH, HEIGHT in APPLET
• codebase
– Used identically to CODEBASE attribute of APPLET
• align
– Used identically to ALIGN in APPLET and IMG
Attributes of the jsp:plugin Element
Page 14Classification: Restricted
• hspace, vspace
– Used identically to HSPACE, VSPACE in APPLET,
• archive
– Used identically to ARCHIVE attribute of APPLET, specifying a JAR file from
which classes and images should be loaded
• name
– Used identically to NAME attribute of APPLET, specifying a name to use
for inter-applet communication or for identifying applet to scripting
languages like JavaScript.
• title
– Used identically to rarely used TITLE attribute
Attributes of the jsp:plugin Element (Cont.)
Page 15Classification: Restricted
• jreversion
– Identifies version of the Java Runtime Environment (JRE) that is required.
Default is 1.2.
• iepluginurl
– Designates a URL from which plug-in for Internet Explorer can be
downloaded. Users who don’t already have the plug-in installed will be
prompted to download it from this location. Default value will direct user
to Sun site, but for intranet use you might want to direct user to a local
copy.
• nspluginurl
– Designates a URL from which plug-in for Netscape can be downloaded.
Default value will direct user to Sun site, but for intranet use you might
want local copy.
Attributes of the jsp:plugin Element (Cont.)
Page 16Classification: Restricted
• PARAM Tags
– <APPLET CODE="MyApplet.class"
WIDTH=475 HEIGHT=350>
<PARAM NAME="PARAM1" VALUE="VALUE1">
<PARAM NAME="PARAM2" VALUE="VALUE2">
</APPLET>
• Equivalent jsp:param
– <jsp:plugin type="applet"
code="MyApplet.class"
width="475" height="350">
<jsp:params>
<jsp:param name="PARAM1" value="VALUE1" />
<jsp:param name="PARAM2" value="VALUE2" />
</jsp:params>
</jsp:plugin>
The jsp:param and jsp:params Elements
Using JavaBeans Components
in JSP Documents
Page 18Classification: Restricted
• Scripting elements calling servlet code directly
• Scripting elements calling servlet code indirectly (by means of
utility classes)
• Beans
• Servlet/JSP combo (MVC)
• MVC with JSP expression language
• Custom tags
Simple
Application
Complex
Application
Uses of JSP Constructs
Page 19Classification: Restricted
• Java classes that follow certain conventions
– Must have a zero-argument (empty) constructor
• You can satisfy this requirement either by explicitly defining such a
constructor or by omitting all constructors
– Should have no public instance variables (fields)
• I hope you already follow this practice and use accessor methods
instead of allowing direct access to fields
– Persistent values should be accessed through methods called getXxx and
setXxx
• If class has method getTitle that returns a String, class is said to have a
String property named title
• Boolean properties use isXxx instead of getXxx
– For more on beans, see
http://java.sun.com/beans/docs/
Background: What Are Beans?
Page 20Classification: Restricted
• jsp:useBean
– In the simplest case, this element builds a new bean.
It is normally used as follows:
• <jsp:useBean id="beanName" class="package.Class" />
• jsp:getProperty
– This element reads and outputs the value of a bean property.
It is used as follows:
• <jsp:getProperty name="beanName" property="propertyName" />
• jsp:setProperty
– This element modifies a bean property (i.e., calls a method of the form
setXxx). It is normally used as follows:
• <jsp:setProperty name="beanName"
• property="propertyName"
• value="propertyValue" />
Using Beans: Basic Tasks
Page 21Classification: Restricted
• Format
– <jsp:useBean id="name" class="package.Class" />
• Purpose
– Allow instantiation of Java classes without explicit Java programming
(XML-compatible syntax)
• Notes
– Simple interpretation:
<jsp:useBean id="book1" class="coreservlets.Book" />
can be thought of as equivalent to the scriptlet
<% coreservlets.Book book1 = new coreservlets.Book(); %>
– But jsp:useBean has two additional advantages:
• It is easier to derive object values from request parameters
• It is easier to share objects among pages or servlets
Building Beans: jsp:useBean
Page 22Classification: Restricted
• Format
– <jsp:getProperty name="name" property="property" />
• Purpose
– Allow access to bean properties (i.e., calls to getXxx methods) without
explicit Java programming
• Notes
– <jsp:getProperty name="book1" property="title" />
is equivalent to the following JSP expression
<%= book1.getTitle() %>
Accessing Bean Properties: jsp:getProperty
Page 23Classification: Restricted
• Format
– <jsp:setProperty name="name"
property="property"
value="value" />
• Purpose
– Allow setting of bean properties (i.e., calls to setXxx methods) without
explicit Java programming
• Notes
– <jsp:setProperty name="book1"
property="title"
value="Core Servlets and JavaServer Pages" />
is equivalent to the following scriptlet
<% book1.setTitle("Core Servlets and JavaServer Pages"); %>
Setting Simple Bean Properties: jsp:setProperty
Page 24Classification: Restricted
• package coreservlets;
• public class StringBean {
• private String message = "No message specified";
• public String getMessage() {
• return(message);
• }
• public void setMessage(String message) {
• this.message = message;
• }
• }
• Beans installed in normal Java directory
– …/WEB-INF/classes/directoryMatchingPackageName
• Beans (and utility classes) must always be in packages!
Example: StringBean
Page 25Classification: Restricted
• <jsp:useBean id="stringBean"
• class="coreservlets.StringBean" />
• <OL><LI>Initial value (from jsp:getProperty):
• <I><jsp:getProperty name="stringBean"
• property="message" /></I>
• <LI>Initial value (from JSP expression):
• <I><%= stringBean.getMessage() %></I>
• <LI><jsp:setProperty name="stringBean"
• property="message"
• value="Best string bean: Fortex" /> Value after setting property with
jsp:setProperty:
• <I><jsp:getProperty name="stringBean"
• property="message" /></I>
• <LI><% stringBean.setMessage
• ("My favorite: Kentucky Wonder"); %>
• Value after setting property with scriptlet:
• <I><%= stringBean.getMessage() %></I>
• </OL>
JSP Page That Uses StringBean (Code)
Page 26Classification: Restricted
• You can use the scope attribute to specify additional places where bean is
stored
– Still also bound to local variable in _jspService
– <jsp:useBean id="…" class="…"
scope="…" />
• Lets multiple servlets or JSP pages
share data
• Also permits conditional bean creation
– Creates new object only if it can't find existing one
Sharing Beans
Page 27Classification: Restricted
• page (<jsp:useBean … scope="page"/> or
<jsp:useBean…>)
– Default value. Bean object should be placed in the PageContext object for
the duration of the current request. Lets methods in same servlet access
bean
• application
(<jsp:useBean … scope="application"/>)
– Bean will be stored in ServletContext (available through the application
variable or by call to getServletContext()). ServletContext is shared by all
servlets in the same Web application (or all servlets on server if no explicit
Web applications are defined).
Values of the scope Attribute
Page 28Classification: Restricted
• session
(<jsp:useBean … scope="session"/>)
– Bean will be stored in the HttpSession object associated with the current
request, where it can be accessed from regular servlet code with
getAttribute and setAttribute, as with normal session objects.
• request
(<jsp:useBean … scope="request"/>)
– Bean object should be placed in the ServletRequest object for the
duration of the current request, where it is available by means of
getAttribute
Values of the scope Attribute
Page 29Classification: Restricted
• Bean conditionally created
– jsp:useBean results in new bean being instantiated only if no bean with
same id and scope can be found.
– If a bean with same id and scope is found, the preexisting bean is simply
bound to variable referenced by id.
• Bean properties conditionally set
– <jsp:useBean ... />
replaced by
<jsp:useBean ...>statements</jsp:useBean>
– The statements (jsp:setProperty elements) are executed only if a new
bean is created, not if an existing bean is found.
Conditional Bean Operations
Page 30Classification: Restricted
• Using unshared (page-scoped) beans.
• Sharing request-scoped beans.
• Sharing session-scoped beans.
• Sharing application-scoped (i.e., ServletContext-scoped) beans.
Note:
– Use different names (i.e., id in jsp:useBean) for different beans
Sharing Beans in Four Different Ways
Page 31Classification: Restricted
• …
• <BODY>
• <H1>Baked Bean Values: session-based Sharing</H1>
• <jsp:useBean id="sessionBean"
• class="coreservlets.BakedBean"
• scope="session" />
• <jsp:setProperty name="sessionBean"
• property="*" />
• <H2>Bean level:
• <jsp:getProperty name="sessionBean"
• property="level" />
• </H2>
• <H2>Dish bean goes with:
• <jsp:getProperty name="sessionBean"
• property="goesWith" />
• </H2></BODY></HTML>
Session-Based Sharing: Code
Page 32Classification: Restricted
• <BODY>
• <H1>Baked Bean Values:
• application-based Sharing</H1>
• <jsp:useBean id="applicationBean"
• class="coreservlets.BakedBean"
• scope="application" />
• <jsp:setProperty name="applicationBean"
• property="*" />
• <H2>Bean level:
• <jsp:getProperty name="applicationBean"
• property="level" />
• </H2>
• <H2>Dish bean goes with:
• <jsp:getProperty name="applicationBean"
• property="goesWith"/>
• </H2></BODY></HTML>
Application-Based Sharing: Code
Page 33Classification: Restricted
Application-Based Sharing: Result (Later Request -- Same Client)
Page 34Classification: Restricted
Client
Browser
1. Request
4. Response
JSP
2. Create
JavaBeans
3. Retrieve data
DB
Application Server
The JSP Model 1 Architecture
Page 35Classification: Restricted
JavaBeans
Client
Browser
1. Request
6. Response
Servlet
(Controller
2. Create
JavaBeans
DB
Application Server
JSP (View)
4.Invoke JSP
5.Use
JavaBeans
3.Retrieve
data
The JSP Model 2 Architecture
Page 36Classification: Restricted
Thank You

More Related Content

What's hot (20)

PDF
20jsp
Adil Jafri
 
PDF
Jsp
Priya Goyal
 
PPS
Jsp chapter 1
kamal kotecha
 
PPTX
Java Server Pages
Shah Nawaz Bhurt
 
PPT
JSP
vikram singh
 
PPTX
JSP - Java Server Page
Vipin Yadav
 
PPT
Jsp(java server pages)
Khan Mac-arther
 
PPT
Java Server Pages
BG Java EE Course
 
PPTX
Jsp presentation
Sher Singh Bardhan
 
PPTX
API Development with Laravel
Michael Peacock
 
PPTX
Getting started with laravel
Advance Idea Infotech
 
PPTX
JSP Directives
ShahDhruv21
 
PPSX
JSP - Part 2 (Final)
Hitesh-Java
 
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Aaqib Hussain
 
PPT
Jsp slides
Kumaran K
 
PDF
Spring 3 - Der dritte Frühling
Thorsten Kamann
 
PDF
Staying Sane with Drupal NEPHP
Oscar Merida
 
PPTX
CQ Provisionning & Authoring
Gabriel Walt
 
PPTX
JAVA SERVER PAGES
Kalpana T
 
PPT
Jsp Slides
DSKUMAR G
 
20jsp
Adil Jafri
 
Jsp chapter 1
kamal kotecha
 
Java Server Pages
Shah Nawaz Bhurt
 
JSP - Java Server Page
Vipin Yadav
 
Jsp(java server pages)
Khan Mac-arther
 
Java Server Pages
BG Java EE Course
 
Jsp presentation
Sher Singh Bardhan
 
API Development with Laravel
Michael Peacock
 
Getting started with laravel
Advance Idea Infotech
 
JSP Directives
ShahDhruv21
 
JSP - Part 2 (Final)
Hitesh-Java
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Aaqib Hussain
 
Jsp slides
Kumaran K
 
Spring 3 - Der dritte Frühling
Thorsten Kamann
 
Staying Sane with Drupal NEPHP
Oscar Merida
 
CQ Provisionning & Authoring
Gabriel Walt
 
JAVA SERVER PAGES
Kalpana T
 
Jsp Slides
DSKUMAR G
 

Similar to JSP Part 2 (20)

PDF
13 java beans
snopteck
 
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
WebStackAcademy
 
PPTX
JSP APP DEVLOPMENT.pptx Related to Android App Development
BhawnaSaini45
 
PPT
Spring introduction
AnilKumar Etagowni
 
PPTX
JSP.pptx
NishaRohit6
 
PPTX
Jsp and jstl
vishal choudhary
 
PPSX
Java server pages
Tanmoy Barman
 
PDF
Intro To Sap Netweaver Java
Leland Bartlett
 
PDF
14 mvc
snopteck
 
PPTX
Cis 274 intro
Aren Zomorodian
 
PDF
java beans
lapa
 
PDF
Coursejspservlets00
Rajesh Moorjani
 
PDF
Jeetrainers.com coursejspservlets00
Rajesh Moorjani
 
PPTX
jsp unit 3byudoue8euwuuutrttttyyii90oigyu7
Shashankk46
 
PDF
Using java beans(ii)
Ximentita Hernandez
 
PPTX
JSP_Complete_Guide_With_Step_By_Step_solution
powerofthehelios
 
PPTX
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
13 java beans
snopteck
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
WebStackAcademy
 
JSP APP DEVLOPMENT.pptx Related to Android App Development
BhawnaSaini45
 
Spring introduction
AnilKumar Etagowni
 
JSP.pptx
NishaRohit6
 
Jsp and jstl
vishal choudhary
 
Java server pages
Tanmoy Barman
 
Intro To Sap Netweaver Java
Leland Bartlett
 
14 mvc
snopteck
 
Cis 274 intro
Aren Zomorodian
 
java beans
lapa
 
Coursejspservlets00
Rajesh Moorjani
 
Jeetrainers.com coursejspservlets00
Rajesh Moorjani
 
jsp unit 3byudoue8euwuuutrttttyyii90oigyu7
Shashankk46
 
Using java beans(ii)
Ximentita Hernandez
 
JSP_Complete_Guide_With_Step_By_Step_solution
powerofthehelios
 
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
Ad

More from DeeptiJava (13)

PPT
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
PPTX
Java Generics
DeeptiJava
 
PPTX
Java Collection
DeeptiJava
 
PPTX
Java Exception Handling
DeeptiJava
 
PPTX
Java OOPs
DeeptiJava
 
PPTX
Java Access Specifier
DeeptiJava
 
PPTX
Java JDBC
DeeptiJava
 
PPTX
Java Thread
DeeptiJava
 
PPTX
Java Inner Class
DeeptiJava
 
PPT
JSP Part 1
DeeptiJava
 
PPTX
Java I/O
DeeptiJava
 
PPT
Java Hibernate Basics
DeeptiJava
 
PPTX
Introduction to Java
DeeptiJava
 
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
Java Generics
DeeptiJava
 
Java Collection
DeeptiJava
 
Java Exception Handling
DeeptiJava
 
Java OOPs
DeeptiJava
 
Java Access Specifier
DeeptiJava
 
Java JDBC
DeeptiJava
 
Java Thread
DeeptiJava
 
Java Inner Class
DeeptiJava
 
JSP Part 1
DeeptiJava
 
Java I/O
DeeptiJava
 
Java Hibernate Basics
DeeptiJava
 
Introduction to Java
DeeptiJava
 
Ad

Recently uploaded (20)

PDF
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
How a Code Plagiarism Checker Protects Originality in Programming
Code Quiry
 
PDF
UiPath vs Other Automation Tools Meeting Presentation.pdf
Tracy Dixon
 
PPTX
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
PDF
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
PDF
Upskill to Agentic Automation 2025 - Kickoff Meeting
DianaGray10
 
PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PPTX
Lecture 5 - Agentic AI and model context protocol.pptx
Dr. LAM Yat-fai (林日辉)
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
PDF
Rethinking Security Operations - Modern SOC.pdf
Haris Chughtai
 
PDF
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
PDF
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 
Arcee AI - building and working with small language models (06/25)
Julien SIMON
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
How a Code Plagiarism Checker Protects Originality in Programming
Code Quiry
 
UiPath vs Other Automation Tools Meeting Presentation.pdf
Tracy Dixon
 
The Yotta x CloudStack Advantage: Scalable, India-First Cloud
ShapeBlue
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
How Current Advanced Cyber Threats Transform Business Operation
Eryk Budi Pratama
 
visibel.ai Company Profile – Real-Time AI Solution for CCTV
visibelaiproject
 
Upskill to Agentic Automation 2025 - Kickoff Meeting
DianaGray10
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Ampere Offers Energy-Efficient Future For AI And Cloud
ShapeBlue
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Lecture 5 - Agentic AI and model context protocol.pptx
Dr. LAM Yat-fai (林日辉)
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
OpenInfra ID 2025 - Are Containers Dying? Rethinking Isolation with MicroVMs.pdf
Muhammad Yuga Nugraha
 
Rethinking Security Operations - Modern SOC.pdf
Haris Chughtai
 
HR agent at Mediq: Lessons learned on Agent Builder & Maestro by Tacstone Tec...
UiPathCommunity
 
The Past, Present & Future of Kenya's Digital Transformation
Moses Kemibaro
 

JSP Part 2

  • 1. Java/J2EE Programming Training Including File and Applet in JSP Pages
  • 2. Page 2Classification: Restricted Agenda • Including Files at Request Time: jsp:include • Understanding jsp:include vs. <%@ include … %> • Options for Deploying Applets • Using jsp:plugin • Attributes of the jsp:plugin Element • Using JavaBeans Components in JSP Documents • Background: What Are Beans? • Using Beans: Basic Tasks • Setting Simple Bean Properties: jsp:setProperty • JSP Page That Uses StringBean(Code) • Conditional Bean Operations • Sharing Beans in Four Different Ways • Session-Based Sharing: Code • Application-Based Sharing: Code • Application-Based Sharing: Result
  • 3. Page 3Classification: Restricted • Format – <jsp:include page="Relative URL" /> • Purpose – To reuse JSP, HTML, or plain text content – To permit updates to the included content without changing the main JSP page(s) • Notes – JSP content cannot affect main page: only output of included JSP page is used – Don't forget that trailing slash – Relative URLs that starts with slashes are interpreted relative to the Web app, not relative to the server root. – You are permitted to include files from WEB-INF Including Files at Request Time: jsp:include
  • 4. Page 4Classification: Restricted • … • <BODY> • <TABLE BORDER=5 ALIGN="CENTER"> • <TR><TH CLASS="TITLE"> • What's New at JspNews.com</TABLE> • <P> • Here is a summary of our three • most recent news stories: • <OL> • <LI><jsp:include page="/WEB-INF/Item1.html" /> • <LI><jsp:include page="/WEB-INF/Item2.html" /> • <LI><jsp:include page="/WEB-INF/Item3.html" /> • </OL> • </BODY></HTML> jsp:include Example: A News Headline Page (Main Page)
  • 5. Page 5Classification: Restricted A News Headline Page: Result
  • 6. Page 6Classification: Restricted • Use jsp:include whenever possible – Changes to included page do not require any manual updates – Speed difference between jsp:include and the include directive (@include) is insignificant • The include directive (<%@ include …%>) has additional power, however – Main page • <%! int accessCount = 0; %> – Included page • <%@ include file="snippet.jsp" %> • <%= accessCount++ %> Which Should You Use?
  • 7. Page 7Classification: Restricted • Footer defined the accessCount field (instance variable) • • If main pages used accessCount, they would have to use @include – Otherwise accessCount would be undefined • In this example, the main page did not use accessCount – So why did we use @include? Understanding jsp:include vs. <%@ include … %>
  • 8. Page 8Classification: Restricted • Develop the applets with JDK 1.1 or even 1.02 (to support really old browsers). – Works with almost any browser – Uses the simple APPLET tag • Have users install version 1.4 of the Java Runtime Environment (JRE), then use JDK 1.4 for the applets. – Requires IE 5 or later or Netscape 6 or later – Uses the simple APPLET tag • Have users install any version of the Java 2 Plug-in, then use Java 2 for the applets. – Works with almost any browser – Uses ugly OBJECT and EMBED tags Options for Deploying Applets
  • 9. Page 9Classification: Restricted • Simple APPLET-like tag – Expands into the real OBJECT and EMBED tags • APPLET Tag – <APPLET CODE="MyApplet.class" WIDTH=475 HEIGHT=350> </APPLET> • Equivalent jsp:plugin – <jsp:plugin type="applet" code="MyApplet.class" width="475" height="350"> </jsp:plugin> • Reminder – JSP element and attribute names are case sensitive – All attribute values must be in single or double quotes – This is like XML but unlike HTML Using jsp:plugin
  • 10. Page 10Classification: Restricted <jsp:plugin type="applet" code="SomeApplet.class" width="300" height="200"> </jsp:plugin> jsp:plugin: Source Code
  • 11. Page 11Classification: Restricted • … • <BODY> • <CENTER> • <TABLE BORDER=5> • <TR><TH CLASS="TITLE"> • Using jsp:plugin</TABLE> • <P> • <jsp:plugin type="applet" • code="PluginApplet.class" • width="370" height="420"> • </jsp:plugin> • </CENTER></BODY></HTML> jsp:plugin: Example (JSP Code)
  • 12. Page 12Classification: Restricted • import javax.swing.*; • /** An applet that uses Swing and Java 2D • * and thus requires the Java Plug-in. • */ • public class PluginApplet extends JApplet { • public void init() { • WindowUtilities.setNativeLookAndFeel(); • setContentPane(new TextPanel()); • } • } • Where are .class files installed? jsp:plugin: Example (Java Code)
  • 13. Page 13Classification: Restricted • type – For applets, this should be "applet". Use "bean" to embed JavaBeans elements in Web pages. • code – Used identically to CODE attribute of APPLET, specifying the top-level applet class file • width, height – Used identically to WIDTH, HEIGHT in APPLET • codebase – Used identically to CODEBASE attribute of APPLET • align – Used identically to ALIGN in APPLET and IMG Attributes of the jsp:plugin Element
  • 14. Page 14Classification: Restricted • hspace, vspace – Used identically to HSPACE, VSPACE in APPLET, • archive – Used identically to ARCHIVE attribute of APPLET, specifying a JAR file from which classes and images should be loaded • name – Used identically to NAME attribute of APPLET, specifying a name to use for inter-applet communication or for identifying applet to scripting languages like JavaScript. • title – Used identically to rarely used TITLE attribute Attributes of the jsp:plugin Element (Cont.)
  • 15. Page 15Classification: Restricted • jreversion – Identifies version of the Java Runtime Environment (JRE) that is required. Default is 1.2. • iepluginurl – Designates a URL from which plug-in for Internet Explorer can be downloaded. Users who don’t already have the plug-in installed will be prompted to download it from this location. Default value will direct user to Sun site, but for intranet use you might want to direct user to a local copy. • nspluginurl – Designates a URL from which plug-in for Netscape can be downloaded. Default value will direct user to Sun site, but for intranet use you might want local copy. Attributes of the jsp:plugin Element (Cont.)
  • 16. Page 16Classification: Restricted • PARAM Tags – <APPLET CODE="MyApplet.class" WIDTH=475 HEIGHT=350> <PARAM NAME="PARAM1" VALUE="VALUE1"> <PARAM NAME="PARAM2" VALUE="VALUE2"> </APPLET> • Equivalent jsp:param – <jsp:plugin type="applet" code="MyApplet.class" width="475" height="350"> <jsp:params> <jsp:param name="PARAM1" value="VALUE1" /> <jsp:param name="PARAM2" value="VALUE2" /> </jsp:params> </jsp:plugin> The jsp:param and jsp:params Elements
  • 18. Page 18Classification: Restricted • Scripting elements calling servlet code directly • Scripting elements calling servlet code indirectly (by means of utility classes) • Beans • Servlet/JSP combo (MVC) • MVC with JSP expression language • Custom tags Simple Application Complex Application Uses of JSP Constructs
  • 19. Page 19Classification: Restricted • Java classes that follow certain conventions – Must have a zero-argument (empty) constructor • You can satisfy this requirement either by explicitly defining such a constructor or by omitting all constructors – Should have no public instance variables (fields) • I hope you already follow this practice and use accessor methods instead of allowing direct access to fields – Persistent values should be accessed through methods called getXxx and setXxx • If class has method getTitle that returns a String, class is said to have a String property named title • Boolean properties use isXxx instead of getXxx – For more on beans, see http://java.sun.com/beans/docs/ Background: What Are Beans?
  • 20. Page 20Classification: Restricted • jsp:useBean – In the simplest case, this element builds a new bean. It is normally used as follows: • <jsp:useBean id="beanName" class="package.Class" /> • jsp:getProperty – This element reads and outputs the value of a bean property. It is used as follows: • <jsp:getProperty name="beanName" property="propertyName" /> • jsp:setProperty – This element modifies a bean property (i.e., calls a method of the form setXxx). It is normally used as follows: • <jsp:setProperty name="beanName" • property="propertyName" • value="propertyValue" /> Using Beans: Basic Tasks
  • 21. Page 21Classification: Restricted • Format – <jsp:useBean id="name" class="package.Class" /> • Purpose – Allow instantiation of Java classes without explicit Java programming (XML-compatible syntax) • Notes – Simple interpretation: <jsp:useBean id="book1" class="coreservlets.Book" /> can be thought of as equivalent to the scriptlet <% coreservlets.Book book1 = new coreservlets.Book(); %> – But jsp:useBean has two additional advantages: • It is easier to derive object values from request parameters • It is easier to share objects among pages or servlets Building Beans: jsp:useBean
  • 22. Page 22Classification: Restricted • Format – <jsp:getProperty name="name" property="property" /> • Purpose – Allow access to bean properties (i.e., calls to getXxx methods) without explicit Java programming • Notes – <jsp:getProperty name="book1" property="title" /> is equivalent to the following JSP expression <%= book1.getTitle() %> Accessing Bean Properties: jsp:getProperty
  • 23. Page 23Classification: Restricted • Format – <jsp:setProperty name="name" property="property" value="value" /> • Purpose – Allow setting of bean properties (i.e., calls to setXxx methods) without explicit Java programming • Notes – <jsp:setProperty name="book1" property="title" value="Core Servlets and JavaServer Pages" /> is equivalent to the following scriptlet <% book1.setTitle("Core Servlets and JavaServer Pages"); %> Setting Simple Bean Properties: jsp:setProperty
  • 24. Page 24Classification: Restricted • package coreservlets; • public class StringBean { • private String message = "No message specified"; • public String getMessage() { • return(message); • } • public void setMessage(String message) { • this.message = message; • } • } • Beans installed in normal Java directory – …/WEB-INF/classes/directoryMatchingPackageName • Beans (and utility classes) must always be in packages! Example: StringBean
  • 25. Page 25Classification: Restricted • <jsp:useBean id="stringBean" • class="coreservlets.StringBean" /> • <OL><LI>Initial value (from jsp:getProperty): • <I><jsp:getProperty name="stringBean" • property="message" /></I> • <LI>Initial value (from JSP expression): • <I><%= stringBean.getMessage() %></I> • <LI><jsp:setProperty name="stringBean" • property="message" • value="Best string bean: Fortex" /> Value after setting property with jsp:setProperty: • <I><jsp:getProperty name="stringBean" • property="message" /></I> • <LI><% stringBean.setMessage • ("My favorite: Kentucky Wonder"); %> • Value after setting property with scriptlet: • <I><%= stringBean.getMessage() %></I> • </OL> JSP Page That Uses StringBean (Code)
  • 26. Page 26Classification: Restricted • You can use the scope attribute to specify additional places where bean is stored – Still also bound to local variable in _jspService – <jsp:useBean id="…" class="…" scope="…" /> • Lets multiple servlets or JSP pages share data • Also permits conditional bean creation – Creates new object only if it can't find existing one Sharing Beans
  • 27. Page 27Classification: Restricted • page (<jsp:useBean … scope="page"/> or <jsp:useBean…>) – Default value. Bean object should be placed in the PageContext object for the duration of the current request. Lets methods in same servlet access bean • application (<jsp:useBean … scope="application"/>) – Bean will be stored in ServletContext (available through the application variable or by call to getServletContext()). ServletContext is shared by all servlets in the same Web application (or all servlets on server if no explicit Web applications are defined). Values of the scope Attribute
  • 28. Page 28Classification: Restricted • session (<jsp:useBean … scope="session"/>) – Bean will be stored in the HttpSession object associated with the current request, where it can be accessed from regular servlet code with getAttribute and setAttribute, as with normal session objects. • request (<jsp:useBean … scope="request"/>) – Bean object should be placed in the ServletRequest object for the duration of the current request, where it is available by means of getAttribute Values of the scope Attribute
  • 29. Page 29Classification: Restricted • Bean conditionally created – jsp:useBean results in new bean being instantiated only if no bean with same id and scope can be found. – If a bean with same id and scope is found, the preexisting bean is simply bound to variable referenced by id. • Bean properties conditionally set – <jsp:useBean ... /> replaced by <jsp:useBean ...>statements</jsp:useBean> – The statements (jsp:setProperty elements) are executed only if a new bean is created, not if an existing bean is found. Conditional Bean Operations
  • 30. Page 30Classification: Restricted • Using unshared (page-scoped) beans. • Sharing request-scoped beans. • Sharing session-scoped beans. • Sharing application-scoped (i.e., ServletContext-scoped) beans. Note: – Use different names (i.e., id in jsp:useBean) for different beans Sharing Beans in Four Different Ways
  • 31. Page 31Classification: Restricted • … • <BODY> • <H1>Baked Bean Values: session-based Sharing</H1> • <jsp:useBean id="sessionBean" • class="coreservlets.BakedBean" • scope="session" /> • <jsp:setProperty name="sessionBean" • property="*" /> • <H2>Bean level: • <jsp:getProperty name="sessionBean" • property="level" /> • </H2> • <H2>Dish bean goes with: • <jsp:getProperty name="sessionBean" • property="goesWith" /> • </H2></BODY></HTML> Session-Based Sharing: Code
  • 32. Page 32Classification: Restricted • <BODY> • <H1>Baked Bean Values: • application-based Sharing</H1> • <jsp:useBean id="applicationBean" • class="coreservlets.BakedBean" • scope="application" /> • <jsp:setProperty name="applicationBean" • property="*" /> • <H2>Bean level: • <jsp:getProperty name="applicationBean" • property="level" /> • </H2> • <H2>Dish bean goes with: • <jsp:getProperty name="applicationBean" • property="goesWith"/> • </H2></BODY></HTML> Application-Based Sharing: Code
  • 33. Page 33Classification: Restricted Application-Based Sharing: Result (Later Request -- Same Client)
  • 34. Page 34Classification: Restricted Client Browser 1. Request 4. Response JSP 2. Create JavaBeans 3. Retrieve data DB Application Server The JSP Model 1 Architecture
  • 35. Page 35Classification: Restricted JavaBeans Client Browser 1. Request 6. Response Servlet (Controller 2. Create JavaBeans DB Application Server JSP (View) 4.Invoke JSP 5.Use JavaBeans 3.Retrieve data The JSP Model 2 Architecture