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

PPTX
Java Server Pages
PPTX
SFDC UI - Advanced Visualforce
PPTX
Jsp (java server page)
PPTX
Jsp elements
PPTX
PPTX
Java Server Pages(jsp)
PPS
Jsp element
Java Server Pages
SFDC UI - Advanced Visualforce
Jsp (java server page)
Jsp elements
Java Server Pages(jsp)
Jsp element

What's hot (20)

PDF
20jsp
PDF
PPS
Jsp chapter 1
PPTX
Java Server Pages
PPT
PPTX
JSP - Java Server Page
PPT
Jsp(java server pages)
PPT
Java Server Pages
PPTX
Jsp presentation
PPTX
API Development with Laravel
PPTX
Getting started with laravel
PPTX
JSP Directives
PPSX
JSP - Part 2 (Final)
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
PPT
Jsp slides
PDF
Spring 3 - Der dritte Frühling
PDF
Staying Sane with Drupal NEPHP
PPTX
CQ Provisionning & Authoring
PPTX
JAVA SERVER PAGES
PPT
Jsp Slides
20jsp
Jsp chapter 1
Java Server Pages
JSP - Java Server Page
Jsp(java server pages)
Java Server Pages
Jsp presentation
API Development with Laravel
Getting started with laravel
JSP Directives
JSP - Part 2 (Final)
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Jsp slides
Spring 3 - Der dritte Frühling
Staying Sane with Drupal NEPHP
CQ Provisionning & Authoring
JAVA SERVER PAGES
Jsp Slides
Ad

Similar to JSP Part 2 (20)

PDF
13 java beans
PDF
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
PPTX
JSP APP DEVLOPMENT.pptx Related to Android App Development
PPT
Spring introduction
PPTX
JSP.pptx
PPTX
Jsp and jstl
PPSX
Java server pages
PDF
Intro To Sap Netweaver Java
PDF
14 mvc
PPTX
Cis 274 intro
PDF
java beans
PDF
Jeetrainers.com coursejspservlets00
PDF
Coursejspservlets00
PPTX
jsp unit 3byudoue8euwuuutrttttyyii90oigyu7
PDF
Using java beans(ii)
PPTX
JSP_Complete_Guide_With_Step_By_Step_solution
PPTX
SCWCD : Java server pages CHAP : 9
13 java beans
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
JSP APP DEVLOPMENT.pptx Related to Android App Development
Spring introduction
JSP.pptx
Jsp and jstl
Java server pages
Intro To Sap Netweaver Java
14 mvc
Cis 274 intro
java beans
Jeetrainers.com coursejspservlets00
Coursejspservlets00
jsp unit 3byudoue8euwuuutrttttyyii90oigyu7
Using java beans(ii)
JSP_Complete_Guide_With_Step_By_Step_solution
SCWCD : Java server pages CHAP : 9
Ad

More from DeeptiJava (13)

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

Recently uploaded (20)

PDF
Ebook - The Future of AI A Comprehensive Guide.pdf
PPT
Storage Area Network Best Practices from HP
PPTX
Report in SIP_Distance_Learning_Technology_Impact.pptx
PDF
Addressing the challenges of harmonizing law and artificial intelligence tech...
PPTX
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
PDF
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
PDF
Optimizing bioinformatics applications: a novel approach with human protein d...
PPTX
Blending method and technology for hydrogen.pptx
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
Technical Debt in the AI Coding Era - By Antonio Bianco
PDF
Ericsson 5G Feature,KPIs Analysis_ Overview, Dependencies & Recommendations (...
PPTX
How to Convert Tickets Into Sales Opportunity in Odoo 18
PDF
Chapter 1: computer maintenance and troubleshooting
PPTX
Strategic Picks — Prioritising the Right Agentic Use Cases [2/6]
PDF
Decision Optimization - From Theory to Practice
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
PDF
EIS-Webinar-Regulated-Industries-2025-08.pdf
PDF
Advancements in abstractive text summarization: a deep learning approach
PDF
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
Ebook - The Future of AI A Comprehensive Guide.pdf
Storage Area Network Best Practices from HP
Report in SIP_Distance_Learning_Technology_Impact.pptx
Addressing the challenges of harmonizing law and artificial intelligence tech...
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
CCUS-as-the-Missing-Link-to-Net-Zero_AksCurious.pdf
Optimizing bioinformatics applications: a novel approach with human protein d...
Blending method and technology for hydrogen.pptx
Connector Corner: Transform Unstructured Documents with Agentic Automation
Technical Debt in the AI Coding Era - By Antonio Bianco
Ericsson 5G Feature,KPIs Analysis_ Overview, Dependencies & Recommendations (...
How to Convert Tickets Into Sales Opportunity in Odoo 18
Chapter 1: computer maintenance and troubleshooting
Strategic Picks — Prioritising the Right Agentic Use Cases [2/6]
Decision Optimization - From Theory to Practice
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
EIS-Webinar-Regulated-Industries-2025-08.pdf
Advancements in abstractive text summarization: a deep learning approach
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER

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