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

20jsp
20jsp20jsp
20jsp
Adil Jafri
 
Jsp
JspJsp
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
kamal kotecha
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
Shah Nawaz Bhurt
 
JSP
JSPJSP
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
Vipin Yadav
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
Khan Mac-arther
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
BG Java EE Course
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
Sher Singh Bardhan
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
Michael Peacock
 
Getting started with laravel
Getting started with laravelGetting started with laravel
Getting started with laravel
Advance Idea Infotech
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
ShahDhruv21
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
Hitesh-Java
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Aaqib Hussain
 
Jsp slides
Jsp slidesJsp slides
Jsp slides
Kumaran K
 
Spring 3 - Der dritte Frühling
Spring 3 - Der dritte FrühlingSpring 3 - Der dritte Frühling
Spring 3 - Der dritte Frühling
Thorsten Kamann
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
Oscar Merida
 
CQ Provisionning & Authoring
CQ Provisionning & AuthoringCQ Provisionning & Authoring
CQ Provisionning & Authoring
Gabriel Walt
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
Kalpana T
 
Jsp Slides
Jsp SlidesJsp Slides
Jsp Slides
DSKUMAR G
 

What's hot (20)

20jsp
20jsp20jsp
20jsp
 
Jsp
JspJsp
Jsp
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
JSP
JSPJSP
JSP
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
Jsp(java server pages)
Jsp(java server pages)Jsp(java server pages)
Jsp(java server pages)
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
API Development with Laravel
API Development with LaravelAPI Development with Laravel
API Development with Laravel
 
Getting started with laravel
Getting started with laravelGetting started with laravel
Getting started with laravel
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
 
Jsp slides
Jsp slidesJsp slides
Jsp slides
 
Spring 3 - Der dritte Frühling
Spring 3 - Der dritte FrühlingSpring 3 - Der dritte Frühling
Spring 3 - Der dritte Frühling
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
CQ Provisionning & Authoring
CQ Provisionning & AuthoringCQ Provisionning & Authoring
CQ Provisionning & Authoring
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Jsp Slides
Jsp SlidesJsp Slides
Jsp Slides
 

Similar to JSP Part 2

13 java beans
13 java beans13 java beans
13 java beans
snopteck
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
WebStackAcademy
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
AnilKumar Etagowni
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
NishaRohit6
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
vishal choudhary
 
Java server pages
Java server pagesJava server pages
Java server pages
Tanmoy Barman
 
Jsp
JspJsp
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Intro To Sap Netweaver Java
Intro To Sap Netweaver JavaIntro To Sap Netweaver Java
Intro To Sap Netweaver Java
Leland Bartlett
 
14 mvc
14 mvc14 mvc
14 mvc
snopteck
 
Cis 274 intro
Cis 274   introCis 274   intro
Cis 274 intro
Aren Zomorodian
 
java beans
java beansjava beans
java beans
lapa
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
Coursejspservlets00
Coursejspservlets00Coursejspservlets00
Coursejspservlets00
Rajesh Moorjani
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
Rajesh Moorjani
 
Using java beans(ii)
Using java beans(ii)Using java beans(ii)
Using java beans(ii)
Ximentita Hernandez
 
SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
Boston 2011 OTN Developer Days - Java EE 6
Boston 2011 OTN Developer Days - Java EE 6Boston 2011 OTN Developer Days - Java EE 6
Boston 2011 OTN Developer Days - Java EE 6
Arun Gupta
 
Jsp advance part i
Jsp advance part iJsp advance part i
Jsp advance part i
sameersaxena90
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Ayes Chinmay
 

Similar to JSP Part 2 (20)

13 java beans
13 java beans13 java beans
13 java beans
 
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
Web Component Development Using Servlet & JSP Technologies (EE6) - Chapter 7 ...
 
Spring introduction
Spring introductionSpring introduction
Spring introduction
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
Jsp and jstl
Jsp and jstlJsp and jstl
Jsp and jstl
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Jsp
JspJsp
Jsp
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Intro To Sap Netweaver Java
Intro To Sap Netweaver JavaIntro To Sap Netweaver Java
Intro To Sap Netweaver Java
 
14 mvc
14 mvc14 mvc
14 mvc
 
Cis 274 intro
Cis 274   introCis 274   intro
Cis 274 intro
 
java beans
java beansjava beans
java beans
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 
Coursejspservlets00
Coursejspservlets00Coursejspservlets00
Coursejspservlets00
 
Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00Jeetrainers.com coursejspservlets00
Jeetrainers.com coursejspservlets00
 
Using java beans(ii)
Using java beans(ii)Using java beans(ii)
Using java beans(ii)
 
SCWCD : Java server pages CHAP : 9
SCWCD : Java server pages  CHAP : 9SCWCD : Java server pages  CHAP : 9
SCWCD : Java server pages CHAP : 9
 
Boston 2011 OTN Developer Days - Java EE 6
Boston 2011 OTN Developer Days - Java EE 6Boston 2011 OTN Developer Days - Java EE 6
Boston 2011 OTN Developer Days - Java EE 6
 
Jsp advance part i
Jsp advance part iJsp advance part i
Jsp advance part i
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 

More from DeeptiJava

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
DeeptiJava
 
Java Generics
Java GenericsJava Generics
Java Generics
DeeptiJava
 
Java Collection
Java CollectionJava Collection
Java Collection
DeeptiJava
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
DeeptiJava
 
Java OOPs
Java OOPs Java OOPs
Java OOPs
DeeptiJava
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
DeeptiJava
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
DeeptiJava
 
Java Thread
Java ThreadJava Thread
Java Thread
DeeptiJava
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
DeeptiJava
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
DeeptiJava
 
Java I/O
Java I/OJava I/O
Java I/O
DeeptiJava
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
DeeptiJava
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
DeeptiJava
 

More from DeeptiJava (13)

Generating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status CodesGenerating the Server Response: HTTP Status Codes
Generating the Server Response: HTTP Status Codes
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java Collection
Java CollectionJava Collection
Java Collection
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
 
Java OOPs
Java OOPs Java OOPs
Java OOPs
 
Java Access Specifier
Java Access SpecifierJava Access Specifier
Java Access Specifier
 
Java JDBC
Java JDBCJava JDBC
Java JDBC
 
Java Thread
Java ThreadJava Thread
Java Thread
 
Java Inner Class
Java Inner ClassJava Inner Class
Java Inner Class
 
JSP Part 1
JSP Part 1JSP Part 1
JSP Part 1
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Hibernate Basics
Java Hibernate BasicsJava Hibernate Basics
Java Hibernate Basics
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 

Recently uploaded

GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
SOFTTECHHUB
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 

Recently uploaded (20)

GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
Goodbye Windows 11: Make Way for Nitrux Linux 3.5.0!
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 

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