Thursday, September 2, 2010

Java for the Web with Servlets, JSP, and EJB













Table of Content
Table of Content........................................................................................................... ii
Copyright ...................................................................................................................... ix
Copyright © 2002 by New Riders Publishing...................................................... ix
Trademarks .............................................................................................................. ix
Warning and Disclaimer ......................................................................................... ix
About the Author ......................................................................................................... xi
About the Technical Reviewers ................................................................................ xi
Acknowledgments....................................................................................................... xi
Tell Us What You Think............................................................................................. xii
Introduction.................................................................................................................. xii
The Hypertext Transfer Protocol (HTTP) .......................................................... xiv
System Architecture............................................................................................. xvii
Java 2, Enterprise Edition (J2EE).....................................................................xviii
Developing Web Applications in Java..............................................................xviii
Overview of Parts and Chapters......................................................................... xix
Part I: Building Java Web Applications......................................................................... 1
Chapter 1. The Servlet Technology .......................................................................... 2
The Benefits of Servlets .......................................................................................... 2
Servlet Application Architecture............................................................................. 4
How a Servlet Works ............................................................................................... 5
The Tomcat Servlet Container ............................................................................... 5
Six Steps to Running Your First Servlet ............................................................... 6
Summary ................................................................................................................. 11
Chapter 2. Inside Servlets......................................................................................... 12
The javax.servlet Package.................................................................................... 12
A Servlet's Life Cycle............................................................................................. 13
Obtaining Configuration Information ................................................................... 16
Preserving the ServletConfig................................................................................ 18
The Servlet Context ............................................................................................... 19
Sharing Information Among Servlets .................................................................. 21
Requests and Responses..................................................................................... 23
The GenericServlet Wrapper Class .................................................................... 28
Creating Thread-Safe Servlets............................................................................. 29
Summary ................................................................................................................. 34
Chapter 3. Writing Servlet Applications.................................................................. 35
The HttpServlet Class............................................................................................ 35
The HttpServletRequest Interface ....................................................................... 39
HttpServletResponse............................................................................................. 48
Sending an Error Code.......................................................................................... 51
Sending Special Characters................................................................................. 51
Buffering the Response......................................................................................... 55
Populating HTML Elements.................................................................................. 56
Request Dispatching.............................................................................................. 57
Summary ................................................................................................................. 65
Chapter 4. Accessing Databases with JDBC ........................................................ 67
The java.sql Package ............................................................................................ 67
Four Steps to Getting to the Database ............................................................... 71
A Database-Based Login Servlet......................................................................... 79
The Single Quote Factor....................................................................................... 84
Inserting Data into a Table with RegistrationServlet ........................................ 86
Displaying All Records........................................................................................... 96
Search Page ........................................................................................................... 98
An Online SQL Tool............................................................................................. 102
Should I Keep the Connection Open? .............................................................. 108
Transactions.......................................................................................................... 109
Connection Pooling.............................................................................................. 110
Summary ............................................................................................................... 110
Chapter 5. Session Management .......................................................................... 111
What Is Session Management?......................................................................... 111
URL Rewriting....................................................................................................... 113
Hidden Fields........................................................................................................ 125
Cookies .................................................................................................................. 138
Session Objects.................................................................................................... 152
Knowing Which Technique to Use .................................................................... 160
Summary ............................................................................................................... 161
Chapter 6. Application and Session Events......................................................... 162
Listening to Application Events .......................................................................... 162
Listening to HttpSession Events ........................................................................ 171
Summary ............................................................................................................... 177
Chapter 7. Servlet Filtering..................................................................................... 178
An Overview of the API ....................................................................................... 178
A Basic Filter......................................................................................................... 180
Mapping a Filter with a URL ............................................................................... 182
A Logging Filter..................................................................................................... 183
Filter Configuration............................................................................................... 185
A Filter that Checks User Input .......................................................................... 186
Filtering the Response ........................................................................................ 192
Filter Chain............................................................................................................ 195
Summary ............................................................................................................... 199
Chapter 8. JSP Basics ............................................................................................ 200
What's Wrong with Servlets?.............................................................................. 200
Running Your First JSP....................................................................................... 202
How JSP Works.................................................................................................... 206
The JSP Servlet Generated Code..................................................................... 206
The JSP API.......................................................................................................... 208
The Generated Servlet Revisited ...................................................................... 210
Implicit Objects ..................................................................................................... 214
Summary ............................................................................................................... 217
Chapter 9. JSP Syntax ............................................................................................ 218
Directives............................................................................................................... 218
Scripting Elements ............................................................................................... 229
Standard Action Elements .................................................................................. 239
Comments ............................................................................................................. 240
Converting into XML Syntax............................................................................... 241
Summary ............................................................................................................... 242
Chapter 10. Developing JSP Beans...................................................................... 243
Calling Your Bean from a JSP Page................................................................. 243
A Brief Theory of JavaBeans.............................................................................. 245
Making a Bean Available .................................................................................... 246
Accessing Properties Using jsp:getProperty and jsp:setProperty ................ 250
Setting a Property Value from a Request......................................................... 252
JavaBeans Code Initialization............................................................................ 254
The SQLToolBean Example............................................................................... 255
Summary ............................................................................................................... 262
Chapter 11. Using JSP Custom Tags................................................................... 264
Writing Your First Custom Tag........................................................................... 265
The Role of the Deployment Descriptor ........................................................... 269
The Tag Library Descriptor................................................................................. 270
The Custom Tag Syntax ..................................................................................... 272
The JSP Custom Tag API................................................................................... 273
The Life Cycle of a Tag Handler ........................................................................ 274
Summary ............................................................................................................... 285
Chapter 12. Programmable File Download.......................................................... 286
Keys to Programmable File Download ............................................................. 287
Using the Brainysoftware.com File Download Bean ...................................... 288
Summary ............................................................................................................... 288
Chapter 13. File Upload .......................................................................................... 290
The HTTP Request .............................................................................................. 290
Client-Side HTML................................................................................................. 294
HTTP Request of an Uploaded File .................................................................. 295
Uploading a File.................................................................................................... 298
FileUpload Bean................................................................................................... 302
Multiple File Upload ............................................................................................. 303
Summary ............................................................................................................... 304
Chapter 14. Security Configuration ....................................................................... 305
Imposing Security Constraints ........................................................................... 305
Allowing Multiple Roles ....................................................................................... 312
Form-Based Authentication................................................................................ 313
Digest Authentication........................................................................................... 316
Methods Related to Security .............................................................................. 317
Restricting Certain Methods ............................................................................... 319
Summary ............................................................................................................... 319
Chapter 15. Caching................................................................................................ 320
Caching Data into a Text File............................................................................. 320
Caching in Memory .............................................................................................. 325
Summary ............................................................................................................... 330
Chapter 16. Application Deployment .................................................................... 331
Application Directory Structure .......................................................................... 331
Deployment Descriptor........................................................................................ 333
Servlet Alias and Mapping.................................................................................. 351
JSP Alias and Mapping ....................................................................................... 353
Packaging and Deploying a Web Application.................................................. 355
Summary ............................................................................................................... 355
Chapter 17. Architecting Java Web Applications................................................ 356
Model 1 Architecture............................................................................................ 356
Model 2 Architecture............................................................................................ 361
Chapter 18. Developing E-Commerce Applications ........................................... 365
Project Specification ............................................................................................ 365
The Database Structure...................................................................................... 366
Page Design.......................................................................................................... 367
Preparation............................................................................................................ 367
Application Design ............................................................................................... 368
Building the Project .............................................................................................. 371
Summary ............................................................................................................... 388
Chapter 19. XML-Based E-Books ......................................................................... 389
The Table of Contents......................................................................................... 390
Translating XML into the Object Tree ............................................................... 390
The Project ............................................................................................................ 392
Pre-Render the Table of Contents..................................................................... 403
Summary ............................................................................................................... 403
Chapter 20. Web-Based Document Management.............................................. 404
The Docman Project ............................................................................................ 405
Summary ............................................................................................................... 437
Part II: Client-Side Programming with JavaScript................................................... 439
Chapter 21. JavaScript Basics............................................................................... 440
Introduction to JavaScript ................................................................................... 440
Adding JavaScript Code to HTML ..................................................................... 450
JavaScript Object Model ..................................................................................... 452
Event Handler ....................................................................................................... 453
Window and String Objects ................................................................................ 454
Summary ............................................................................................................... 457
Chapter 22. Client-Side Programming Basics..................................................... 458
Checking Whether JavaScript Is Enabled........................................................ 458
Handling JavaScript-Unaware Browsers.......................................................... 461
Handling Different Versions of JavaScript........................................................ 461
Including a JavaScript File.................................................................................. 462
Checking the Operating System ........................................................................ 463
Checking the Browser Generation .................................................................... 464
Checking the Browser Type ............................................................................... 465
Checking the Browser Language ...................................................................... 465
Handling Dynamic Variable-Names .................................................................. 466
Summary ............................................................................................................... 467
Chapter 23. Redirection .......................................................................................... 468
Anticipating Failed Redirection .......................................................................... 468
Using the Refresh Met Tag .............................................................................. 468
Using the location Object .................................................................................... 469
Going Back to the Previous Page...................................................................... 470
Moving Forward.................................................................................................... 471
Navigation with a SELECT Element.................................................................. 472
Summary ............................................................................................................... 473
Chapter 24. Client-Side Input Validation .............................................................. 474
The isEmpty Function.......................................................................................... 474
The trim Function ................................................................................................. 475
The trimAll Function............................................................................................. 477
The isPositiveInteger Function........................................................................... 477
The isValidPhoneNumber Function .................................................................. 478
The isMoney Function ......................................................................................... 479
The isUSDate and isOZDate Functions ........................................................... 480
Converting Date Formats.................................................................................... 483
Data Type Conversion: String to Numeric........................................................ 483
Data Type Conversion: Numeric to String........................................................ 485
Using the Validation Functions........................................................................... 485
Summary ............................................................................................................... 487
Chapter 25. Working with Client-Side Cookies ................................................... 488
Creating Cookies with a META Tag ............................................................. 488
Creating Cookies with document.cookie .......................................................... 489
Creating Cookies with the setCookie Function................................................ 490
Reading Cookies on the Browser ...................................................................... 492
Deleting a Cookie on the Browser..................................................................... 493
Checking If the Browser Can Accept Cookies Using JavaScript.................. 494
Checking If the Browser Accepts Cookies Without JavaScript..................... 495
Summary ............................................................................................................... 495
Chapter 26. Working with Object Trees................................................................ 496
The Array Object .................................................................................................. 496
Truly Deleting an Array Element........................................................................ 499
Creating an Object ............................................................................................... 501
A Hierarchy of Objects......................................................................................... 501
Summary ............................................................................................................... 513
Chapter 27. Controlling Applets............................................................................. 514
Is Java Enabled?.................................................................................................. 514
Is the Applet Ready? ........................................................................................... 515
Resizing an Applet ............................................................................................... 516
Calling an Applet's Method................................................................................. 516
Getting an Applet's Property............................................................................... 517
Setting an Applet Property.................................................................................. 518
Using Java Classes Directly............................................................................... 519
Applet-to-JavaScript Communication................................................................ 520
Accessing the Document Object Model from an Applet ................................ 522
Invoking JavaScript Functions from an Applet ................................................ 523
Evaluating a JavaScript Statement from an Applet ........................................ 524
Setting the Applet Parameter ............................................................................. 525
Applet-to-Applet Communication Through JavaScript ................................... 526
Direct Applet-to-Applet Communication ........................................................... 528
Summary ............................................................................................................... 530
Part III: Developing Scalable Applications with EJB............................................... 531
Chapter 28. Enterprise JavaBeans ....................................................................... 532
What Is an Enterprise JavaBean?..................................................................... 532
Benefits of EJB ..................................................................................................... 533
EJB Application Architecture.............................................................................. 533
The Six EJB Roles ............................................................................................... 534
Types of Enterprise Beans ................................................................................. 535
Writing Your First Enterprise Bean.................................................................... 535
EJB Explained....................................................................................................... 538
Writing Client Applications .................................................................................. 541
Creating a Bean's Instance................................................................................. 544
Chapter 29. The Session Bean.............................................................................. 548
What Is a Session Bean?.................................................................................... 548
Stateful and Stateless Session Beans.............................................................. 548
Writing a Session Bean....................................................................................... 549
The Tassie Online Bookstore Example ............................................................ 554
Summary ............................................................................................................... 572
Chapter 30. Entity Beans ........................................................................................ 573
What Is an Entity Bean?...................................................................................... 573
The Remote Interface.......................................................................................... 574
The Home Interface ............................................................................................. 574
The Primary Key Class........................................................................................ 576
The Entity Bean.................................................................................................... 576
Two Types of Entity Beans................................................................................. 580
Writing a BMP Entity Bean ................................................................................. 580
Writing a CMP Entity Bean ................................................................................. 596
Summary ............................................................................................................... 602
Chapter 31. EJB Query Language ........................................................................ 603
EJB QL Syntax ..................................................................................................... 604
EJB QL BNF.......................................................................................................... 609
Summary ............................................................................................................... 611
Chapter 32. Java Message Service ...................................................................... 612
Introduction to Messaging................................................................................... 612
The JMS API......................................................................................................... 612
The JMS API Messaging Domains.................................................................... 613
The JMS Object Model........................................................................................ 614
Writing JMS Clients.............................................................................................. 619
Summary ............................................................................................................... 622
Chapter 33. Message-Driven Beans..................................................................... 624
What Is a Message-Driven Bean?..................................................................... 624
The Application Programming Interface ........................................................... 624
Writing a Message-Driven Bean........................................................................ 625
Summary ............................................................................................................... 630
Part IV: Appendixes ..................................................................................................... 631
Appendix A. Tomcat Installation and Configuration............................................ 632
Tomcat Installation............................................................................................... 632
Tomcat Directories............................................................................................... 636
Changing the Port ................................................................................................ 637
Constructing a JSP Application.......................................................................... 637
Appendix B. The javax.servlet Package Reference ........................................... 639
Interfaces............................................................................................................... 640
Classes .................................................................................................................. 650
Exceptions............................................................................................................. 657
Appendix C. The javax.servlet.http Package Reference ................................... 659
Interfaces............................................................................................................... 660
Classes .................................................................................................................. 671
Appendix D. The javax.servlet.jsp Package Reference..................................... 679
Interfaces............................................................................................................... 679
Classes .................................................................................................................. 680
Appendix E. The javax.servlet.jsp.tagext Package Reference ......................... 689
Interfaces............................................................................................................... 689
Classes .................................................................................................................. 692
Appendix F. JBoss Installation and Configuration .............................................. 702
System Requirements ......................................................................................... 702
Installing JBoss..................................................................................................... 702
Directory Structure ............................................................................................... 704
Configuration......................................................................................................... 704
Running JBoss...................................................................................................... 705
Deployment ........................................................................................................... 705
JBoss and Tomcat ............................................................................................... 705
Summary ............................................................................................................... 705
Appendix G. Related Resources ........................................................................... 706
J2EE....................................................................................................................... 706
Servlet .................................................................................................................... 706
JSP ......................................................................................................................... 706
Tag Library ............................................................................................................ 706
Servlet/JSP Containers ....................................................................................... 707
JDBC...................................................................................................................... 707
JNDI........................................................................................................................ 707
JMS......................................................................................................................... 707
EJB ......................................................................................................................... 707
J2EE Server .......................................................................................................... 708
Appendix H. What's On the CD-ROM?................................................................. 709
Read This Before Opening the Software.......................................................... 709
GNU LESSER GENERAL PUBLIC LICENSE................................................. 710
GNU LESSER GENERAL PUBLIC LICENSE................................................. 711
NO WARRANTY................................................................................................... 716

Download
Another J2EE Books

No comments:

Post a Comment

Related Posts with Thumbnails

Put Your Ads Here!