Web Tier Programming
Web Tier Programming
In
Depth Session:
Carol McDonald
Technology Evangelist at Sun Microsystems
Develop and deliver Code Camps for
J2EE, Java™ performance tuning, J2ME and
web services technologies for Sun
Microsystems
Speak around the world concerning
Sun™ technologies
Tri-lingual: English, French, German
Presentation
Agenda
Web Tier
Programming
and Web
Services
Servlets JDBC / WebRowSet
Browser on JSP
…desktop JSTL
…cell phone JSF DB
Portlets
Presentation
Audio on phone Content
Channels
JAX-RPC
SVG on desktop
J2EE
JAX-RPC
Server-Side
Technology
Java™
Servlets
Early JSP™
Struts and MVC
Sun™ One Application Framework
JSTL specification 1.0, JSP™
specification 2.0
<c:forEach var="item"
values="${cart}">
JavaServer™ Faces (JSF) specification
1.0
How
They
Fit Together
JSF Struts
JSTL
S1AF
JSP™
java bean, expression
lang.,custom tag lib
Servlet
session tracking, filtering, listener
Sun ONE
Application
Framework
(JATO)
A J2EE application framework
Primarily focused on Web tier
Leverage Sun Java Center J2EE Patterns
Provide a robust, scalable application
foundation
Reusable components
Full-featured event model and high-level tag library
Leveraged through Sun ONE Studio Wizard-
based visual interfaces
Sun ONE
Application
Framework
JSF Struts
JSTL
S1AF
JSP™
java bean, expression
lang.,custom tag lib
Servlet
session tracking, filtering, listener
Agenda
Struts
Architecture
Model
View
Controller
Development Steps
Struts and J2EE design pattern demo
Model
View Controller
Separate control
From model data
Request Servlet From presentation of view
Controller
Forward Business
Response Event Event
Model
Business
JSP Bean
View Model
Data
MVC Pattern
Jakarta Struts
http
http
request struts-config.xml response
ActionForm TagLibs
Action
ActionServlet Action JSP
Action JSP
Action
Controller View
ActionForm
ActionForm STRUTS
ActionForm Model
Your code
Struts-config.xml
........
Action
........
........
Struts: MVC Pattern
Implemented
Controller
Request Action
Action
Servlet
Mapping Action Model
(xml)
Client
Uses
Response
Action Action
JSP View
Forward Form
Model Data
View
Struts Components
and Feature
Struts
Architecture
Model
View
Controller
Development Steps
Struts and J2EE design pattern demo
Example: Account
Services Form
Architecture
http
http
request struts-config.xml response
ActionForm TagLibs
Action
ActionServlet Action JSP
Action JSP
Action
Controller View
ActionForm
ActionForm STRUTS
ActionForm Model
Your code
Controller
http
http
request struts-config.xml response
ActionForm TagLibs
Action
ActionServlet Action JSP
Action JSP
Action
Controller View
ActionForm
ActionForm STRUTS
ActionForm Model
Your code
Action Form
ActionForm
Binding by name JSP
Tag Attribute and the
Address
JavaBean Property-
Name CustomerForm
name
address
validate()
<html:form action="updateCustomer">
<TABLE border="0">
<TBODY>
<TR>
<TD>Name</TD> Binding between the
<TD><html:text property="name" /></TD> Action
</TR>
...
updateCustomer
</TBODY> and the class
</TABLE> CustomerForm is in
<html:submit value="ok" />
</html:form> ViewCustomer.jsp struts-config.xml
Struts-config.xml: ActionMapping
1 <form-beans>
2 <form-bean name="customerForm"
3 type="packagex.CustomerForm"/>
4 </form-beans>
5
6 <action-mappings>
7 <action path="/updateCustomer"
8 type="UpdateCustomerAction"
9 name="customerForm"
10 scope="request"
11 input="/logon.jsp">
12 </action>
13 <forward name="success" path="/ViewCustomer.jsp"/>
14 <forward name="error" path="/error.jsp" />
15 </action-mappings>
Model: ActionForm
01 import org.apache.struts.action.ActionForm;
02 import org.apache.struts.action.ActionMapping;
03 import org.apache.struts.upload.FormFile;
04 public class CustomerForm extends ActionForm {
05 protected String firstName;
06 protected String lastName;
07 public void setFirstName(String firstName) {
08 this.firstName = firstName;
09 }
10 public void setLastName(String lastName) { ...}
11 //There would also be getters for these properties
12 }
Architecture
http
http
request struts-config.xml response
ActionForm TagLibs
Action
ActionServlet Action JSP
Action JSP
Action
Controller View
ActionForm
ActionForm STRUTS
ActionForm Model
Your code
is
What an Action
Class?
1 <action-mappings>
2 <action path="/updateCustomer"
3 type="CustomerAction"
4 name="customerForm"
5 scope="request"
6 input="/logon.jsp">
7 </action>
8 <forward name="success" path="/ViewCustomer.jsp"/>
9 <forward name="error" path="/error.jsp" />
10 </action-mappings>
Struts Flow
Struts
Architecture
Model
View
Controller
Development Steps
Struts and J2EE design pattern demo
Architecture
http
http
request struts-config.xml response
ActionForm TagLibs
Action
ActionServlet Action JSP
Action JSP
Action
Controller View
ActionForm
ActionForm STRUTS
ActionForm Model
Your code
Model Components
JDO
Struts
Architecture
Model
View
Controller
Development Steps
Struts and J2EE design pattern demo
Architecture
http
http
request struts-config.xml response
ActionForm TagLibs
Action
ActionServlet Action JSP
Action JSP
Action
Controller View
ActionForm
ActionForm STRUTS
ActionForm Model
Your code
View
Struts
Architecture
Model
View
Controller
Development Steps
Struts and J2EE design pattern demo
Develop Struts
Application in
!
"!
#!
Modify web.xml:
"(
$
""
"-
$
&'
'
". ?&'; 1
'
"7
"+
$
Struts-config.xml: <action-mappings>
"
- /
.
7
+ &'!1
'
= ?&'5!1
'
>
&'
'
* &'5!1
:'
, &'A'
"( !&''
"" 8!
&'' &'B81
:'
"- 8!
&'' &':'
".
"7
"+
"=
">
Write
4.
ActionForm
classes
Extend org.apache.struts.action.ActionForm
class
Decide set of properties that reflect the
input form
Write getter and setter methods for each
property
Write validate() method if input validation
is desired
Model: ActionForm
01 import org.apache.struts.action.ActionForm;
02 import org.apache.struts.action.ActionMapping;
03 import org.apache.struts.upload.FormFile;
04 public class CustomerForm extends ActionForm {
05 protected String firstName;
06 protected String lastName;
07 public void setFirstName(String firstName) {
08 this.firstName = firstName;
09 }
10 public void setLastName(String lastName) { ...}
11 //There would also be getters for these properties
12 }
5. Write Action
Classes
ApplicationResource.properties
Struts
Architecture
Model
View
Controller
Development Steps
Struts and J2EE design pattern demo
Example: Account
Services Form
Struts and Core
J2EE Patterns
Controller Model
Service
Request Action Interface
Action
Mapping Action
Servlet
(xml)
Business Business
Delegate Service
Client
Uses
Service
Locator
Response
Action Action
JSP View
Forward Form
! "#
$
% !"#
!"#
' ( "#
' ( ( ) !"#
Agenda
JavaServer™ Faces
Architecture overview
Key concepts
Development steps
Car demo
Is
What
JavaServer™ Faces
Technology?
Server
Request
UI
Client (events)
Response
(markup)
JSF
Architecture
Server
JSF Page
HTML
HTML
RenderKit
Front App
ctrl JSF Page Backend
WML
RenderKit
WML
JavaServer
Faces
c1 A c1 A A
c2 B c2 B B
c3 C c3 C C
Client Server
Motivation
for JSF
JSF App
JSF App
JSF Tags
JSP
(1.2 or later) JSF API
Developer Implementor
Page Tool
Author Provider
JavaServer™ Faces
Architecture overview
Key concepts
Development steps
Car demo
Important Basic
Capabilities
The web.xml Configuration File
!
"#
$
!
%
!
&
%
!
$
!
' !
$
JavaServer™ Faces
Architecture overview
Key concepts
Development steps
Car demo
in
Steps
Development Process
Managed Bean
The model (M) in MVC
A regular JavaBeans with get set methods for all
attributes or properties
May contain action methods and
event handlers
Use to hold data from a UI (JSP)
Creation and lifetime is managed
by JSF runtime
Application, session, request, none
JSF keeps the bean’s data in sync with the UI
Login.jsp
Faces-config.xml:
(&
$%1
()
$%$
(* %
&
(=
$%1
(3
$%$
(5 '%
&
(6
$%1
(8
$%$
(9 (
&(
$%1
&&
$%1
Managed Bean
Creation Facility
+, ! %-4!
4
!:-4%
& ) 4
$
$%1
%
&
$%1
!1
%
&
%
&
$%1
!1 % * ) ?@ 0
$%1
!1 $ * ) ?@ 0
>
%
&
$%1
3.
Create JSF
Pages
Login.jsp
(& !
() !
((
(*
!
"# +)
+,! !
!-./0$#!%1
12.
+)
((
(3 +, %-4$4
-4$4
(5
(6 7
,
(8 +, ! %-4!
4
(9
!:-4$#
!
4
1
&(
&& ;
<%,
&) +, ! %-4
<%4
&*
!:-4$#
<%4
1
&=
&3 +,
Components
UI
A BA +C#A
:
% #!A A
#!A
1A %A
BA
Components
UI
UIComponent/UIComponentBase
JavaBean™ base class with standard behavior
html_basic
Defines tags for representing common HTML user
interface components
jsf_core
Defines other JSF related tags
Independent of any rendering technology
JSP page needs to declare them
<%@ taglib uri="http://java.sun.com/jsf/html/"
prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core/"
prefix="f" %>
Navigation
Model
Example
to-view
from-outcome
from-view
+
4.
Define Page
Navigation Rules:
Faces-config.xml
<h:command_button type=”submit” label=”Log On”
action=”success”/>
1: <navigation-rule>
1: <from-view-id>/login.jsp</from-tree-id>
2: <navigation-case>
3: <from-outcome>success</from-outcome>
4: <to-view-id>/menu.jsp</to-tree-id>
5: </navigation-case>
6: <navigation-case>
7: <from-outcome>error</from-outcome>
8: <to-view-id>/error.jsp</to-tree-id>
9: </navigation-case>
10: . . .
11: </navigation-rule>
12:
13:
Request
Processing Lifecycle
Response Response
Complete Complete
Faces
Request Reconstitute Apply
Component Request Process Process Process
Tree Values Events Validations Events
Render Response
Response Response
Complete Complete
Faces
Response Render Process Invoke Process Update
Responder Events Application Events Model
Values
Conversion Errors /
Render Response
Validation / Conversion
Errors / Render Response
JSF
Lifecycle
!
Response Response
Complete Complete
Faces
Request Reconstitute Apply
Component Request Process Process Process
Tree Values Events Validations Events
Render Response
Response Response
Complete Complete
Faces
Response Render Process Invoke Process Update
Responder Events Application Events Model
Values
Conversion Errors /
Render Response
Validation / Conversion
Errors / Render Response
Request
Lifecycle
Processing
1:
2: <!-- Converter -->
3: <converter>
4: <description>CreditCard Converter</description>
5: <converter-id>creditcard</converter-id>
6: <converter-class>cardemo.CreditCardConverter
7: </converter-class>
8: </converter>
Converters and
Validators
Response Response
Complete Complete
Faces
Request Reconstitute Apply
Component Request Process Process Process
Tree Values Events Validations Events
Render Response
Response Response
Complete Complete
Faces
Response Render Process Invoke Process Update
Responder Events Application Events Model
Values
Conversion Errors /
Render Response
Validation / Conversion
Errors / Render Response
Request
Lifecycle
Processing
<h:input_text id="email"
valueRef="subscriberBean.email">
Request
Lifecycle
Processing
Response Response
Complete Complete
Faces
Request Reconstitute Apply
Component Request Process Process Process
Tree Values Events Validations Events
Render Response
Response Response
Complete Complete
Faces
Response Render Process Invoke Process Update
Responder Events Application Events Model
Values
Conversion Errors /
Render Response
Validation / Conversion
Errors / Render Response
Request Processing Lifecycle
Response Response
Complete Complete
Faces
Request Reconstitute Apply
Component Request Process Process Process
Tree Values Events Validations Events
Render Response
Response Response
Complete Complete
Faces
Response Render Process Invoke Process Update
Responder Events Application Events Model
Values
Conversion Errors /
Render Response
Validation / Conversion
Errors / Render Response
Request Processing Lifecycle
Button
Link
What’s in Faces
Application
It’s a WAR:
Your.jpg
your.jsp
WEB-INF/web.xml
WEB-INF/faces-config.xml
WEB-INF/classes/your.class
WEB-INF/lib/your.jar
(if not running in Java WSDP 1.3)
WEB-INF/lib/jsf-api.jar
WEB-INF/lib/jsf-ri.jar
WEB-INF/lib/jstl.jar
WEB-INF/lib/jsf-el.jar
WEB-INF/lib/standard.jar
WEB-INF/lib/commons-beanutils.jar
WEB-INF/lib/commons-digester.jar
WEB-INF/lib/commons-collections.jar
WEB-INF/lib/commons-logging.jar
Example:
web.xml in JSF beta
: 1)C<6
: 8$
!
" !
/
#
JavaWeb
Services
JavaServer™ Faces
Architecture overview
Key concepts
Development steps
Car demo
Car
Demo Page
Flow
More.jsp
Finish
Storefront.jsp Customer.jsp Thanks.jsp
Change
options Give personal
information
Buy.jsp
Combining SVG
and
JavaServer™
Faces Technologies
"# $%&'
!
"# ()'
!
*
+
Struts
and JavaServer Faces™
(cont.)
(cont.)
Validation
Navigation
Struts
and JavaServer
Faces™
(cont.)
(cont.)
Portals
What are
Portals?
@E 1! !
@ 1
?
F @ 0;
? @$
8!
0
4
F @
F 1!
@; 1!
D!
What are Portlets?
Web Components
Generate content as markup “fragments”
aggregated in a portal page
managed by a portlet container
Fragment generated may vary depending on
user profile and configuration
Page personalization
Like specialized servlets aware of portal
context
JSR 168 Portlet – Standardization
JSR 168
Standardization
Write Once
Custom
Portal
Portlet Interface
Deploy to Many
JSR 168
Compliant
Portals
Aggregation
Technologies —
Portlets
Java™ Portlets 1.0, JSR-168 brand-
new specification
Features include:
Web components, managed by a
portlet container
Aggregation, layout management
Page personalization
JSR 168 Portlet – Standardization
Portal-Common Terminology
4
User Agent 4
Browser 8; 4
Phone,
Aggregation
PDA Personalization
Security 4
G168 Define?
What
Does JSR
Portlet API
Portlet requests
Preferences
User information
Security
Deployment packaging
Portlet Container
Extension of servlet container
Contract between component
and container
G168
What Does
JSR
Define?
Portlet API
Portlet requests
Preferences
User information
Security
Deployment packaging
Portlet Container
Extension of servlet container
Contract between component and
container
Comparing
with Servlets
Portlets
Window
Security Config
Remote Portlet
Execution
module Portlet Enterprise
Portlet
Dispatcher Information
Aggregation Systems
Engine
Portlet
User
Configuration/ Persistent
Personalization Data Store
Engine
Portlet Life Cycle
0?! $? destroy()
Notifies the destruction
of portlet by container.
Should free up resources in
this method.
Portlet Interface
}
Two
Types
Requests
of Portlet
4 4
5 4 1
0
1
D!
!
D!
2 A
? $ !
!
D! 1
!
J
J 1
)! 4 4
GenericPortlet class
Portlet Modes
Render()--like service() in servlets, calls
specified render methods:
doView() Portlet renders markup fragment in
this mode
doEdit() Used to change per-user settings to
customize rendering
doHelp() Used to display help information
DoView()
DoEdit()
IBM
X
ProcessAction()
IBM
IBM
X
!"
PortletPreferences
1:
2: public class HelloWorldPortlet extends GenericPortlet {
3:
4: public void doView( RenderRequest request,
5: RenderResponse response)
6: throws PortletException {
7:
8: response.setContentType(“text/html”);
9: PrintWriter writer = response.getWriter();
10: Writer.write("Hello World");
11: }
12: }
Portlet Request
Dispatcher
1: <portlet-app>
2: <portlet>
3: <portlet-name>HelloWorldPortlet</portlet-name>
4: <portlet-class>
5: Custom.HelloWorldPortlet
6: </portlet-class>
7: <supports>
8: <mime-type>text/html</mime-type>
9: <portlet-mode>VIEW</portlet-mode>
10: </supports>
11: <portlet-preference>
12: <preference>
13: <name>locale </name>
14: <value> USA </value>
15: </preference>
16: </portlet-preference>
17: </portlet>
18: </portlet-app>
Portlet Builder
2.0 New Features
*
Avoid
String Concatenation
"+=" (all)
Right Way!!!
01 Private DataSource ds = null;
02 public void doGet(...)
03 {...
04 Connection conn=ds.getConnection("db2admin", "db2admin");
05 PrepareStatement pStmt = conn.prepareStatement("...");
06 ResultSet rs = pStmt.executeQuery(); ...
Overhead
Avoid
of
Acquiring and Closing
connections (all)
Make JDBC connections by obtaining
javax.sql.DataSource in JDBC 2.0
DataSource.getConnection()
Finished (servlets)
<%@page session="false"%>
Avoid
Beans.instanitiate()
to
,
-
+
Summary
Struts
http://jakarta.apache.org/struts
Q&A
Carol McDonald
Staff Engineer/Technology Evangelist
[email protected]