Java Server Programming For Professionals

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Table Of Contents

SECTION I: OVERVIEW OF JAVA ENTERPRISE EDITION 5


1. INTRODUCTION TO JAVA............................................................................................................ 1
HISTORY OF JAVA ........................................................................................................................... 1
BASIC JAVA ARCHITECTURE........................................................................................................ 2
The Java Programming Language .................................................................................................... 3
The Java Class File........................................................................................................................... 4
Java Runtime Environment .............................................................................................................. 5
The Java Virtual Machine ............................................................................................................ 5
The Java API ................................................................................................................................ 6
Java Platform.................................................................................................................................... 6
Java Platform, Micro Edition or Java ME .................................................................................... 7
Java Platform, Standard Edition or Java SE ................................................................................. 7
Java Platform, Enterprise Edition or Java EE............................................................................... 7
JAVA AVAILABILITY ...................................................................................................................... 8
WHAT CAN BE DONE WITH JAVA................................................................................................ 8
2. SYSTEM ARCHITECTURE .......................................................................................................... 10
SINGLE TIER ARCHITECTURE..................................................................................................... 11
TWO TIER ARCHITECTURE.......................................................................................................... 12
THREE TIER ARCHITECTURE...................................................................................................... 14
N TIER ARCHITECTURE................................................................................................................ 17
ENTERPRISE ARCHITECTURE..................................................................................................... 19
Challenges Of Enterprise Application Development ..................................................................... 20
3. INTRODUCTION TO JAVA ENTERPRISE EDITION 5 .......................................................... 21
WHAT IS JAVA ENTERPRISE EDITION ...................................................................................... 21
JAVA ENTERPRISE EDITION 5..................................................................................................... 22
Java EE 5 - Platform Overview ...................................................................................................... 23
Features Of Java EE 5 .................................................................................................................... 23
Platform Support ........................................................................................................................ 23
Java Server Faces 1.2 And Ajax Technologies .......................................................................... 24
The Developer's Productivity ..................................................................................................... 24
Supports Integrated Development Environment [IDE] .............................................................. 25
Using Annotations Instead Of Deployment Descriptors ............................................................ 25
Packaging Applications .............................................................................................................. 25
EJB Software Development Is Simplified.................................................................................. 27
Dependency Injection To Access Resources.............................................................................. 28
Web Application Design With Java Server Faces Technology .................................................. 29
Java Server Pages Standard Tag Library .................................................................................... 30
JAVA EE 5 - PLATFORM TECHNOLOGIES AT A GLANCE.................................................. 30

Java Server Programming For Professionals


Page viii

JAVA EE 5 TECHNOLOGIES......................................................................................................31
Java Servlet Technology.............................................................................................................31
Enterprise Java Beans Technology .............................................................................................31
Java Server Pages Technology ...................................................................................................32
Java Server Pages Standard Tag Library ....................................................................................32
Java Server Faces........................................................................................................................32
SUN JAVA SYSTEM APPLICATION SERVER PLATFORM EDITION 9.0................................33
Glassfish .........................................................................................................................................33
4. INSTALLATION OF JAVA ENTERPRISE EDITION 5 ............................................................35
DOWNLOADING JAVA ENTERPRISE EDITION 5 SDK.............................................................35
INSTALLING JAVA ENTERPRISE EDITION 5 SDK....................................................................37
Installing Java Enterprise Edition 5 On Linux................................................................................37
Graphical Interface Based...........................................................................................................37
Console Based ............................................................................................................................42
Installing Java Enterprise Edition 5 On Windows ..........................................................................48
UNINSTALLING JAVA ENTERPRISE EDITION 5.......................................................................55
Uninstalling Java Enterprise Edition 5 on Linux ............................................................................55
Graphical Interface Based...........................................................................................................55
Console Based ............................................................................................................................58
Uninstalling Java Enterprise Edition 5 On Windows .....................................................................59
5. INTRODUCTION TO WEB APPLICATION ..............................................................................62
The Web Browser...........................................................................................................................64
The Web Server ..............................................................................................................................65
WEB APPLICATION ARCHITECTURE .........................................................................................65
COLLECTING DATA .......................................................................................................................66
SENDING THE WEB SERVER A REQUEST .................................................................................67
The GET Method............................................................................................................................67
The POST Method..........................................................................................................................68
Which Method Should Be Used .....................................................................................................69
EXECUTING SERVER SIDE SCRIPTS / PROGRAMS..................................................................69
RETURNING THE RESULTS TO A BROWSER............................................................................70
DEVELOPING SERVER APPLICATIONS .....................................................................................70
SERVER-SIDE TECHNOLOGIES ...................................................................................................70
Common Gateway Interface ...........................................................................................................71
Proprietary Web Server's APIs [ISAPI, NSAPI] ............................................................................72
Active Server Pages........................................................................................................................72
Java Servlets And Java Server Pages..............................................................................................73
WHY USE JAVA'S SERVER SIDE TECHNOLOGIES...................................................................74
Platform Independence ...................................................................................................................74
Efficiency .......................................................................................................................................74
Access To Enterprise Java API's ....................................................................................................74
Reusability ......................................................................................................................................75
Modularity ......................................................................................................................................75

Table Of Contents
Page ix

SECTION II: JAVA SERVLETS


6. INTRODUCTION TO JAVA SERVLETS .................................................................................... 76
WHAT IS A SERVLET ..................................................................................................................... 77
WHY SERVLETS.............................................................................................................................. 78
WHAT CAN SERVLETS DO ........................................................................................................... 79
SERVLETS V/S CGI ......................................................................................................................... 79
How Are Servlets Created .............................................................................................................. 80
Where Are Servlets Run................................................................................................................. 80
What Do Servlets Look Like.......................................................................................................... 80
How Are Servlets Run.................................................................................................................... 81
Standalone Servlet Engines ........................................................................................................ 82
Add-On Servlet Engines............................................................................................................. 82
ARCHITECTURE OF A SERVLET ................................................................................................. 83
7. BASICS OF SERVLET APPLICATION PROGRAMMING INTERFACE ............................. 84
Servlet Application Programming Interface [Servlet API]............................................................. 84
Class java.lang.Object ................................................................................................................ 84
GenericServlet Class .................................................................................................................. 87
HttpServlet Class........................................................................................................................ 87
Methods Required For Creating A Functional Http Servlet ................................................... 88
Common Servlet Methods...................................................................................................... 88
The Servlet Architecture................................................................................................................. 89
The Servlet Life Cycle ................................................................................................................... 92
Initialization ............................................................................................................................... 92
Why Not Use A Constructor Instead Of The init() Method ................................................... 93
The init() Method ................................................................................................................... 93
Runtime ...................................................................................................................................... 94
Destruction ................................................................................................................................. 94
A SIMPLE HELLO WORLD SERVLET.......................................................................................... 95
8. WORKING WITH SERVLETS ................................................................................................... 102
Getting Started.............................................................................................................................. 102
WHAT IS A WEB APPLICATION................................................................................................. 102
ORGANIZATION OF A WEB APPLICATION............................................................................. 103
CREATING A SERVLET FILE ...................................................................................................... 104
SampleServlet.java ................................................................................................................... 104
COMPILING AND BUILDING THE APPLICATION .................................................................. 105
The Asant Utility .......................................................................................................................... 106
build.xml .................................................................................................................................. 106
web.xml .................................................................................................................................... 110
Packaging Using The Asant Utility .......................................................................................... 112
Deploying The Application .......................................................................................................... 116
The Auto Deploy Method......................................................................................................... 116
How Is The war File Internally Deployed By The Application Server ................................ 119
Running The Deployed Web Application ............................................................................ 120

Java Server Programming For Professionals


Page x

The Application Server's Deploy Tool .....................................................................................121


The Application Server's Command Based Deploy Tool .....................................................127
Running The Deployed Web Application.............................................................................128
Viewing Deployed Web Applications ..................................................................................128
MODIFYING THE PACKAGED WEB MODULE ........................................................................129
Dynamic Reloading ......................................................................................................................129
UNDEPLOYING THE WEB MODULE .........................................................................................131
ADDING A USER INTERFACE TO THE WEB APPLICATION.................................................132
HANDS ON EXERCISES ...............................................................................................................140
9. WORKING WITH DATABASES ................................................................................................142
JDBC ................................................................................................................................................142
ADVANTAGES OF JDBC ..............................................................................................................143
JDBC ARCHITECTURE .................................................................................................................144
TYPES OF JDBC DRIVERS ...........................................................................................................146
HOW TO ACCESS DATABASE ....................................................................................................148
Configuring JDBC Driver.............................................................................................................148
Creating A Database Connection..................................................................................................149
Executing Queries.........................................................................................................................150
Standard Statement ...................................................................................................................150
Prepared Statement ...................................................................................................................151
Callable Statement ....................................................................................................................151
Processing The Results .................................................................................................................151
Closing The Database Connection................................................................................................152
USING DATABASE........................................................................................................................152
MySQL Connector/J 5.0...............................................................................................................153
Downloading MySQL Connector/J ..........................................................................................153
Installing And Configuring MySQL Connector/J JDBC Driver...............................................155
With ClassPath .....................................................................................................................156
Without ClassPath ................................................................................................................158
JDBC-ODBC Bridge Driver.........................................................................................................159
Configuring JDBC-ODBC Bridge Driver ................................................................................160
CONNECTING TO DATABASES..................................................................................................164
PERFORMING DATABASE OPERATIONS.................................................................................170
Creating A Table ..........................................................................................................................170
Inserting Data In A Table .............................................................................................................173
Updating Data In A Table.............................................................................................................176
Updating Data In MySQL Database .........................................................................................176
Deleting Data From A Table ........................................................................................................178
A Servlet Example........................................................................................................................180
HANDS ON EXERCISES ...............................................................................................................202
10. COOKIES .....................................................................................................................................204
MARKING OF A BROWSER BY A WEB SERVER.....................................................................204
THE CONTENTS OF A COOKIE...................................................................................................205
DIFFERENT TYPES OF COOKIES ...............................................................................................206
Permanent / Persistent Cookies.....................................................................................................206

Table Of Contents
Page xi

Session / Transient Cookies.......................................................................................................... 206


WHERE COOKIES ARE USED ..................................................................................................... 206
Where Are Cookies Placed........................................................................................................... 207
ADVANTAGES OF COOKIES ...................................................................................................... 208
CREATING COOKIES USING SERVLET.................................................................................... 208
Working With Cookies................................................................................................................. 208
Creating A Cookie Constructor ................................................................................................ 208
Setting A Cookie ...................................................................................................................... 210
Sending A Cookie To The Client ............................................................................................. 210
Creating A Sample Cookie....................................................................................................... 211
Retrieving Cookies ................................................................................................................... 213
Expiration Of A Cookie............................................................................................................ 216
Destroying A Cookie................................................................................................................ 217
Assigning Values To Cookie.................................................................................................... 218
AUTOMATIC LOGIN..................................................................................................................... 227
HANDS ON EXERCISES ............................................................................................................... 242
11. SESSIONS..................................................................................................................................... 244
SESSIONS ....................................................................................................................................... 245
LIFECYCLE OF HTTP SESSION .................................................................................................. 248
Session Management Rules.......................................................................................................... 248
SESSION TRACKING WITH SERVLET API ............................................................................... 249
Methods Of Session Tracking ...................................................................................................... 250
Cookies..................................................................................................................................... 250
URL Rewriting ......................................................................................................................... 251
Hidden Form Fields.................................................................................................................. 253
Working With Session.................................................................................................................. 254
Creating A Session ................................................................................................................... 255
Destroying Session ................................................................................................................... 258
Managing Sessions Lifetime .................................................................................................... 261
The getAttribute() Method ................................................................................................... 261
The getAttributeNames() Method ........................................................................................ 261
The setAttribute() Method.................................................................................................... 262
The removeAttribute() Method ............................................................................................ 262
The getId() Method .............................................................................................................. 263
The isNew() Method............................................................................................................. 263
The getCreationTime() Method............................................................................................ 263
The getLastAccessedTime() Method.................................................................................... 264
The getMaxInactiveInterval() Method ................................................................................. 264
The setMaxInactiveInterval() Method.................................................................................. 265
A SHOPPING CART....................................................................................................................... 268
The RunTime................................................................................................................................ 277
The Working ................................................................................................................................ 282
HANDS ON EXERCISES ............................................................................................................... 288
12. EVENT LISTENERS................................................................................................................... 292
OVERVIEW OF SERVLETCONTEXT INTERFACE................................................................... 294

Java Server Programming For Professionals


Page xii

GETTING STARTED WITH EVENT LISTENERS.......................................................................297


Declaring And Invoking Event Listeners .....................................................................................297
Working With Servlet Context Lifecycle Listeners......................................................................299
The contextInitialized() Method ...............................................................................................300
The contextDestroyed() Method...............................................................................................300
A Simple Servlet Context Lifecycle Listener Example ............................................................300
Working With Servlet Context Attribute Listeners ......................................................................308
The attributeAdded() Method ...................................................................................................308
The attributeRemoved() Method ..............................................................................................308
The attributeReplaced() Method...............................................................................................308
A Simple Servlet Context Attribute Listener Example.............................................................309
Working With HTTP Session Lifecycle Listeners .......................................................................323
The sessionCreated() Method ...................................................................................................323
The sessionDestroyed() Method ...............................................................................................324
A Simple Example Of Http Session Lifecycle Listener............................................................324
Working With HTTP Session Attribute Listeners ........................................................................333
The attributeAdded() Method ...................................................................................................333
The attributeRemoved() Method ..............................................................................................333
The attributeReplaced() Method...............................................................................................334
A Simple Example Of HttpSessionAttributeListener ...............................................................334
Working With The HttpSession Binding Listener........................................................................345
The valueBound() Method........................................................................................................346
The valueUnbound() Method....................................................................................................346
A Simple Example Of HttpSessionBindingListener.................................................................346
Working With The ServletRequest Listener.................................................................................352
The requestInitialized() Method ...............................................................................................352
The requestDestroyed() Method ...............................................................................................352
A Simple Example Of ServletRequestListener.........................................................................353
13. FILTERS .......................................................................................................................................360
IMPORTANCE OF FILTERS .........................................................................................................361
IMPLEMENTING A FILTER INTERFACE...................................................................................362
The Filter Interface .......................................................................................................................362
The init() Method......................................................................................................................362
The doFilter() Method ..............................................................................................................363
The destroy() Method ...............................................................................................................364
The FilterChain Interface..............................................................................................................364
The FilterConfig Interface ............................................................................................................365
DEPLOYMENT OF FILTERS ........................................................................................................365
The <filter> Element ....................................................................................................................365
The <filter-mapping> Element .....................................................................................................366
CREATING A SAMPLE FILTER ...................................................................................................367
SampleFilter.java..........................................................................................................................367
SampleServlet.java .......................................................................................................................371
web.xml ........................................................................................................................................372
Executing The SampleFilter .........................................................................................................373

Table Of Contents
Page xiii

CREATING A FILTER TO ADD URL AND TIME TO A DATABASE ...................................... 375


HANDS ON EXERCISES ............................................................................................................... 385
14. REQUEST DISPATCHING........................................................................................................ 388
The RequestDispatcher Interface.................................................................................................. 388
Obtaining An Object Implementing RequestDispatcher .......................................................... 389
The getRequestDispatcher() Method.................................................................................... 389
The getNamedDispatcher() Method ..................................................................................... 390
Methods Of RequestDispatcher................................................................................................ 391
The include() Method ........................................................................................................... 391
The forward() Method .......................................................................................................... 394
Request Attributes .................................................................................................................... 400
Using Filters With RequestDispatcher ......................................................................................... 406
HANDS ON EXERCISES ............................................................................................................... 407
15. RESOURCE BUNDLES.............................................................................................................. 411
WHAT IS A LOCALE..................................................................................................................... 412
RESOURCE BUNDLES.................................................................................................................. 417
CREATING RESOURCE BUNDLES............................................................................................. 419
ListResourceBundle Class............................................................................................................ 421
ListResourceBundle Class:........................................................................................................... 421
PropertyResourceBundle Class .................................................................................................... 430
HANDS ON EXERCISES ............................................................................................................... 441

SECTION III: JAVA SERVER PAGES


16. INTRODUCTION TO JAVA SERVER PAGES ...................................................................... 444
WHY USE JAVA SERVER PAGES............................................................................................... 445
Nobody Can Borrow The Code .................................................................................................... 445
Faster Loading Of Pages .............................................................................................................. 445
No Browser Compatibility Issues................................................................................................. 445
JSP Support .................................................................................................................................. 446
Compilation.................................................................................................................................. 446
JSP Elements In HTML / XML Pages ......................................................................................... 446
DISADVANTAGES OF JSP ........................................................................................................... 446
Attractive Java Code .................................................................................................................... 447
Java Code Required...................................................................................................................... 447
Simple Tasks Are Hard To Code.................................................................................................. 447
Difficult Looping In JSP .............................................................................................................. 448
Occupies A Lot Of Space ............................................................................................................. 448
JSP V/S SERVLETS ........................................................................................................................ 448
Why JSP If Java Already Has An API For Answering HTTP Requests ...................................... 449
JSP V/S ASP .................................................................................................................................... 449
Platform Independent ................................................................................................................... 449
Components.................................................................................................................................. 449
Compilation.................................................................................................................................. 449

Java Server Programming For Professionals


Page xiv

Extensible Tags.............................................................................................................................450
Platform Compatibility .................................................................................................................450
JSP ARCHITECTURE.....................................................................................................................450
The Page-Centric Approach .........................................................................................................450
Page View Architecture............................................................................................................451
Page View With Bean Architecture..........................................................................................451
The Dispatcher Approach .............................................................................................................452
LIFE CYCLE OF A JSP PAGE .......................................................................................................453
HOW DOES A JSP FUNCTION .....................................................................................................454
HOW DOES JSP EXECUTE ...........................................................................................................458
DEPLOYING AND EXECUTING JSP ...........................................................................................459
Modifying JSP page......................................................................................................................470
FEATURES OF JSP 2.1...................................................................................................................471
The Unified Expression Language [EL] .......................................................................................472
Better Alignment With JSF ..........................................................................................................472
Injection Annotations....................................................................................................................473
Blank Lines Removed...................................................................................................................473
Backward Compatibility...............................................................................................................475
17. GETTING STARTED WITH JAVA SERVER PAGES...........................................................477
COMMENTS ...................................................................................................................................477
TEMPLATE TEXT ..........................................................................................................................478
JSP ELEMENTS ..............................................................................................................................479
Directives......................................................................................................................................480
The page Directive....................................................................................................................481
The include Directive ...............................................................................................................485
The taglib Directive ..................................................................................................................487
Scripting Elements........................................................................................................................488
Declarations ..............................................................................................................................488
Scriptlets ...................................................................................................................................489
Expressions...............................................................................................................................490
A SAMPLE JSP PAGE ....................................................................................................................491
HANDS ON EXERCISES ...............................................................................................................498
18. JAVA SERVER PAGES DOCUMENT......................................................................................500
Directives......................................................................................................................................502
The page Directive....................................................................................................................502
The include Directive ...............................................................................................................504
The taglib Directive ..................................................................................................................505
Scripting Elements........................................................................................................................506
Declarations ..............................................................................................................................506
Scriptlets ...................................................................................................................................507
Expressions...............................................................................................................................507
A SAMPLE JSP DOCUMENT ........................................................................................................508
HANDS ON EXERCISES ...............................................................................................................523

Table Of Contents
Page xv

19. ACTION ELEMENTS OF JAVA SERVER PAGES ............................................................... 524


INCLUDING OTHER FILES.......................................................................................................... 524
FORWARDING JSP PAGE TO ANOTHER PAGE....................................................................... 529
PASSING PARAMETERS FOR OTHER ACTIONS..................................................................... 536
THE PLUG-INS............................................................................................................................... 537
Parameters Passed To The Applet ................................................................................................ 539
Rendering Alternative Output For Browser Without Applet Support .......................................... 545
LOADING A JAVA BEAN............................................................................................................. 549
Adding A Bean To A JSP Page.................................................................................................... 549
Setting The Properties Of The Bean............................................................................................. 552
Accessing The Properties Of The Beans ...................................................................................... 555
A REGISTRATION FORM USING ACTION ELEMENTS.......................................................... 555
HANDS ON EXERCISES ............................................................................................................... 575
20. IMPLICIT OBJECTS, SCOPE AND EL EXPRESSIONS ...................................................... 580
IMPLICIT OBJECTS....................................................................................................................... 580
Request......................................................................................................................................... 580
Response....................................................................................................................................... 582
Out................................................................................................................................................ 584
Session.......................................................................................................................................... 585
Application ................................................................................................................................... 586
Config........................................................................................................................................... 587
PageContext ................................................................................................................................. 588
Page .............................................................................................................................................. 588
Exception...................................................................................................................................... 589
SCOPE ............................................................................................................................................. 590
Application Scope ........................................................................................................................ 590
Session Scope............................................................................................................................... 591
Request Scope .............................................................................................................................. 591
Page Scope ................................................................................................................................... 592
CHARACTER QUOTING CONVENTIONS ................................................................................. 592
UNIFIED EXPRESSION LANGUAGE [UNIFIED EL] ................................................................ 593
Unified EL Evaluation Syntax...................................................................................................... 595
Immediate Evaluation............................................................................................................... 595
Deferred Evaluation ................................................................................................................. 596
Types Of Expressions................................................................................................................... 597
Value Expressions .................................................................................................................... 597
Method Expressions ................................................................................................................. 598
Using EL Expressions .................................................................................................................. 598
Literals.......................................................................................................................................... 599
Operators ...................................................................................................................................... 600
Arithmetic Operators ................................................................................................................ 600
Logical Operators ..................................................................................................................... 601
Relational Operators................................................................................................................. 601
Conditional Operators .............................................................................................................. 602
The . [A Dot] Operator ............................................................................................................. 602

Java Server Programming For Professionals


Page xvi

The [] Operator .........................................................................................................................603


The empty Operator..................................................................................................................604
Implicit Objects ............................................................................................................................605
21. CUSTOM TAG LIBRARIES - CLASSIC TAGS......................................................................607
WHAT IS A TAG.............................................................................................................................609
TYPES OF TAGS ............................................................................................................................611
Tags Without Attributes ...............................................................................................................612
Tags With Attributes.....................................................................................................................612
Tags With A Body........................................................................................................................612
Tags Defining Scripting Variables ...............................................................................................613
Tags Cooperating With Each Other..............................................................................................613
THE COMPOSITION OF A TAG LIBRARY.................................................................................614
The Tag Handler...........................................................................................................................614
The TLD File ................................................................................................................................615
The JSP Page ................................................................................................................................620
DEFINING A TAG ..........................................................................................................................621
Lifecycle Of Tags .........................................................................................................................621
Creating Tag Handler Instance .................................................................................................621
Passing The Contextual And Environmental Information ........................................................622
Passing The Parental Information.............................................................................................623
Executing The Functionality.....................................................................................................623
The doStartTag() Method .....................................................................................................623
The doEndTag() Method ......................................................................................................624
Releasing The State ..................................................................................................................624
DEFINING TAGS WITH ATTRIBUTES .......................................................................................633
Using Dynamic Attributes ............................................................................................................653
DEFINING TAGS WITH BODY ....................................................................................................661
Lifecycle Of Body Tags................................................................................................................661
Using The <body-content> Element In The TLD File .............................................................661
Creating Tag Handler Instance .................................................................................................662
Passing The Contextual, Environmental And Parental Information .........................................662
Calling The doStartTag() Method.............................................................................................663
Setting The Body Content ........................................................................................................663
Calling The doInitBody() Method ............................................................................................663
Calling The doAfterBody() Method .........................................................................................664
Calling The doEndTag() Method..............................................................................................664
Releasing The State ..................................................................................................................664
DEFINING TAGS THAT ITERATE...............................................................................................671
Lifecycle Of The Iteration Tags....................................................................................................672
Creating The Tag Handler Class...............................................................................................672
Passing The Contextual, Environmental And Parental Information .........................................672
Calling The doStartTag() Method.............................................................................................672
Calling The doAfterBody() Method .........................................................................................673
Calling The doEndTag() Method..............................................................................................673
Releasing The Sate ...................................................................................................................673

Table Of Contents
Page xvii

DEFINING TAGS WITH SCRIPTING VARIABLES ................................................................... 679


Defining The Scripting Variables In The TLD File ..................................................................... 679
Defining The Scripting Variables In The TEI File ....................................................................... 686
HANDS ON EXERCISES ............................................................................................................... 693
22. CUSTOM TAG LIBRARIES - TAG FILES ............................................................................. 696
Tag File Directives ....................................................................................................................... 698
The tag Directive ...................................................................................................................... 699
The include Directive ............................................................................................................... 705
The taglib Directive.................................................................................................................. 710
The attribute Directive.............................................................................................................. 715
The variable Directive .............................................................................................................. 721
The <jsp:doBody> Action Element.......................................................................................... 723
The <jsp:invoke> Action Element............................................................................................ 724
The <jsp:attribute> Action Element ......................................................................................... 725
The <jsp:body> Action Element .............................................................................................. 727
HANDS ON EXERCISES ............................................................................................................... 729
23. CUSTOM TAG LIBRARIES - SIMPLE TAGS........................................................................ 732
SIMPLE TAGS ................................................................................................................................ 733
SimpleTag Interface ..................................................................................................................... 733
SimpleTagSupport Class .............................................................................................................. 734
JSP Fragments .............................................................................................................................. 734
Lifecycle Of Simple Tags............................................................................................................. 735
Attributes And Variables.............................................................................................................. 742
HANDS ON EXERCISES ............................................................................................................... 766
24. JSP STANDARD TAG LIBRARIES.......................................................................................... 769
WHAT IS WRONG IN USING JSP SCRIPLET TAGS ................................................................. 770
HOW JSTL FIXES JSP SCRIPLET'S SHORTCOMINGS ............................................................. 770
DISADVANTAGES OF JSTL......................................................................................................... 771
TAG LIBRARIES ............................................................................................................................ 771
The Core Tag Library................................................................................................................... 772
General-Purpose Actions.......................................................................................................... 772
How Are Values Displayed .................................................................................................. 773
How Are Values Set ............................................................................................................. 773
Exception Handling.............................................................................................................. 774
How Are Values Removed................................................................................................... 775
Conditional Actions Or Flow Control Statements.................................................................... 775
Iterator Actions......................................................................................................................... 776
The <c:forEach> Element..................................................................................................... 777
The <c:forTokens> Element................................................................................................. 779
URL-Related Actions ............................................................................................................... 779
Importing The Contents Of A URL Based Resources.......................................................... 779
URL Encoding And Rewriting ............................................................................................. 780
Sending An HTTP Redirect To A Client.............................................................................. 781
Specifying The Request Parameters ..................................................................................... 782

Java Server Programming For Professionals


Page xviii

The XML Tag Library ..................................................................................................................782


Core Actions .............................................................................................................................782
What is XPath.......................................................................................................................782
Using The Select Attribute ...................................................................................................787
How Are Resources Accessed ..............................................................................................788
How Are XML Documents Parsed .......................................................................................788
Conditional Or Flow Control Statements .................................................................................789
Transforming XML ..................................................................................................................790
The Internationalization And Formatting Tag Library .................................................................795
Locale Actions..........................................................................................................................796
Setting The Locale................................................................................................................796
Character Encoding For An HTTP Request .........................................................................797
Messaging Actions....................................................................................................................798
The <fmt:Message> Element................................................................................................798
Obtaining The Resource Bundle...........................................................................................798
Formatting The Dates And Numbers........................................................................................799
Formatting Numbers.............................................................................................................799
Parsing Numbers...................................................................................................................801
Formatting The Date.............................................................................................................802
Parsing The Date...................................................................................................................803
Time Zones ...........................................................................................................................804
The Database / SQL Tag Library..................................................................................................804
Datasources...............................................................................................................................805
Queries And Updates................................................................................................................806
The Database Transactions .......................................................................................................810
Functions Tag Library ..................................................................................................................810
The fn:contains Function ..........................................................................................................811
The fn:containsIgnoreCase Function ........................................................................................811
The fn:endsWith Function ........................................................................................................811
The fn:escapeXml Function......................................................................................................812
The fn:indexOf Function ..........................................................................................................812
The fn:join Function .................................................................................................................812
The fn:length Function .............................................................................................................813
The fn:replace Function............................................................................................................813
The fn:split Function.................................................................................................................813
The fn:startsWith Function .......................................................................................................813
The fn:substring Function.........................................................................................................814
The fn:substringAfter Function ................................................................................................814
The fn:substringBefore Function ..............................................................................................814
The fn:toLowerCase Function ..................................................................................................815
The fn:toUpperCase Function...................................................................................................815
The fn:trim Function.................................................................................................................815
HANDS ON EXERCISES ...............................................................................................................834

Table Of Contents
Page xix

SECTION IV: JAVA SERVER FACES


25. INTRODUCTION TO JAVA SERVER FACES....................................................................... 837
WHY MODEL VIEW CONTROLLER........................................................................................... 837
WHAT IS JSF .................................................................................................................................. 838
Server Side ................................................................................................................................... 838
User Interface ............................................................................................................................... 838
Component Framework................................................................................................................ 838
COMPONENTS OF JSF.................................................................................................................. 838
A JSF Application's Features........................................................................................................ 839
MODEL VIEW CONTROLLER DESIGN PATTERN................................................................... 840
BENEFITS OF JAVA SERVER FACES ........................................................................................ 842
Based On Model View Controller Architecture ........................................................................... 842
Ease-Of-Use ................................................................................................................................. 842
Standardization............................................................................................................................. 843
Based On Reusable Components.................................................................................................. 843
Making UI Development Easier ................................................................................................... 843
Handling Data Transfer Mechanism............................................................................................. 843
Easily Manageable Event Handling ............................................................................................. 843
Separating Behavior And Presentation Layer............................................................................... 843
Influence UI Component And Web Tier Concepts ...................................................................... 844
Making Web Development Faster And Easier ............................................................................. 844
DISADVANTAGES OF JAVA SERVER FACES ......................................................................... 844
JAVA SERVER FACES APPLICATION ....................................................................................... 845
Java Server Faces Application Composition ................................................................................ 846
Java Server Faces Application Lifecycle...................................................................................... 847
Restore View ................................................................................................................................ 849
Apply Request Values .................................................................................................................. 849
Process Validations ...................................................................................................................... 850
Update Model Values................................................................................................................... 851
Invoke Application ....................................................................................................................... 852
Render Response .......................................................................................................................... 852
CONFIGURING THE JAVA SEVER FACES................................................................................ 853
A SIMPLE LOGIN EXAMPLE....................................................................................................... 855
26. COMPONENTS AND RENDERERS ........................................................................................ 867
COMPONENTS............................................................................................................................... 869
UIComponent ............................................................................................................................... 870
Standard JSF UI Components ...................................................................................................... 870
UIViewRoot ............................................................................................................................. 872
UICommand ............................................................................................................................. 872
The <h:commandButton> Element ...................................................................................... 873
The <h:commandLink> Element.......................................................................................... 874
UIForm ..................................................................................................................................... 875
UIGraphic................................................................................................................................. 876
UIInput ..................................................................................................................................... 877
The <h:inputText> Element ................................................................................................. 877

Java Server Programming For Professionals


Page xx

The <h:inputTextarea> Element ...........................................................................................879


The <h:inputSecret> Element ...............................................................................................880
The <h:inputHidden> Element .............................................................................................881
UIOutput...................................................................................................................................883
The <h:outputText> Element................................................................................................883
The <h:outputLabel> Element ..............................................................................................884
The <h:outputLink> Element ...............................................................................................885
The <h:outputFormat> Element............................................................................................887
UIPanel .....................................................................................................................................889
The <h:panelGrid> Element .................................................................................................889
The <h:panelGroup> Element ..............................................................................................891
UIParameter..............................................................................................................................892
UISelectBoolean .......................................................................................................................893
UISelectOne..............................................................................................................................894
The <h:selectOneRadio> Element ........................................................................................894
The <h:selectOneListbox> Element .....................................................................................896
The <h:selectOneMenu> Element ........................................................................................898
UISelectMany...........................................................................................................................900
The <h:selectManyCheckbox> Element...............................................................................900
The <h:selectManyListbox> Element...................................................................................902
he <h:selectManyMenu> Element ........................................................................................904
UISelectItem.............................................................................................................................905
UISelectItems ...........................................................................................................................906
UIMessage ................................................................................................................................907
UIMessages ..............................................................................................................................908
UIData ......................................................................................................................................910
UIColumn .................................................................................................................................912
UINamingContainer .................................................................................................................913
HTML Attributes ..........................................................................................................................913
RENDERER .....................................................................................................................................932
Render Kits ...................................................................................................................................932
CORE LIBRARY ACTIONS...........................................................................................................950
Registering Listener......................................................................................................................950
The <f:actionListener> Element ...............................................................................................950
The <f:setPropertyActionListener> Element............................................................................951
The <f:valueChangeListener> Element ....................................................................................952
The <f:phaseListener> Element................................................................................................953
Adding Attributes To The UI Components ..................................................................................954
Registering Facets.........................................................................................................................955
Displaying Static Text ..................................................................................................................957
27. MANAGED BEANS AND NAVIGATION ................................................................................959
MANAGED BEANS........................................................................................................................959
Creating Bean ...............................................................................................................................960
Registering Bean...........................................................................................................................961
Binding Bean ................................................................................................................................962
Managed Bean Property Initialization ..........................................................................................962

Table Of Contents
Page xxi

NAVIGATION................................................................................................................................. 963
Configuring Navigation Rule ....................................................................................................... 964
Ways Of Navigating Between Pages........................................................................................ 967
The Default Navigation Case ............................................................................................... 967
The Default Outcome Case .................................................................................................. 967
Using Wildcards Or Patterns ................................................................................................ 968
Using The <from-action> Element....................................................................................... 969
Global Outcome ................................................................................................................... 970
Redirect Rendering............................................................................................................... 970
Navigation Cases Split Over Multiple Rules........................................................................ 972
Overlapping Navigation Cases ............................................................................................. 972
Conflict Of Navigation Cases............................................................................................... 973
Navigation Process ....................................................................................................................... 974
Types Of Navigation .................................................................................................................... 976
Simple Or Static Navigation..................................................................................................... 976
Complex Or Dynamic Navigation............................................................................................ 978
Actions And Navigation ............................................................................................................... 980
Implementing Application Actions .............................................................................................. 981
28. CONVERTERS AND VALIDATORS ....................................................................................... 995
CONVERTERS................................................................................................................................ 996
VALIDATORS ................................................................................................................................ 996
CONVERSION AND VALIDATION PROCESS........................................................................... 997
GETTING STARTED WITH CONVERTERS ............................................................................... 998
Standard Converters ..................................................................................................................... 999
DateTime Converter ............................................................................................................... 1000
Number Converter.................................................................................................................. 1003
Custom Converters ..................................................................................................................... 1005
Implementing The Converter Interface .................................................................................. 1006
Implementing The getAsObject() Method.............................................................................. 1007
Implementing The getAsString() Method .............................................................................. 1008
Registering The Custom Converter ........................................................................................ 1008
Using The <f:converter> Element .......................................................................................... 1010
GETTING STARTED WITH VALIDATORS .............................................................................. 1011
Standard Validators .................................................................................................................... 1012
Length Validator..................................................................................................................... 1012
Long Range Validator ............................................................................................................ 1013
Double Range Validator ......................................................................................................... 1013
Custom Validators...................................................................................................................... 1014
Implementing A Validator Interface....................................................................................... 1015
Implementing The validate() Method..................................................................................... 1015
Registering A Custom Validator ............................................................................................ 1016
Using The <f:validator> Element........................................................................................... 1017
Application Level Validation ..................................................................................................... 1017
Validator Methods In Backing Bean .......................................................................................... 1018
The required And The immediate Attributes.............................................................................. 1020
The required Attribute ........................................................................................................ 1020

Java Server Programming For Professionals


Page xxii

The immediate Attribute.....................................................................................................1020


DISPLAYING ERROR MESSAGES ............................................................................................1021
AN EXAMPLE OF STANDARD CONVERTERS AND VALIDATORS...................................1024
AN EXAMPLE OF CUSTOM CONVERTERS AND VALIDATORS ........................................1038
HANDS ON EXERCISES .............................................................................................................1052

SECTION V: ENTERPRISE JAVA BEANS


29. INTRODUCTION TO ENTERPRISE JAVA BEANS ...........................................................1053
ENTERPRISE BEAN ARCHITECTURE .....................................................................................1053
How Does The Communication Takes Place .............................................................................1054
Enterprise Bean Server ...............................................................................................................1055
An Enterprise Bean.....................................................................................................................1056
Enterprise Bean Containers ........................................................................................................1057
Enterprise Bean Clients ..............................................................................................................1058
FEATURES / BENEFITS OF ENTERPRISE BEAN....................................................................1059
Complete Focus Only On Business Logic ..................................................................................1059
Reusable Components ................................................................................................................1059
Portable.......................................................................................................................................1060
Fast Building Of Applications ....................................................................................................1060
One Business Logic Having Many Presentation Logics.............................................................1060
Distributed Deployment..............................................................................................................1060
Application Interoperability........................................................................................................1061
Customized Deployment ............................................................................................................1061
Simplified EJB 3.0 API ..............................................................................................................1061
TYPES OF ENTERPRISE BEAN .................................................................................................1062
Session Beans .............................................................................................................................1062
Stateless Session Bean............................................................................................................1062
Life Cycle Of Stateless Session Bean.................................................................................1063
Stateful Session Bean .............................................................................................................1063
Life Cycle Of Stateful Session Bean ..................................................................................1064
Entity Beans................................................................................................................................1065
Message Driven Beans ...............................................................................................................1065
Life Cycle Of Message-Driven Bean..................................................................................1066
WRITING ENTERPRISE BEAN ..................................................................................................1067
Writing Business Interface .........................................................................................................1067
Writing Bean Class .....................................................................................................................1068
Points To Be Remembered While Writing Bean Class ..........................................................1068
Writing Deployment Descriptor .................................................................................................1069
Packaging Bean ..........................................................................................................................1070
Deployment of Bean ...................................................................................................................1071
Writing the Client .......................................................................................................................1071
30. BEGINNING WITH ENTERPRISE JAVA BEANS ..............................................................1073
CREATING A BUSINESS INTERFACE .....................................................................................1073
CREATING AN ENTERPRISE BEAN.........................................................................................1075

Table Of Contents
Page xxiii

CREATING A WEB CLIENT USING JSP................................................................................... 1077


COMPILATION AND PACKAGING .......................................................................................... 1081
DEPLOYING THE WEB APPLICATION ................................................................................... 1097
RUNNING THE APPLICATION.................................................................................................. 1097
31. WORKING WITH SESSION BEANS ..................................................................................... 1101
STATELESS SESSION BEANS ................................................................................................... 1102
Instance Pool Mechanism........................................................................................................... 1103
STATEFUL SESSION BEANS..................................................................................................... 1103
Passivation And Activation ........................................................................................................ 1104
AFTER BEAN INSTANTIATION................................................................................................ 1105
BEAN INSTANCES MANAGEMENT BY THE CONTAINER ................................................. 1106
REMOTE AND LOCAL INTERFACE......................................................................................... 1107
LIFECYCLE CALLBACK METHODS........................................................................................ 1107
Callbacks For Stateless Session Beans....................................................................................... 1108
@PostConstruct...................................................................................................................... 1108
@PreDestroy .......................................................................................................................... 1108
Callbacks For Stateful Session Beans ........................................................................................ 1109
@PostConstruct...................................................................................................................... 1109
@PreDestroy .......................................................................................................................... 1109
@PrePassivate ........................................................................................................................ 1109
@PostActivate........................................................................................................................ 1110
@Init....................................................................................................................................... 1110
Bean Removal ............................................................................................................................ 1110
@Remove............................................................................................................................... 1110
THE SHOPPING CART [STATEFUL SESSION BEAN]............................................................ 1111
Creating A Business Interface .................................................................................................... 1113
Creating An Enterprise Bean...................................................................................................... 1115
Lifecycle Callback Methods................................................................................................... 1122
Creating A Web Client Using JSP.............................................................................................. 1122
Db Tables For The Enterprise Bean ........................................................................................... 1128
Compilation And Packaging....................................................................................................... 1129
Deploying The Web Application................................................................................................ 1131
Running The Application ........................................................................................................... 1131
THE VISITOR STATISTICS [STATELESS SESSION BEAN] .................................................. 1133
Creating A Business Interface .................................................................................................... 1134
Creating An Enterprise Bean...................................................................................................... 1135
Creating A Web Client Using JSP.............................................................................................. 1139
Db Tables For The Enterprise Bean ........................................................................................... 1141
Compilation And Packaging....................................................................................................... 1142
Deploying The Web Application................................................................................................ 1143
Running The Application ........................................................................................................... 1144
32. WORKING WITH MESSAGE-DRIVEN BEANS ................................................................. 1146
MESSAGE DRIVEN BEAN IN EJB 3.0....................................................................................... 1147
THE onMessage() METHOD......................................................................................................... 1147
USES OF MESSAGE DRIVEN BEANS ...................................................................................... 1148

Java Server Programming For Professionals


Page xxiv

A BASIC MESSAGE DRIVEN BEAN .........................................................................................1148


Creating An Enterprise Bean ......................................................................................................1149
Creating A Web Client Using JSP..............................................................................................1154
Compilation And Packaging.......................................................................................................1159
Creating The Administered Objects ...........................................................................................1163
Deploying The Web Application................................................................................................1164
Running The Application ...........................................................................................................1164
REMOVING THE ADMINISTERED OBJECTS .........................................................................1165
33. WORKING WITH ENTITY BEANS.......................................................................................1167
JAVA PERSISTENCE ...................................................................................................................1168
The Java Persistence API [JPA] .................................................................................................1168
The Query Language ..................................................................................................................1169
ENTITY CLASS ............................................................................................................................1169
PERSISTENT FIELDS AND PROPERTIES ................................................................................1170
USER REGISTRATION AND LOGIN .........................................................................................1171
Creating A Database Table .........................................................................................................1174
Creating An Enterprise Bean ......................................................................................................1178
Creating The Web Client ............................................................................................................1181
Compilation And Packaging.......................................................................................................1192
Deploying The Web Application................................................................................................1196
Running The Application ...........................................................................................................1196
AUTHOR MASTER EXAMPLE...................................................................................................1198
Creating A Database Table .........................................................................................................1198
Creating An Enterprise Bean ......................................................................................................1199
Creating A Web Client ...............................................................................................................1202
Compilation And Packaging.......................................................................................................1213
Deploying The Web Application................................................................................................1215
Running The Application ...........................................................................................................1215
HANDS ON EXERCISES .............................................................................................................1216

SECTION VI: PROJECT DEVELOPMENT USING JAVA EE 5


34. PERSONNEL MANAGEMENT SYSTEM .............................................................................1219
The Current Business Model ......................................................................................................1219
DATABASE STRUCTURE...........................................................................................................1221
System And Referential Information ..........................................................................................1222
Authentication Information ........................................................................................................1224
Client Information ......................................................................................................................1225
Candidate Information ................................................................................................................1229
PERSONNEL MANAGEMENT SYSTEM MANUAL ................................................................1234
GETTING STARTED ....................................................................................................................1235
The Home Page ..........................................................................................................................1235
REQUIREMENTS .........................................................................................................................1236
SECTIONS .....................................................................................................................................1236
Clients Section............................................................................................................................1237

Table Of Contents
Page xxv

Candidates Section ..................................................................................................................... 1237


The Administrator Module ......................................................................................................... 1237
The Data Entry Forms Of The Administrative Module.......................................................... 1239
Adding A New Category .................................................................................................... 1240
Modifying Existing Categories........................................................................................... 1242
Deleting Existing Categories .............................................................................................. 1243
The Default Login Module......................................................................................................... 1245
Sign Up Options ..................................................................................................................... 1247
The Candidate Module ............................................................................................................... 1248
Candidate Registration ........................................................................................................... 1248
Personal Information .............................................................................................................. 1249
Qualification Details............................................................................................................... 1251
Employment Details ............................................................................................................... 1253
Language Proficiency............................................................................................................. 1255
Candidate's Home Page .......................................................................................................... 1257
Modifying Login Information ............................................................................................ 1258
Modifying Personal Information ........................................................................................ 1258
Modifying Qualification Details......................................................................................... 1259
Modifying Details for Job Experience................................................................................ 1260
Modifying Details for Languages Known .......................................................................... 1261
Incomplete Resume .................................................................................................................... 1262
Client Login................................................................................................................................ 1263
Help Pages.................................................................................................................................. 1264
TECHNICAL SPECIFICATIONS................................................................................................. 1265
The Administration Module ....................................................................................................... 1265
Login Form............................................................................................................................. 1265
Layout................................................................................................................................. 1265
Purpose ............................................................................................................................... 1265
Objects On The HTML Page.............................................................................................. 1265
Form Details ....................................................................................................................... 1265
Data Fields.......................................................................................................................... 1265
Data Controls...................................................................................................................... 1266
Page Managing List Of Job Categories .................................................................................. 1266
Layout................................................................................................................................. 1266
Purpose ............................................................................................................................... 1266
Objects On The HTML Page.............................................................................................. 1266
Form Details ....................................................................................................................... 1267
Data Fields.......................................................................................................................... 1267
Data Controls...................................................................................................................... 1267
Page Managing List Of Countries .......................................................................................... 1267
Purpose ............................................................................................................................... 1267
Objects On The HTML Page.............................................................................................. 1268
Form Details ....................................................................................................................... 1268
Data Fields.......................................................................................................................... 1268
Data Controls...................................................................................................................... 1268
Page Managing List Of Languages ........................................................................................ 1268
Purpose ............................................................................................................................... 1269

Java Server Programming For Professionals


Page xxvi

Objects On The HTML Page ..............................................................................................1269


Form Details .......................................................................................................................1269
Data Fields..........................................................................................................................1269
Data Controls......................................................................................................................1269
Page Allowing Access To The PMS.......................................................................................1270
Layout.................................................................................................................................1270
Purpose ...............................................................................................................................1270
Objects On The HTML Page ..............................................................................................1270
Page Capturing The Login Information..................................................................................1271
Layout.................................................................................................................................1271
Purpose ...............................................................................................................................1271
Objects On The HTML Page ..............................................................................................1271
Form Details .......................................................................................................................1271
Data Fields..........................................................................................................................1271
Data Controls......................................................................................................................1272
The Candidate Module ...............................................................................................................1272
The Signup Process Page Flow...............................................................................................1272
The Login Process Page Flow ................................................................................................1276
Candidate Registration Page...................................................................................................1276
Layout.................................................................................................................................1276
Purpose ...............................................................................................................................1277
Form Details .......................................................................................................................1277
Data Fields..........................................................................................................................1277
Data Controls......................................................................................................................1277
Candidate Home Page.............................................................................................................1278
Layout.................................................................................................................................1278
Purpose ...............................................................................................................................1278
Objects On The HTML Page ..............................................................................................1278
Page Managing Candidate's Personal Information .................................................................1279
Layout.................................................................................................................................1279
Purpose ...............................................................................................................................1279
Objects On The HTML Page ..............................................................................................1279
Form Details .......................................................................................................................1280
Data Fields..........................................................................................................................1280
Data Controls......................................................................................................................1281
Page Managing Candidate's Qualification Information ..........................................................1282
Layout.................................................................................................................................1282
Purpose ...............................................................................................................................1282
Objects On The HTML Page ..............................................................................................1282
Form Details .......................................................................................................................1283
Data Fields..........................................................................................................................1283
Data Controls......................................................................................................................1283
Page Managing Candidate's Employment Information ..........................................................1284
Layout.................................................................................................................................1284
Purpose ...............................................................................................................................1284
Objects On The HTML Page ..............................................................................................1285
Form Details .......................................................................................................................1285

Table Of Contents
Page xxvii

Data Fields.......................................................................................................................... 1285


Data Controls...................................................................................................................... 1286
Page Managing Candidate's Language Proficiency Information............................................ 1286
Layout................................................................................................................................. 1286
Purpose ............................................................................................................................... 1286
Objects On The HTML Page.............................................................................................. 1287
Form Details ....................................................................................................................... 1287
Data Fields.......................................................................................................................... 1287
Data Controls...................................................................................................................... 1287
Change Login Page Of The Candidate Module...................................................................... 1288
Layout................................................................................................................................. 1288
Purpose ............................................................................................................................... 1288
The Client Module...................................................................................................................... 1288
SITE MAP - PERSONNEL MANAGEMENT SYSTEM ............................................................. 1289
DEVELOPMENT PROPOSAL ..................................................................................................... 1289

Java Server Programming For Professionals

You might also like