Table of Contents
Introduction..............................................................................................…….xxix
Chapter 1: Getting Started with Web Applications in Java ...............................1
Introduction to Web Applications............................................................................................................................2
Benefits of Web Applications....................................................................................................................................5
Technologies used in Web Applications..................................................................................................................5
Describing JDBC................................................................................................................................................ 5
Exploring the JDBC Architecture........................................................................................................... 6 
Describing Java Servlets................................................................................................................................... 7
Advantages of Java Servlets................................................................................................................... 8 
Describing JSP.................................................................................................................................................... 8
Request Processing of a JSP Page .......................................................................................................... 9
Describing the JSP Life-cycle.................................................................................................................. 9 
Introduction to Web Architecture Models ............................................................................................................10
Describing the Model-1 Architecture ........................................................................................................... 10
Limitations of the Model-1 Architecture ............................................................................................ 11 
Describing the Model-2 Architecture ........................................................................................................... 11
Advantages of the Model-2 Architecture ........................................................................................... 12 
Introduction to the MVC Architecture...................................................................................................................12
Describing the Model Component................................................................................................................ 13
Describing the View Component.................................................................................................................. 13
Describing the Controller Component ......................................................................................................... 13
Summary....................................................................................................................................................................14
Exercise ......................................................................................................................................................................14
Chapter 2: Getting Started with JDBC ..............................................................15
Introducing ODBC....................................................................................................................................................16
Defining ODBC.........................................................................................................................................................16
Listing Features of ODBC .............................................................................................................................. 17
Table of Contents
vi
Describing the ODBC Components .............................................................................................................. 17
The Application Component................................................................................................................ 18 
The DriverManager Component ......................................................................................................... 18 
The Driver Component......................................................................................................................... 18 
The Datasource Component................................................................................................................. 19 
Describing the ODBC Architecture............................................................................................................... 19
The Single-tier ODBC Architecture..................................................................................................... 19 
The Multiple-tier ODBC Architecture................................................................................................. 20 
Introducing JDBC .....................................................................................................................................................20
Describing Components of JDBC.................................................................................................................. 22
Listing Features of JDBC ................................................................................................................................ 22
Listing New Features in JDBC 4.0................................................................................................................. 24
Exploring the JDBC Architecture............................................................................................................................25
The Type-1 Driver........................................................................................................................................... 26
Advantages of the Type-1 Driver ........................................................................................................ 27 
Disadvantages of the Type-1 Driver ................................................................................................... 27 
Situations to Use the Type-1 Driver .................................................................................................... 27 
The Type-2 Driver (Java to native API)........................................................................................................ 27
Advantages of the Type-2 Driver ........................................................................................................ 28 
Disadvantages of the Type-2 Driver ................................................................................................... 28 
Examples of the Type-2 Driver ............................................................................................................ 28 
Situations to Use the Type-2 Driver .................................................................................................... 28 
The Type-3 Driver (Java to Network Protocol/All Java Driver)............................................................... 29
Advantages of the Type-3 Driver ........................................................................................................ 29 
Disadvantages of the Type-3 Driver ................................................................................................... 29 
Examples of the Type-3 Driver ............................................................................................................ 29 
Situations to Use the Type-3 Driver .................................................................................................... 30 
The Type-4 Driver (Java to Database protocol)........................................................................................... 30
Advantages of the Type-4 Driver ........................................................................................................ 30 
Disadvantage of the Type-4 Driver ..................................................................................................... 30 
Examples of the Type-4 Driver ............................................................................................................ 31 
Summary....................................................................................................................................................................31
Exercise ......................................................................................................................................................................32
State True or False ....................................................................................................................................................32 
Chapter 3: Working with JDBC APIs.................................................................33
Introducing JDBC API..............................................................................................................................................34
Explaining the Types of JDBC APIs.............................................................................................................. 35 
The JDBC Core API ............................................................................................................................... 35 
The JDBC Extension API....................................................................................................................... 36 
Table of Contents
vii
Listing the New APIs Added to JDBC 4.0.................................................................................................... 37 
Describing the Major Classes and Interfaces............................................................................................... 38 
The DriverManager Class..................................................................................................................... 38 
The Driver Interface .............................................................................................................................. 39 
The Connection Interface...................................................................................................................... 40 
The Statement Interface ........................................................................................................................ 42 
Communicating with Databases by using JDBC APIs.........................................................................................45
Step 1: Obtaining a Connection..................................................................................................................... 46 
Obtaining an Connection using Driver object.................................................................................... 46 
Step 1.i: Registering a Driver object with DriverManager................................................................ 47 
Step 1.ii: Establishing a Connection using DriverManager.............................................................. 48 
Step 2: Creating a JDBC Statement Object ................................................................................................... 49 
Step 3: Executing SQL Statements................................................................................................................. 49 
Step 4: Closing the Connection...................................................................................................................... 49 
Creating a Simple Application................................................................................................................................50
Configuring the Application.......................................................................................................................... 51 
Creating a Table..................................................................................................................................... 51 
Creating a DSN................................................................................................................................................ 51 
Understanding Various JDBC Drivers...................................................................................................................53
The OCI8 Driver for Oracle............................................................................................................................ 54 
The IDS Driver................................................................................................................................................. 54 
Installing the IDS Server ....................................................................................................................... 54 
Configuring IDS Driver ........................................................................................................................ 57 
The Thin driver for Oracle ............................................................................................................................. 57 
Summary....................................................................................................................................................................58
Exercise ......................................................................................................................................................................58
Fill in the blanks with appropriate words or phrases:.........................................................................................58
Chapter 4: Implementing JDBC Statements and ResultSets ..........................59
Describing JDBC Statement.....................................................................................................................................60
Working with the Statement Interface...................................................................................................................60
Exploring the Methods of Statement ............................................................................................................ 60 
The executeUpdate (String sqlStatement) Method............................................................................ 61 
The executeQuery (String query) Method.......................................................................................... 61 
The execute (String sqlStatement) Method......................................................................................... 61 
The getResultSet () Method .................................................................................................................. 62 
The getUpdateCount () Method .......................................................................................................... 62 
Using Statement .............................................................................................................................................. 62 
Creating a Table..................................................................................................................................... 62 
Inserting a Row...................................................................................................................................... 64 
Table of Contents
viii
Working with the PreparedStatement Interface...................................................................................................65
Comparing Statement and PreparedStatement Objects............................................................................. 66 
Describing the setXXX methods of PreparedStatement............................................................................. 67 
Listing the Advantages and Disadvantages of PreparedStatement ......................................................... 69 
Using PreparedStatement .............................................................................................................................. 69 
Creating a PreparedStatement Object................................................................................................. 69 
Setting the Values for Parameters........................................................................................................ 70 
Executing the PreparedStatement ....................................................................................................... 70 
Using PreparedStatement with Parameters................................................................................................. 70 
Exploring the Usage of the PreparedStatement Object .............................................................................. 73 
Working with the CallableStatement Interface.....................................................................................................73
Describing Stored Procedures ....................................................................................................................... 73 
Listing the Benefits of Working with a Stored Procedure ......................................................................... 74 
Using CallableStatement ................................................................................................................................ 74 
Creating the CallableStatement object ................................................................................................ 74 
Setting the Values of the Parameters................................................................................................... 75 
Registering the OUT Parameter Type................................................................................................. 75 
Executing the Stored Procedure or Function ..................................................................................... 75 
Retrieving the Parameter Values ......................................................................................................... 75 
Using CallableStatement with Parameters .................................................................................................. 75 
Executing a Stored Procedure with IN Parameters........................................................................... 75 
Executing a Stored Procedure with OUT Parameters....................................................................... 77 
Calling Functions by Using CallableStatement........................................................................................... 79 
Using Cursors in CallableStatement............................................................................................................. 81 
Comparing Procedure and Function ............................................................................................................ 82 
Working with the ResultSet Interface ....................................................................................................................82
Describing the Methods of ResultSet............................................................................................................ 83 
Using ResultSet................................................................................................................................................ 85 
Moving the Cursor Position ................................................................................................................. 86 
Reading the Column Values................................................................................................................. 86 
Retrieving All the Rows from a Table .......................................................................................................... 89 
Retrieving a Particular Column Using ResultSet........................................................................................ 91 
Handling NULL Values...........................................................................................................................................94
Working with the ResultSetMetaData Interface...................................................................................................95
Describing ResultSetMetaData...................................................................................................................... 95 
Using ResultSetMetaData .............................................................................................................................. 96 
Creating SQLEditor in Java.....................................................................................................................................98
Creating the Command-Based SQLEditor................................................................................................... 98 
Creating the Windows-Based SQLEditor................................................................................................... 100 
Table of Contents
ix
Using Advanced Concepts of ResultSet ..............................................................................................................104
Exploring Characteristics of the ResultSet Object..................................................................................... 104 
ResultSet Types.................................................................................................................................... 104 
ResultSet Concurrency........................................................................................................................ 105 
ResultSet Holdability .......................................................................................................................... 106 
Setting the ResultSet Type and ResultSet Concurrency........................................................................... 106 
Using ResultSet Objects to Manipulate Data............................................................................................. 112 
Using the ResultSet Object to Update Data...................................................................................... 112 
Using the ResultSet Object to Delete a Row..................................................................................... 114 
Using a ResultSet Object to Insert a Row.......................................................................................... 116 
Closing the Connection..........................................................................................................................................118
Closing the Statement and ResultSet.......................................................................................................... 118 
Working with the DatabaseMetaData Interface .................................................................................................119
Retrieving Database and Driver Details..................................................................................................... 121 
Retrieving Table Description....................................................................................................................... 122 
Retrieving Column Details........................................................................................................................... 124 
Retrieving Data from a Microsoft Excel Sheet........................................................................................... 125 
Summary..................................................................................................................................................................128
Exercise ....................................................................................................................................................................129
Chapter 5: Describing Advanced JDBC Concepts.........................................131
Exploring the SQL 99 Data Types.........................................................................................................................132
The BLOB Data Type .................................................................................................................................... 132 
Storing BLOB Values........................................................................................................................... 133 
Reading BLOB Values......................................................................................................................... 135 
The CLOB Data Type.................................................................................................................................... 136 
Storing CLOB Values .......................................................................................................................... 137 
Reading CLOB Values......................................................................................................................... 138 
The Struct (Object) Data Type ..................................................................................................................... 140 
Using User-Defined Object Types in Java to Represent Database Object Types......................... 140 
Creating a JDBC UDT.......................................................................................................................... 141 
Mapping the JDBC UDT..................................................................................................................... 141 
Implementing UDT ............................................................................................................................. 141 
Using the java.sql.Struct Interface ..................................................................................................... 145 
The Array Data Type .................................................................................................................................... 146 
The Ref Data Type......................................................................................................................................... 151 
Working with the ROWID Type...........................................................................................................................151
Working with Batch Updates................................................................................................................................152
Using Batch Updates with the Statement Object ...................................................................................... 153 
The Batch Update Methods................................................................................................................ 153 
Table of Contents
x
The BatchUpdateException Exception.............................................................................................. 154 
Using Batch Updates with the PreparedStatement Object ...................................................................... 156 
Describing SQL/XML and XML support............................................................................................................157
Retrieving Auto Generated Keys..........................................................................................................................158
Working with RowSets in JDBC ...........................................................................................................................161
The Connected RowSets............................................................................................................................... 161 
The Disconnected RowSets.......................................................................................................................... 161 
Using the RowSet at Design Time............................................................................................................... 162 
Setting the Properties of the RowSet Instance ................................................................................. 162 
Working with RowSet Events ............................................................................................................ 162 
Using RowSets at Run Time ........................................................................................................................ 162 
Setting Parameter Values of a RowSet Object.................................................................................. 163 
Executing RowSets .............................................................................................................................. 163 
Traversing a RowSet............................................................................................................................ 163 
Exploring Connection Pooling..............................................................................................................................163
Working with Connection Pool ............................................................................................................................165
Working with Transactions...................................................................................................................................166
Describing ACID Properties ........................................................................................................................ 166 
Atomicity .............................................................................................................................................. 167 
Consistency........................................................................................................................................... 167 
Isolation................................................................................................................................................. 167 
Durability.............................................................................................................................................. 167 
Types of Transactions................................................................................................................................... 167 
Performing Transaction Management........................................................................................................ 168 
Describing the Wrapper Interface ........................................................................................................................171
The isWrapperFor() Method........................................................................................................................ 171 
The unwrap() Method .................................................................................................................................. 171 
Describing SQLException......................................................................................................................................171
Summary..................................................................................................................................................................173
Exercise ....................................................................................................................................................................174
Chapter 6: Understanding Java Servlet ..........................................................175
Understanding the Need for Server-Side Extensions ........................................................................................176
Introducing CGI......................................................................................................................................................176
Introducing Java Servlet ........................................................................................................................................178
Advantages of a Servlet................................................................................................................................ 178 
New Features of Servlet 2.5.......................................................................................................................... 178 
Exploring the Servlet Container ...........................................................................................................................180
Describing the Request Processing Mechanism........................................................................................ 180 
Describing the Types of Servlet Container Configurations..................................................................... 180 
Table of Contents
xi
The Standalone Container .................................................................................................................. 181 
The In-Process Container.................................................................................................................... 181 
The Out-Process Containers............................................................................................................... 182 
Introducing the Servlet API...................................................................................................................................182
Describing the javax.servlet Package.......................................................................................................... 183 
Describing the javax.servlet.http Package.................................................................................................. 184 
Implementing a Servlet Object..............................................................................................................................185
The javax.servlet.Servlet Interface............................................................................................................... 185 
The javax.servlet.ServletConfig Interface<H2>......................................................................................... 185 
Understanding the Servlet LifeCycle ...................................................................................................................186
Describing the Stages of the Servlet LifeCycle .......................................................................................... 186 
Loading a Servlet ................................................................................................................................. 186 
Initializing a Servlet............................................................................................................................. 187 
Handling Request................................................................................................................................ 187 
Destroying a Servlet (End of service)................................................................................................ 189 
Exploring the Servlet Life Cycle Methods ................................................................................................. 189 
The init () method ................................................................................................................................ 189 
The service( ) Method.......................................................................................................................... 189 
The destroy () Method......................................................................................................................... 190 
Developing the First Servlet Application ............................................................................................................190
Creating the home.html Page ...................................................................................................................... 190 
Creating the LifeCycleServlet...................................................................................................................... 191 
Creating the Deployment Descriptor ......................................................................................................... 191 
Deploying Web Application........................................................................................................................ 192 
Running the LifeCycle Application ............................................................................................................ 193 
Understanding the Request Processing Workflow ............................................................................................194
Request Navigation....................................................................................................................................... 195 
Request Delegation ....................................................................................................................................... 196 
Working with the GenericServlet Class...............................................................................................................197
Exploring Methods of the GenericServlet Class ...................................................................................... 198 
Using the GenericServlet Class .................................................................................................................. 199 
Creating the Home.html Page............................................................................................................ 199 
Developing the HelloServlet.java Servlet......................................................................................... 200 
Deploying and Running the Application ......................................................................................... 200 
Summary..................................................................................................................................................................201
Exercise ....................................................................................................................................................................202
Chapter 7: Understanding Request Processing and HTTP...........................203
Implementing the ServletRequest Interface........................................................................................................204
Managing the Servlet Object as a Throwaway Object.............................................................................. 204 
Table of Contents
xii
Managing the ServletRequest Object Pool................................................................................................. 204 
Working with Request Parameters............................................................................................................. 205 
Creating the Login Form..................................................................................................................... 206 
Creating the LoginServlet Servlet...................................................................................................... 206 
Creating the Deployment Descriptor File......................................................................................... 208 
Running the Application .................................................................................................................... 209 
Working with Initialization Parameters ..............................................................................................................210
Exploring the Need for Initialization Parameters..................................................................................... 211 
Retrieving Initialization Parameters........................................................................................................... 211 
Handling Servlet Initialization Parameters ............................................................................................... 212 
Creating the Home.html File.............................................................................................................. 212 
Creating the InitParamsServlet Servlet............................................................................................. 212 
Creating the Deployment Descriptor File......................................................................................... 213 
Running the Application .................................................................................................................... 214 
Working with Context Initialization Parameters................................................................................................216
Retrieving Context Initialization Parameters ............................................................................................ 216 
Using Context Initialization Parameters .................................................................................................... 216 
Creating the Home.html File.............................................................................................................. 217 
Creating the Login.html File .............................................................................................................. 217 
Creating the Register.html File .......................................................................................................... 218 
Creating the RegistrationServlet Servlet........................................................................................... 219 
Creating the LoginServlet Servlet...................................................................................................... 221 
Creating the Deployment Descriptor File......................................................................................... 223 
Running the Application .................................................................................................................... 225 
Understanding ServletResponse...........................................................................................................................227
Understanding Request Dispatching...................................................................................................................228
Navigation...................................................................................................................................................... 229 
Delegation ...................................................................................................................................................... 230 
Dispatching the Request........................................................................................................................................230
Getting a RequestDispatcher object............................................................................................................ 230 
The getRequestDispatcher() Method of ServletContext ................................................................. 230 
The getNamedDispatcher() Method of ServletContext .................................................................. 231 
The getRequestDispatcher() Method of ServletRequest................................................................. 231 
Comparing the getRequestDispatcher() Methods of ServletContext and ServletRequest......... 231 
Comparing the getRequestDispatcher() & getNamedDispatcher() Methods.............................. 232 
Understanding the include() and forward() Methods of RequestDispatcher ....................................... 232 
Describing the include() Method....................................................................................................... 232 
Describing the forward() Method...................................................................................................... 234 
Comparing the include() and forward() Methods........................................................................... 235 
Table of Contents
xiii
Considering the Situations to use the include() and forward() Methods..................................... 236 
Working with RequestDispatcher............................................................................................................... 236 
Creating the Home.html File.............................................................................................................. 236 
Creating the TestServlet1 Servlet....................................................................................................... 237 
Creating the TestServlet2 Servlet....................................................................................................... 238 
Creating the Deployment Descriptor File......................................................................................... 239 
Running the Application .................................................................................................................... 240 
Describing Request Attributes..............................................................................................................................242
Working with Request Attributes............................................................................................................... 242 
Creating the Home.html File.............................................................................................................. 243 
Creating the ValidationServlet Servlet.............................................................................................. 243 
Creating the AddServlet Servlet ........................................................................................................ 244 
Creating the SubServlet Servlet ......................................................................................................... 244 
Creating the ResponseServlet Servlet ............................................................................................... 245 
Creating the Deployment Descriptor File......................................................................................... 246 
Running the Application .................................................................................................................... 247 
Describing HTTP Basic ..........................................................................................................................................249
Describing HTTP Status Codes ................................................................................................................... 249 
1XX – Informational ............................................................................................................................ 249 
2XX – Success ....................................................................................................................................... 249 
3XX – Redirection ................................................................................................................................ 249 
4XX – Client Error................................................................................................................................ 250 
5XX – Server Error ............................................................................................................................... 250 
Describing the HttpServletRequest & HttpServletResponse Interfaces ..........................................................250
Working with HttpServletRequest ............................................................................................................. 251 
Working with HttpServletResponse........................................................................................................... 251 
Exploring the Methods for Response Headers ................................................................................ 252 
Exploring the Methods for Buffering................................................................................................ 253 
Comparing RequestDispatcher forward and sendRedirect........................................................... 253 
Handling the HttpServletRequest and HttpServletResponse Interfaces ............................................... 254 
Creating the Home.html File.............................................................................................................. 254 
Creating the HttpServletRequestEx Servlet ..................................................................................... 254 
Creating the Deployment Descriptor File......................................................................................... 255 
Running the Application .................................................................................................................... 256 
Describing HttpServlet...........................................................................................................................................258
The HttpServlet Lifecycle............................................................................................................................. 259 
Summary..................................................................................................................................................................259
Exercise ....................................................................................................................................................................260
Table of Contents
xiv
Chapter 8: Handling Sessions in Servlet ........................................................261
Introducing Session Tracking ...............................................................................................................................262
Describing URL Rewriting ....................................................................................................................................263
Using Query String for URL Rewriting...................................................................................................... 263 
Using Path Info for URL Rewriting ............................................................................................................ 263 
Listing the Disadvantages of URL Rewriting............................................................................................ 264 
Considering Situations to use URL Rewriting .......................................................................................... 264 
Exploring Hidden Form Field...............................................................................................................................264
Listing the Advantages and Disadvantages of Hidden Form Field ....................................................... 265 
Considering the Situations to use Hidden Form Fields ........................................................................... 265 
Working with URL Rewriting and Hidden Form Field ....................................................................................265
Creating the SiteHome.html Page............................................................................................................... 265 
Creating the Login.html Page...................................................................................................................... 266 
Creating the Send.html Page ....................................................................................................................... 266 
Creating the DriverConnection.java File.................................................................................................... 266 
Creating the UserDAO.java File.................................................................................................................. 267 
Creating the LoginServlet.java Servlet ....................................................................................................... 267 
Creating the Mail.java File ........................................................................................................................... 269 
Creating the MailsDAO.java File ................................................................................................................ 270 
Creating the InboxServlet.java Servlet ....................................................................................................... 271 
Creating the ViewMailServlet.java Servlet ................................................................................................ 272 
Creating the SendReplyServlet.java Servlet............................................................................................... 274 
Creating the Deployment Descriptor File.................................................................................................. 275 
Creating the Database Tables ...................................................................................................................... 276 
Creating the Directory Structure................................................................................................................. 277 
Running the Application.............................................................................................................................. 277 
Describing Cookies.................................................................................................................................................279
Listing the Advantages and Disadvantages of Cookies........................................................................... 280 
Considering the Situations to use Cookies ................................................................................................ 280 
Using Cookies .........................................................................................................................................................281
Creating the Home.html File ....................................................................................................................... 281 
Creating the AddCookieServlet.java File................................................................................................... 281 
Creating the GetCookieServlet.java File..................................................................................................... 282 
Creating the web.xml File ............................................................................................................................ 282 
Running the Application.............................................................................................................................. 283 
Exploring HttpSession ...........................................................................................................................................284
Creating a Session Object............................................................................................................................. 285 
Locating a Session Object Associated with the Current Request............................................................ 285 
Difference between the getSession() and getSession(Boolean) Methods...................................... 285 
Table of Contents
xv
Storing Information in a Session ................................................................................................................. 285 
Attaching the Session Identity to the URLs............................................................................................... 286 
Looking up Information in a Session.......................................................................................................... 287 
Discarding Completed or Abandoned Sessions........................................................................................ 287 
Listing the Advantages and Disadvantages of HttpSession.................................................................... 288 
Considering the Situations to use HttpSession ......................................................................................... 288 
Threading Issues of a Session............................................................................................................. 288 
Sessions in a Clustered Environment................................................................................................ 288 
Working with HttpSession....................................................................................................................................288
Creating the Login.html File........................................................................................................................ 289 
Creating the DriverConnection.java File.................................................................................................... 289 
Creating the UserDAO.java File.................................................................................................................. 290 
Creating the Product.java File ..................................................................................................................... 290 
Creating the ProductsDAO.java File .......................................................................................................... 291 
Creating the LoginServlet.java File............................................................................................................. 291 
Creating the UserHomeServlet.java File.................................................................................................... 292 
Creating the GetProductsServlet.java File ................................................................................................. 293 
Creating the AddProductServlet.java File ................................................................................................. 295 
Creating the GetCartDetailsServlet.java File............................................................................................. 297 
Creating the LogoutServlet.java File........................................................................................................... 298 
Creating the web.xml File ............................................................................................................................ 299 
Describing Servlet Context Attributes .................................................................................................................304
Context Attributes in a Distributed Container.......................................................................................... 304 
Differences Between Parameters and Attributes............................................................................. 304 
Describing Scopes of the Web Application Objects ...........................................................................................304
Exploring the Single Thread Model .....................................................................................................................305
Explaining the Need of Synchronization in Servlets................................................................................ 305 
Implementing SingleThreadModel............................................................................................................. 306 
Describing the ServletOutputStream Class.........................................................................................................307
Using ServletOutputStream......................................................................................................................... 307 
Summary..................................................................................................................................................................309
Exercise ....................................................................................................................................................................310
Chapter 9: Implementing Filters, Listeners, and Wrappers.......................... 311
Exploring Filters......................................................................................................................................................312 
Filter Interception Design Pattern............................................................................................................... 312 
Filter API ........................................................................................................................................................ 313 
The Filter Interface............................................................................................................................... 313 
The FilterConfig Interface................................................................................................................... 314 
The FilterChain Interface .................................................................................................................... 314 
Table of Contents
xvi
Working With Filters .................................................................................................................................... 314 
Creating the Home.html Page............................................................................................................ 315 
Creating the TestServlet.java File ...................................................................................................... 315 
Creating the MyFilter1.java File......................................................................................................... 315 
Creating the MyFilter2.java File......................................................................................................... 316 
Configuring the Filters Application .................................................................................................. 316 
Running the Application .................................................................................................................... 318 
Changes in Servlet 2.4 in Filter Configurations......................................................................................... 320 
Using Filters with the forward( ) and include( ) Methods ............................................................. 320 
Describing Listeners...............................................................................................................................................321 
Need of Event Programming and Importance of Listeners..................................................................... 321 
Types of Listeners ......................................................................................................................................... 321 
Describing the Listener Interfaces............................................................................................................... 322 
The ServletContextListener Interface................................................................................................ 323 
The ServletContextAttributeListener Interface................................................................................ 327 
The HttpSessionListener Interface..................................................................................................... 332 
The HttpSessionAttributeListener Interface .................................................................................... 332 
The HttpSessionBindingListener Interface....................................................................................... 337 
The HttpSessionActivationListener Interface .................................................................................. 340 
The ServletRequestListener Interface................................................................................................ 340 
The ServletRequestAttributeListener Interface................................................................................ 341 
Describing Wrappers .............................................................................................................................................341 
Need of Wrappers......................................................................................................................................... 341 
Types of Wrapper Classes............................................................................................................................ 341 
The ServletRequestWrapper Class .................................................................................................... 342
The ServletResponseWrapper Class.................................................................................................. 342
The HttpServletRequestWrapper Class............................................................................................ 343
The HttpServletResponseWrapper Class ......................................................................................... 343 
Using Wrappers............................................................................................................................................. 344 
Creating the Home.html Page............................................................................................................ 344 
Creating the TestServlet.java File ...................................................................................................... 344 
Creating the MyRequestWrapper.java File...................................................................................... 345 
Creating the MyResponseWrapper.java File.................................................................................... 345 
Creating the MyWrapperFilter.java File........................................................................................... 346 
Creating the Web.xml File.................................................................................................................. 346 
Deploying and Running the Application ......................................................................................... 347 
Applet to Servlet Communication........................................................................................................................348 
An Example Using Applet in Servlet.......................................................................................................... 348 
Summary..................................................................................................................................................................351 
Exercise ....................................................................................................................................................................351 
Table of Contents
xvii
Chapter 10: Introduction to JSP ..................................................................... 353
Understanding JSP ................................................................................................................................................. 354 
Advantages of JSP over Servlets ................................................................................................................. 355 
Introducing the Tag-Based Approach ........................................................................................................ 356 
Describing the JSP Architecture .................................................................................................................. 357 
JSP Model I architecture...................................................................................................................... 357 
Describing the JSP Life-Cycle................................................................................................................................ 358 
The Page Translation Stage.......................................................................................................................... 358 
The Compilation Stage ................................................................................................................................. 360 
The Loading & Initialization Stage ............................................................................................................. 360 
The Request Handling Stage........................................................................................................................ 361 
The Destroying Stage.................................................................................................................................... 361 
Creating Simple JSP Pages..................................................................................................................................... 361 
Summary.................................................................................................................................................................. 364 
Exercise .................................................................................................................................................................... 364 
Chapter 11: Working with JSP Basic Tags and Implicit Objects ................. 365
Exploring Scripting Tags ....................................................................................................................................... 366 
Types of Scripting Tags ................................................................................................................................ 366 
The Scriptlet Tag.................................................................................................................................. 366 
The Declarative Tag............................................................................................................................. 367 
The Expression Tag ............................................................................................................................. 367 
Working with JSP Scripting Tags................................................................................................................ 368 
Exploring Implicit Objects..................................................................................................................................... 370 
Features of Implicit Objects ......................................................................................................................... 370 
Types of Implicit Objects.............................................................................................................................. 370 
The request Object ............................................................................................................................... 371 
The response Object............................................................................................................................. 371 
The out Object ...................................................................................................................................... 371 
The page Object.................................................................................................................................... 371 
The pageContext Object...................................................................................................................... 371 
The application Object ........................................................................................................................ 371 
The session Object................................................................................................................................ 371 
The config Object ................................................................................................................................. 371 
The exception Object ........................................................................................................................... 371 
Working with Implicit Objects .................................................................................................................... 372 
Exploring Directive Tags ....................................................................................................................................... 376 
Types of Directive Tags................................................................................................................................ 377 
The Page Directive Tag ....................................................................................................................... 377 
The Include Directive.......................................................................................................................... 378 
Table of Contents
xviii
The Taglib Directive ............................................................................................................................ 378 
Working with JSP Directive Tags................................................................................................................ 379 
Summary.................................................................................................................................................................. 382 
Exercise .................................................................................................................................................................... 382 
Chapter 12: Working with JavaBeans and Action Tags in JSP ................... 383
What is JavaBean?..................................................................................................................................................384 
Advantages of using JavaBeans .................................................................................................................. 384 
Action Tags..............................................................................................................................................................384 
Describing the Include Tag .......................................................................................................................... 385 
Difference Between Include Directive and Include Action............................................................ 385 
Describing the Forward Tag ........................................................................................................................ 386 
Describing the Param Tag............................................................................................................................ 386 
name ...................................................................................................................................................... 387 
value ...................................................................................................................................................... 387 
Describing the useBean Tag......................................................................................................................... 390 
Describing the Attributes of the <jsp:useBean> Tag....................................................................... 390 
Using the <jsp:useBean> Tag Attributes .......................................................................................... 391 
Generating Servlets with the <jsp:useBean> Tag ............................................................................ 392 
Describing the setProperty Tag................................................................................................................... 393 
name ...................................................................................................................................................... 393 
property ................................................................................................................................................ 393 
value ...................................................................................................................................................... 394 
param .................................................................................................................................................... 394 
Describing the getProperty Tag................................................................................................................... 395 
The name Attribute ............................................................................................................................. 395 
The property Attribute........................................................................................................................ 395 
Plugin.............................................................................................................................................................. 395 
Params ............................................................................................................................................................ 396 
Fallback........................................................................................................................................................... 396 
Attribute ......................................................................................................................................................... 396 
Body................................................................................................................................................................ 397 
Element........................................................................................................................................................... 397 
Text.................................................................................................................................................................. 398 
Declaring a Bean in a JSP.......................................................................................................................................398 
Creating a Bean.............................................................................................................................................. 398 
Declaring the Bean in a JSP.......................................................................................................................... 399 
Accessing the Bean Properties..................................................................................................................... 399 
Generating Dynamic Content within a JSP................................................................................................ 400 
Deploying and Running Application ......................................................................................................... 401 
Table of Contents
xix
Summary..................................................................................................................................................................403 
Exercise ....................................................................................................................................................................404 
Chapter 13: Enhancing the JSP Tags Support .............................................. 405
Custom Tags............................................................................................................................................................406 
Need of Custom Tags ................................................................................................................................... 406 
Elements of Custom Tags............................................................................................................................. 407 
Tag Library Descriptor........................................................................................................................ 407 
Tag Handler.......................................................................................................................................... 408 
The Tag Extension API...........................................................................................................................................410 
Empty Tags..............................................................................................................................................................411 
Empty Tag Handler Life Cycle.................................................................................................................... 412 
Using Empty Tags......................................................................................................................................... 412 
Body Content Tag Interface...................................................................................................................................420 
Body Tag Handler Life Cycle ...................................................................................................................... 420 
Using BodyContent Tag .............................................................................................................................. 421 
Iteration tag Interface.............................................................................................................................................429 
Iteration Tag Handler Life Cycle................................................................................................................. 429 
Simple Tags .............................................................................................................................................................433 
Life Cycle of Simple Tag Handlers ............................................................................................................. 433 
Differences between Simple Tag and Classic Tag Handlers ................................................................... 435 
The TryCatchFinally Interface ..............................................................................................................................435 
Summary..................................................................................................................................................................436 
Exercise ....................................................................................................................................................................436 
Chapter 14: Understanding JSP Expression Language ............................... 437
Basic Syntax of using EL........................................................................................................................................438 
Types of EL Expressions............................................................................................................................... 438 
Immediate and Deferred Expressions............................................................................................... 439 
Value Expressions................................................................................................................................ 439 
Method Expressions............................................................................................................................ 440 
Tag Attribute Types...................................................................................................................................... 440 
Resolving EL Expressions ............................................................................................................................ 441 
Describing the EL Resolver Classes................................................................................................... 441 
EL Operators.................................................................................................................................................. 443 
Types of EL Operators ........................................................................................................................ 443 
An Example of Using EL Operators.................................................................................................. 444 
Implicit EL Objects........................................................................................................................................ 445 
Example 1 of Using Implicit EL Objects ........................................................................................... 447 
Example 2 of Using Implicit EL Objects ........................................................................................... 449 
Table of Contents
xx
Introducing Functions with EL.............................................................................................................................451 
Using EL Functions ................................................................................................................................................451 
Summary..................................................................................................................................................................453 
Exercise ....................................................................................................................................................................453 
Chapter 15: Implementing Internationalization ............................................. 455
Introducing Internationalization ..........................................................................................................................456 
Java and Internationalization................................................................................................................................456 
Describing the Locale Class ......................................................................................................................... 456 
Parameters of the Locale Object......................................................................................................... 457 
Using the Locale Class ........................................................................................................................ 457 
Describing the ResourceBundle Class ........................................................................................................ 458 
The PropertyResourceBundle Class.................................................................................................. 459 
The ListResourceBundle Class........................................................................................................... 461 
Internationalizing Web Applications...................................................................................................................463 
Creating the JSP Views................................................................................................................................. 464 
Creating the index.html View............................................................................................................ 464 
Creating the Home.jsp View .............................................................................................................. 464 
Creating the I18NServlet Class.................................................................................................................... 465 
Creating the Resource Bundles.................................................................................................................... 465 
Configuring the Application........................................................................................................................ 466 
Designing the Application Directory Structure ........................................................................................ 466 
Running the Application.............................................................................................................................. 467 
Summary..................................................................................................................................................................468 
Exercise ....................................................................................................................................................................468 
Chapter 16: Working with JSP Standard Tag Library (JSTL)....................... 469
Describing JSTL Core Tags....................................................................................................................................470 
General-Purpose Tags .................................................................................................................................. 470 
The <c:out> tag .................................................................................................................................... 470 
The <c:set> Tag .................................................................................................................................... 472 
The <c:remove> Tag............................................................................................................................ 473 
The <c:catch> Tag................................................................................................................................ 473 
Conditional and Looping Tags.................................................................................................................... 473 
Types of Conditional and Looping Tags .......................................................................................... 473 
Using Conditional and Looping Tags............................................................................................... 475 
Networking Tags........................................................................................................................................... 480 
The <c:import> Tag............................................................................................................................. 480 
The <c:url> Tag.................................................................................................................................... 480 
The <c:redirect> Tag............................................................................................................................ 481 
Table of Contents
xxi
The <c:param> Tag.............................................................................................................................. 481 
Describing the JSTL SQL Tags ..............................................................................................................................481 
Types of JSTL SQL Tags ............................................................................................................................... 481 
The <sql:query> Tag............................................................................................................................ 482 
The <sql:update> Tag ......................................................................................................................... 482 
The <sql:param> Tag .......................................................................................................................... 482 
The <sql:dateParam> Tag................................................................................................................... 483 
The <sql:setDataSource> Tag............................................................................................................. 483 
The <sql:transaction> Tag .................................................................................................................. 483 
Using JSTL SQL Tags.................................................................................................................................... 483 
JSTL Formatting Tags.............................................................................................................................................486 
Basic Formatting Tags .................................................................................................................................. 486 
Types of Basic Formatting Tags......................................................................................................... 486
Using Basic JSTL Formatting Tags .................................................................................................... 488 
Number Formatting Tags............................................................................................................................. 490 
Types of Number Formatting Tags ................................................................................................... 490 
Using Number Format Tags............................................................................................................... 491 
Date Formatting Tags ................................................................................................................................... 494 
Types of Date Formatting Tags.......................................................................................................... 494 
Using Date Formatting Tags .............................................................................................................. 495 
Time Zone Formatting Tags......................................................................................................................... 497 
Types of Time Zone Formatting Tags............................................................................................... 497
Using TimeZone Formatting Tags..................................................................................................... 497 
JSTL XML Tags .......................................................................................................................................................499 
XML Core Tags.............................................................................................................................................. 499 
The <x:parse> Tag ............................................................................................................................... 500 
The <x:out> Tag................................................................................................................................... 500 
The <x:set> Tag.................................................................................................................................... 500 
XML Flow Control Tags............................................................................................................................... 500 
The <x:if> Tag ...................................................................................................................................... 501
The <x:choose> Tag............................................................................................................................. 501
The <x:when> Tag............................................................................................................................... 501
The <x:otherwise> Tag........................................................................................................................ 501
The <x:forEach> tag ............................................................................................................................ 501 
XML Transformation Tags........................................................................................................................... 502 
The <x:transform> Tag........................................................................................................................ 502 
The <x:param> Tag ............................................................................................................................. 502 
Implementing JSTL Tags .......................................................................................................................................502 
Summary..................................................................................................................................................................506 
Exercise ....................................................................................................................................................................506 
Table of Contents
xxii
Chapter 17: Working with JavaServer Faces ................................................ 507
Introducing JSF .......................................................................................................................................................508 
Explaining the Features of JSF ..............................................................................................................................509 
Exploring the JSF Architecture..............................................................................................................................510 
Describing JSF Elements ........................................................................................................................................511 
UI Component ............................................................................................................................................... 511 
Renderer ......................................................................................................................................................... 512 
Validators....................................................................................................................................................... 513 
Backing Beans................................................................................................................................................ 514 
Converters...................................................................................................................................................... 514 
Events and Listeners..................................................................................................................................... 515 
Action Events ....................................................................................................................................... 515 
Value-change Events........................................................................................................................... 516 
Data Model Events .............................................................................................................................. 517 
Phase Events......................................................................................................................................... 517 
Message .......................................................................................................................................................... 517 
Navigation...................................................................................................................................................... 518 
Exploring the JSF Request Processing Life cycle ................................................................................................518 
The Restore View Phase ............................................................................................................................... 519 
The Apply Request Values Phase ............................................................................................................... 519 
The Process Validations Phase .................................................................................................................... 520 
The Update Model Values Phase ................................................................................................................ 520 
The Invoke Application Phase..................................................................................................................... 520 
The Render Response Phase ........................................................................................................................ 521 
Exploring JSF Tag Libraries...................................................................................................................................521 
JSF HTML Tags.............................................................................................................................................. 522 
The <h:commandButton> Tag ........................................................................................................... 522 
The <h:commandLink> Tag............................................................................................................... 523 
The <h:dataTable> Tag ....................................................................................................................... 524 
The <h:form> Tag................................................................................................................................ 525 
The <h:graphicImage > Tag ............................................................................................................... 526 
The <h:inputHidden> Tag.................................................................................................................. 526 
The <h:inputSecret> Tag..................................................................................................................... 527 
The <h:inputText> Tag ....................................................................................................................... 527 
The <h:inputTextarea> Tag................................................................................................................ 528 
The <h:message> Tag.......................................................................................................................... 528 
The <h:messages> Tag........................................................................................................................ 529 
The <h:outputFormat> Tag................................................................................................................ 530 
The <h:outputLabel> Tag................................................................................................................... 530 
Table of Contents
xxiii
The <h:outputLink> Tag..................................................................................................................... 530 
The <h:outputText> Tag..................................................................................................................... 531 
The <h:panelGrid> Tag....................................................................................................................... 531 
The <h:panelGroup> Tag ................................................................................................................... 532 
The <h:selectbooleanCheckbox> Tag................................................................................................ 533 
The <h:selectManyCheckbox> Tag ................................................................................................... 534 
The <h:selectManyListbox> Tag........................................................................................................ 535 
The <h:selectManyMenu> Tag .......................................................................................................... 535 
The <h:selectOneListbox> Tag........................................................................................................... 536 
The <h:selectOneMenu> Tag ............................................................................................................. 537 
The <h:selectOneRadio> Tag ............................................................................................................. 538 
The <h:column> Tag ........................................................................................................................... 539 
JSF Core Tags................................................................................................................................................. 540 
The <f:actionListener> Tag................................................................................................................. 541 
The <f:valueChangeListener> Tag .................................................................................................... 541 
The <f:view> Tag................................................................................................................................. 541 
The <f:attribute> Tag........................................................................................................................... 542 
The <f:converter> Tag......................................................................................................................... 542 
The <f:convertDateTime> Tag ........................................................................................................... 543 
The <f:convertNumber> Tag.............................................................................................................. 544 
The <f:facet> Tag ................................................................................................................................. 544 
The <f:loadBundle> Tag ..................................................................................................................... 545 
The <f:param> Tag .............................................................................................................................. 545 
The <f:selectItem> Tag........................................................................................................................ 546 
The <f:selectItems> Tag ...................................................................................................................... 546 
The <f:subview> Tag........................................................................................................................... 547 
The <f:validateDoubleRange> Tag.................................................................................................... 548 
The <f:validateLength> Tag............................................................................................................... 548 
The <f:validateLongRange> Tag ....................................................................................................... 548 
The <f:validator> Tag.......................................................................................................................... 549 
The <f:verbatim> Tag.......................................................................................................................... 549 
The <f:phaseListener> Tag ................................................................................................................. 550 
The <f:setPropertyActionListener> Tag ........................................................................................... 551 
Exploring Backing Beans .......................................................................................................................................551 
An Example of Using a Backing Bean ........................................................................................................ 551 
Using a Backing Bean Method as an Event Handler................................................................................ 553 
Working with Managed Beans.................................................................................................................... 554 
JSF Input Validation...............................................................................................................................................555 
Using the Validator Method ........................................................................................................................ 556 
Table of Contents
xxiv
Using Validators............................................................................................................................................ 556 
Standard JSF Validators...................................................................................................................... 556 
JSF Type Conversion..............................................................................................................................................557 
Handling Page Navigation in JSF.........................................................................................................................558 
Configuring JSF Applications ...............................................................................................................................559 
Setting the web.xml File ............................................................................................................................... 559 
Configuring the faces-config.xml File......................................................................................................... 560 
Creating a Simple JSF Application .......................................................................................................................561 
Creating the views..................................................................................................................................................562 
Creating Backing Bean.................................................................................................................................. 564 
Configuring the Application........................................................................................................................ 565 
Summary..................................................................................................................................................................568 
Exercise ....................................................................................................................................................................568 
Chapter 18: Java EE Design Patterns ............................................................ 569
Describing the Java EE Application Architecture ..............................................................................................570 
Introducing a Design Pattern................................................................................................................................571 
Discussing the Role of Design Patterns ...............................................................................................................571 
Exploring Types of Patterns..................................................................................................................................571 
The Front Controller Pattern........................................................................................................................ 574 
Problem................................................................................................................................................. 574 
Forces..................................................................................................................................................... 574 
Solution ................................................................................................................................................. 574 
Strategies............................................................................................................................................... 576 
Consequences....................................................................................................................................... 576 
The Composite View Pattern....................................................................................................................... 577 
Problem................................................................................................................................................. 577 
Forces..................................................................................................................................................... 577 
Solution ................................................................................................................................................. 577 
Strategies............................................................................................................................................... 578 
Consequences....................................................................................................................................... 579 
The Composite Entity Pattern ..................................................................................................................... 579 
Problem................................................................................................................................................. 579 
Forces..................................................................................................................................................... 579 
Solution ................................................................................................................................................. 579 
Strategies............................................................................................................................................... 581 
Consequences....................................................................................................................................... 581 
The Intercepting Filter Pattern .................................................................................................................... 582 
Problem................................................................................................................................................. 582 
Forces..................................................................................................................................................... 582 
Table of Contents
xxv
Solution ................................................................................................................................................. 582 
Strategies............................................................................................................................................... 583 
Consequences....................................................................................................................................... 583 
The Transfer Object Pattern ......................................................................................................................... 583 
Problem................................................................................................................................................. 584 
Forces..................................................................................................................................................... 584 
Solution ................................................................................................................................................. 584 
Strategies............................................................................................................................................... 585 
Consequences....................................................................................................................................... 585 
The Session Facade Pattern.......................................................................................................................... 586 
Problem................................................................................................................................................. 586 
Forces..................................................................................................................................................... 586 
Solution ................................................................................................................................................. 586 
Strategies............................................................................................................................................... 587 
Consequences....................................................................................................................................... 587 
The Service Locator Pattern ......................................................................................................................... 588 
Problem................................................................................................................................................. 588 
Forces..................................................................................................................................................... 588 
Solution ................................................................................................................................................. 588 
Strategies............................................................................................................................................... 589 
Consequences....................................................................................................................................... 590 
The Data Access Object Pattern................................................................................................................... 590 
Problem................................................................................................................................................. 590 
Forces..................................................................................................................................................... 590 
Solution ................................................................................................................................................. 590 
Strategies............................................................................................................................................... 592 
Consequences....................................................................................................................................... 592 
The View Helper Pattern.............................................................................................................................. 592 
Problem................................................................................................................................................. 592 
Forces..................................................................................................................................................... 592 
Solution ................................................................................................................................................. 593 
Strategies............................................................................................................................................... 593 
Consequences....................................................................................................................................... 594 
The Dispatcher View Pattern....................................................................................................................... 594 
Problem................................................................................................................................................. 594 
Forces..................................................................................................................................................... 594 
Solution ................................................................................................................................................. 594 
Strategies............................................................................................................................................... 595 
Consequences....................................................................................................................................... 596 
Table of Contents
xxvi
The Service To Worker Pattern.................................................................................................................... 596 
Problem................................................................................................................................................. 596 
Forces..................................................................................................................................................... 596 
Solution ................................................................................................................................................. 596 
Strategies............................................................................................................................................... 597 
Consequences....................................................................................................................................... 597 
Summary..................................................................................................................................................................598 
Exercise ....................................................................................................................................................................598 
Chapter 19: Implementing Security in Web Applications............................. 599
Exploring JAAS.......................................................................................................................................................600 
Implementing Authentication with JAAS.................................................................................................. 600 
Describing Authorization in Web Applications........................................................................................ 602 
Implementing Data Integrity with JAAS API............................................................................................ 602 
Exploring HTTP Authentication Mechanisms....................................................................................................602 
Describing HTTP Basic Authentication...................................................................................................... 602 
Describing Form-Based Authentication..................................................................................................... 603 
Describing Client-Certificate Authentication ............................................................................................ 603 
Describing HTTP Digest Authentication ................................................................................................... 604 
Securing Web Applications...................................................................................................................................604 
Describing Declarative Security .................................................................................................................. 604 
Using Declarative Security ................................................................................................................. 604 
Using Form-based Authentication for Declarative Security .......................................................... 612 
Describing Programmatic Security ............................................................................................................. 615 
Using Programmatic Security ............................................................................................................ 616 
Summary..................................................................................................................................................................626 
Exercise ....................................................................................................................................................................626 
Appendix A: Introduction to DBMS................................................................ 627
Appendix B: Hospital Management System .................................................. 637
Answers............................................................................................................ 641
Index ................................................................................................................. 651
What’s on CD-ROM.......................................................................................... 657

adj

  • 1.
    Table of Contents Introduction..............................................................................................…….xxix Chapter1: Getting Started with Web Applications in Java ...............................1 Introduction to Web Applications............................................................................................................................2 Benefits of Web Applications....................................................................................................................................5 Technologies used in Web Applications..................................................................................................................5 Describing JDBC................................................................................................................................................ 5 Exploring the JDBC Architecture........................................................................................................... 6  Describing Java Servlets................................................................................................................................... 7 Advantages of Java Servlets................................................................................................................... 8  Describing JSP.................................................................................................................................................... 8 Request Processing of a JSP Page .......................................................................................................... 9 Describing the JSP Life-cycle.................................................................................................................. 9  Introduction to Web Architecture Models ............................................................................................................10 Describing the Model-1 Architecture ........................................................................................................... 10 Limitations of the Model-1 Architecture ............................................................................................ 11  Describing the Model-2 Architecture ........................................................................................................... 11 Advantages of the Model-2 Architecture ........................................................................................... 12  Introduction to the MVC Architecture...................................................................................................................12 Describing the Model Component................................................................................................................ 13 Describing the View Component.................................................................................................................. 13 Describing the Controller Component ......................................................................................................... 13 Summary....................................................................................................................................................................14 Exercise ......................................................................................................................................................................14 Chapter 2: Getting Started with JDBC ..............................................................15 Introducing ODBC....................................................................................................................................................16 Defining ODBC.........................................................................................................................................................16 Listing Features of ODBC .............................................................................................................................. 17
  • 2.
    Table of Contents vi Describingthe ODBC Components .............................................................................................................. 17 The Application Component................................................................................................................ 18  The DriverManager Component ......................................................................................................... 18  The Driver Component......................................................................................................................... 18  The Datasource Component................................................................................................................. 19  Describing the ODBC Architecture............................................................................................................... 19 The Single-tier ODBC Architecture..................................................................................................... 19  The Multiple-tier ODBC Architecture................................................................................................. 20  Introducing JDBC .....................................................................................................................................................20 Describing Components of JDBC.................................................................................................................. 22 Listing Features of JDBC ................................................................................................................................ 22 Listing New Features in JDBC 4.0................................................................................................................. 24 Exploring the JDBC Architecture............................................................................................................................25 The Type-1 Driver........................................................................................................................................... 26 Advantages of the Type-1 Driver ........................................................................................................ 27  Disadvantages of the Type-1 Driver ................................................................................................... 27  Situations to Use the Type-1 Driver .................................................................................................... 27  The Type-2 Driver (Java to native API)........................................................................................................ 27 Advantages of the Type-2 Driver ........................................................................................................ 28  Disadvantages of the Type-2 Driver ................................................................................................... 28  Examples of the Type-2 Driver ............................................................................................................ 28  Situations to Use the Type-2 Driver .................................................................................................... 28  The Type-3 Driver (Java to Network Protocol/All Java Driver)............................................................... 29 Advantages of the Type-3 Driver ........................................................................................................ 29  Disadvantages of the Type-3 Driver ................................................................................................... 29  Examples of the Type-3 Driver ............................................................................................................ 29  Situations to Use the Type-3 Driver .................................................................................................... 30  The Type-4 Driver (Java to Database protocol)........................................................................................... 30 Advantages of the Type-4 Driver ........................................................................................................ 30  Disadvantage of the Type-4 Driver ..................................................................................................... 30  Examples of the Type-4 Driver ............................................................................................................ 31  Summary....................................................................................................................................................................31 Exercise ......................................................................................................................................................................32 State True or False ....................................................................................................................................................32  Chapter 3: Working with JDBC APIs.................................................................33 Introducing JDBC API..............................................................................................................................................34 Explaining the Types of JDBC APIs.............................................................................................................. 35  The JDBC Core API ............................................................................................................................... 35  The JDBC Extension API....................................................................................................................... 36 
  • 3.
    Table of Contents vii Listingthe New APIs Added to JDBC 4.0.................................................................................................... 37  Describing the Major Classes and Interfaces............................................................................................... 38  The DriverManager Class..................................................................................................................... 38  The Driver Interface .............................................................................................................................. 39  The Connection Interface...................................................................................................................... 40  The Statement Interface ........................................................................................................................ 42  Communicating with Databases by using JDBC APIs.........................................................................................45 Step 1: Obtaining a Connection..................................................................................................................... 46  Obtaining an Connection using Driver object.................................................................................... 46  Step 1.i: Registering a Driver object with DriverManager................................................................ 47  Step 1.ii: Establishing a Connection using DriverManager.............................................................. 48  Step 2: Creating a JDBC Statement Object ................................................................................................... 49  Step 3: Executing SQL Statements................................................................................................................. 49  Step 4: Closing the Connection...................................................................................................................... 49  Creating a Simple Application................................................................................................................................50 Configuring the Application.......................................................................................................................... 51  Creating a Table..................................................................................................................................... 51  Creating a DSN................................................................................................................................................ 51  Understanding Various JDBC Drivers...................................................................................................................53 The OCI8 Driver for Oracle............................................................................................................................ 54  The IDS Driver................................................................................................................................................. 54  Installing the IDS Server ....................................................................................................................... 54  Configuring IDS Driver ........................................................................................................................ 57  The Thin driver for Oracle ............................................................................................................................. 57  Summary....................................................................................................................................................................58 Exercise ......................................................................................................................................................................58 Fill in the blanks with appropriate words or phrases:.........................................................................................58 Chapter 4: Implementing JDBC Statements and ResultSets ..........................59 Describing JDBC Statement.....................................................................................................................................60 Working with the Statement Interface...................................................................................................................60 Exploring the Methods of Statement ............................................................................................................ 60  The executeUpdate (String sqlStatement) Method............................................................................ 61  The executeQuery (String query) Method.......................................................................................... 61  The execute (String sqlStatement) Method......................................................................................... 61  The getResultSet () Method .................................................................................................................. 62  The getUpdateCount () Method .......................................................................................................... 62  Using Statement .............................................................................................................................................. 62  Creating a Table..................................................................................................................................... 62  Inserting a Row...................................................................................................................................... 64 
  • 4.
    Table of Contents viii Workingwith the PreparedStatement Interface...................................................................................................65 Comparing Statement and PreparedStatement Objects............................................................................. 66  Describing the setXXX methods of PreparedStatement............................................................................. 67  Listing the Advantages and Disadvantages of PreparedStatement ......................................................... 69  Using PreparedStatement .............................................................................................................................. 69  Creating a PreparedStatement Object................................................................................................. 69  Setting the Values for Parameters........................................................................................................ 70  Executing the PreparedStatement ....................................................................................................... 70  Using PreparedStatement with Parameters................................................................................................. 70  Exploring the Usage of the PreparedStatement Object .............................................................................. 73  Working with the CallableStatement Interface.....................................................................................................73 Describing Stored Procedures ....................................................................................................................... 73  Listing the Benefits of Working with a Stored Procedure ......................................................................... 74  Using CallableStatement ................................................................................................................................ 74  Creating the CallableStatement object ................................................................................................ 74  Setting the Values of the Parameters................................................................................................... 75  Registering the OUT Parameter Type................................................................................................. 75  Executing the Stored Procedure or Function ..................................................................................... 75  Retrieving the Parameter Values ......................................................................................................... 75  Using CallableStatement with Parameters .................................................................................................. 75  Executing a Stored Procedure with IN Parameters........................................................................... 75  Executing a Stored Procedure with OUT Parameters....................................................................... 77  Calling Functions by Using CallableStatement........................................................................................... 79  Using Cursors in CallableStatement............................................................................................................. 81  Comparing Procedure and Function ............................................................................................................ 82  Working with the ResultSet Interface ....................................................................................................................82 Describing the Methods of ResultSet............................................................................................................ 83  Using ResultSet................................................................................................................................................ 85  Moving the Cursor Position ................................................................................................................. 86  Reading the Column Values................................................................................................................. 86  Retrieving All the Rows from a Table .......................................................................................................... 89  Retrieving a Particular Column Using ResultSet........................................................................................ 91  Handling NULL Values...........................................................................................................................................94 Working with the ResultSetMetaData Interface...................................................................................................95 Describing ResultSetMetaData...................................................................................................................... 95  Using ResultSetMetaData .............................................................................................................................. 96  Creating SQLEditor in Java.....................................................................................................................................98 Creating the Command-Based SQLEditor................................................................................................... 98  Creating the Windows-Based SQLEditor................................................................................................... 100 
  • 5.
    Table of Contents ix UsingAdvanced Concepts of ResultSet ..............................................................................................................104 Exploring Characteristics of the ResultSet Object..................................................................................... 104  ResultSet Types.................................................................................................................................... 104  ResultSet Concurrency........................................................................................................................ 105  ResultSet Holdability .......................................................................................................................... 106  Setting the ResultSet Type and ResultSet Concurrency........................................................................... 106  Using ResultSet Objects to Manipulate Data............................................................................................. 112  Using the ResultSet Object to Update Data...................................................................................... 112  Using the ResultSet Object to Delete a Row..................................................................................... 114  Using a ResultSet Object to Insert a Row.......................................................................................... 116  Closing the Connection..........................................................................................................................................118 Closing the Statement and ResultSet.......................................................................................................... 118  Working with the DatabaseMetaData Interface .................................................................................................119 Retrieving Database and Driver Details..................................................................................................... 121  Retrieving Table Description....................................................................................................................... 122  Retrieving Column Details........................................................................................................................... 124  Retrieving Data from a Microsoft Excel Sheet........................................................................................... 125  Summary..................................................................................................................................................................128 Exercise ....................................................................................................................................................................129 Chapter 5: Describing Advanced JDBC Concepts.........................................131 Exploring the SQL 99 Data Types.........................................................................................................................132 The BLOB Data Type .................................................................................................................................... 132  Storing BLOB Values........................................................................................................................... 133  Reading BLOB Values......................................................................................................................... 135  The CLOB Data Type.................................................................................................................................... 136  Storing CLOB Values .......................................................................................................................... 137  Reading CLOB Values......................................................................................................................... 138  The Struct (Object) Data Type ..................................................................................................................... 140  Using User-Defined Object Types in Java to Represent Database Object Types......................... 140  Creating a JDBC UDT.......................................................................................................................... 141  Mapping the JDBC UDT..................................................................................................................... 141  Implementing UDT ............................................................................................................................. 141  Using the java.sql.Struct Interface ..................................................................................................... 145  The Array Data Type .................................................................................................................................... 146  The Ref Data Type......................................................................................................................................... 151  Working with the ROWID Type...........................................................................................................................151 Working with Batch Updates................................................................................................................................152 Using Batch Updates with the Statement Object ...................................................................................... 153  The Batch Update Methods................................................................................................................ 153 
  • 6.
    Table of Contents x TheBatchUpdateException Exception.............................................................................................. 154  Using Batch Updates with the PreparedStatement Object ...................................................................... 156  Describing SQL/XML and XML support............................................................................................................157 Retrieving Auto Generated Keys..........................................................................................................................158 Working with RowSets in JDBC ...........................................................................................................................161 The Connected RowSets............................................................................................................................... 161  The Disconnected RowSets.......................................................................................................................... 161  Using the RowSet at Design Time............................................................................................................... 162  Setting the Properties of the RowSet Instance ................................................................................. 162  Working with RowSet Events ............................................................................................................ 162  Using RowSets at Run Time ........................................................................................................................ 162  Setting Parameter Values of a RowSet Object.................................................................................. 163  Executing RowSets .............................................................................................................................. 163  Traversing a RowSet............................................................................................................................ 163  Exploring Connection Pooling..............................................................................................................................163 Working with Connection Pool ............................................................................................................................165 Working with Transactions...................................................................................................................................166 Describing ACID Properties ........................................................................................................................ 166  Atomicity .............................................................................................................................................. 167  Consistency........................................................................................................................................... 167  Isolation................................................................................................................................................. 167  Durability.............................................................................................................................................. 167  Types of Transactions................................................................................................................................... 167  Performing Transaction Management........................................................................................................ 168  Describing the Wrapper Interface ........................................................................................................................171 The isWrapperFor() Method........................................................................................................................ 171  The unwrap() Method .................................................................................................................................. 171  Describing SQLException......................................................................................................................................171 Summary..................................................................................................................................................................173 Exercise ....................................................................................................................................................................174 Chapter 6: Understanding Java Servlet ..........................................................175 Understanding the Need for Server-Side Extensions ........................................................................................176 Introducing CGI......................................................................................................................................................176 Introducing Java Servlet ........................................................................................................................................178 Advantages of a Servlet................................................................................................................................ 178  New Features of Servlet 2.5.......................................................................................................................... 178  Exploring the Servlet Container ...........................................................................................................................180 Describing the Request Processing Mechanism........................................................................................ 180  Describing the Types of Servlet Container Configurations..................................................................... 180 
  • 7.
    Table of Contents xi TheStandalone Container .................................................................................................................. 181  The In-Process Container.................................................................................................................... 181  The Out-Process Containers............................................................................................................... 182  Introducing the Servlet API...................................................................................................................................182 Describing the javax.servlet Package.......................................................................................................... 183  Describing the javax.servlet.http Package.................................................................................................. 184  Implementing a Servlet Object..............................................................................................................................185 The javax.servlet.Servlet Interface............................................................................................................... 185  The javax.servlet.ServletConfig Interface<H2>......................................................................................... 185  Understanding the Servlet LifeCycle ...................................................................................................................186 Describing the Stages of the Servlet LifeCycle .......................................................................................... 186  Loading a Servlet ................................................................................................................................. 186  Initializing a Servlet............................................................................................................................. 187  Handling Request................................................................................................................................ 187  Destroying a Servlet (End of service)................................................................................................ 189  Exploring the Servlet Life Cycle Methods ................................................................................................. 189  The init () method ................................................................................................................................ 189  The service( ) Method.......................................................................................................................... 189  The destroy () Method......................................................................................................................... 190  Developing the First Servlet Application ............................................................................................................190 Creating the home.html Page ...................................................................................................................... 190  Creating the LifeCycleServlet...................................................................................................................... 191  Creating the Deployment Descriptor ......................................................................................................... 191  Deploying Web Application........................................................................................................................ 192  Running the LifeCycle Application ............................................................................................................ 193  Understanding the Request Processing Workflow ............................................................................................194 Request Navigation....................................................................................................................................... 195  Request Delegation ....................................................................................................................................... 196  Working with the GenericServlet Class...............................................................................................................197 Exploring Methods of the GenericServlet Class ...................................................................................... 198  Using the GenericServlet Class .................................................................................................................. 199  Creating the Home.html Page............................................................................................................ 199  Developing the HelloServlet.java Servlet......................................................................................... 200  Deploying and Running the Application ......................................................................................... 200  Summary..................................................................................................................................................................201 Exercise ....................................................................................................................................................................202 Chapter 7: Understanding Request Processing and HTTP...........................203 Implementing the ServletRequest Interface........................................................................................................204 Managing the Servlet Object as a Throwaway Object.............................................................................. 204 
  • 8.
    Table of Contents xii Managingthe ServletRequest Object Pool................................................................................................. 204  Working with Request Parameters............................................................................................................. 205  Creating the Login Form..................................................................................................................... 206  Creating the LoginServlet Servlet...................................................................................................... 206  Creating the Deployment Descriptor File......................................................................................... 208  Running the Application .................................................................................................................... 209  Working with Initialization Parameters ..............................................................................................................210 Exploring the Need for Initialization Parameters..................................................................................... 211  Retrieving Initialization Parameters........................................................................................................... 211  Handling Servlet Initialization Parameters ............................................................................................... 212  Creating the Home.html File.............................................................................................................. 212  Creating the InitParamsServlet Servlet............................................................................................. 212  Creating the Deployment Descriptor File......................................................................................... 213  Running the Application .................................................................................................................... 214  Working with Context Initialization Parameters................................................................................................216 Retrieving Context Initialization Parameters ............................................................................................ 216  Using Context Initialization Parameters .................................................................................................... 216  Creating the Home.html File.............................................................................................................. 217  Creating the Login.html File .............................................................................................................. 217  Creating the Register.html File .......................................................................................................... 218  Creating the RegistrationServlet Servlet........................................................................................... 219  Creating the LoginServlet Servlet...................................................................................................... 221  Creating the Deployment Descriptor File......................................................................................... 223  Running the Application .................................................................................................................... 225  Understanding ServletResponse...........................................................................................................................227 Understanding Request Dispatching...................................................................................................................228 Navigation...................................................................................................................................................... 229  Delegation ...................................................................................................................................................... 230  Dispatching the Request........................................................................................................................................230 Getting a RequestDispatcher object............................................................................................................ 230  The getRequestDispatcher() Method of ServletContext ................................................................. 230  The getNamedDispatcher() Method of ServletContext .................................................................. 231  The getRequestDispatcher() Method of ServletRequest................................................................. 231  Comparing the getRequestDispatcher() Methods of ServletContext and ServletRequest......... 231  Comparing the getRequestDispatcher() & getNamedDispatcher() Methods.............................. 232  Understanding the include() and forward() Methods of RequestDispatcher ....................................... 232  Describing the include() Method....................................................................................................... 232  Describing the forward() Method...................................................................................................... 234  Comparing the include() and forward() Methods........................................................................... 235 
  • 9.
    Table of Contents xiii Consideringthe Situations to use the include() and forward() Methods..................................... 236  Working with RequestDispatcher............................................................................................................... 236  Creating the Home.html File.............................................................................................................. 236  Creating the TestServlet1 Servlet....................................................................................................... 237  Creating the TestServlet2 Servlet....................................................................................................... 238  Creating the Deployment Descriptor File......................................................................................... 239  Running the Application .................................................................................................................... 240  Describing Request Attributes..............................................................................................................................242 Working with Request Attributes............................................................................................................... 242  Creating the Home.html File.............................................................................................................. 243  Creating the ValidationServlet Servlet.............................................................................................. 243  Creating the AddServlet Servlet ........................................................................................................ 244  Creating the SubServlet Servlet ......................................................................................................... 244  Creating the ResponseServlet Servlet ............................................................................................... 245  Creating the Deployment Descriptor File......................................................................................... 246  Running the Application .................................................................................................................... 247  Describing HTTP Basic ..........................................................................................................................................249 Describing HTTP Status Codes ................................................................................................................... 249  1XX – Informational ............................................................................................................................ 249  2XX – Success ....................................................................................................................................... 249  3XX – Redirection ................................................................................................................................ 249  4XX – Client Error................................................................................................................................ 250  5XX – Server Error ............................................................................................................................... 250  Describing the HttpServletRequest & HttpServletResponse Interfaces ..........................................................250 Working with HttpServletRequest ............................................................................................................. 251  Working with HttpServletResponse........................................................................................................... 251  Exploring the Methods for Response Headers ................................................................................ 252  Exploring the Methods for Buffering................................................................................................ 253  Comparing RequestDispatcher forward and sendRedirect........................................................... 253  Handling the HttpServletRequest and HttpServletResponse Interfaces ............................................... 254  Creating the Home.html File.............................................................................................................. 254  Creating the HttpServletRequestEx Servlet ..................................................................................... 254  Creating the Deployment Descriptor File......................................................................................... 255  Running the Application .................................................................................................................... 256  Describing HttpServlet...........................................................................................................................................258 The HttpServlet Lifecycle............................................................................................................................. 259  Summary..................................................................................................................................................................259 Exercise ....................................................................................................................................................................260
  • 10.
    Table of Contents xiv Chapter8: Handling Sessions in Servlet ........................................................261 Introducing Session Tracking ...............................................................................................................................262 Describing URL Rewriting ....................................................................................................................................263 Using Query String for URL Rewriting...................................................................................................... 263  Using Path Info for URL Rewriting ............................................................................................................ 263  Listing the Disadvantages of URL Rewriting............................................................................................ 264  Considering Situations to use URL Rewriting .......................................................................................... 264  Exploring Hidden Form Field...............................................................................................................................264 Listing the Advantages and Disadvantages of Hidden Form Field ....................................................... 265  Considering the Situations to use Hidden Form Fields ........................................................................... 265  Working with URL Rewriting and Hidden Form Field ....................................................................................265 Creating the SiteHome.html Page............................................................................................................... 265  Creating the Login.html Page...................................................................................................................... 266  Creating the Send.html Page ....................................................................................................................... 266  Creating the DriverConnection.java File.................................................................................................... 266  Creating the UserDAO.java File.................................................................................................................. 267  Creating the LoginServlet.java Servlet ....................................................................................................... 267  Creating the Mail.java File ........................................................................................................................... 269  Creating the MailsDAO.java File ................................................................................................................ 270  Creating the InboxServlet.java Servlet ....................................................................................................... 271  Creating the ViewMailServlet.java Servlet ................................................................................................ 272  Creating the SendReplyServlet.java Servlet............................................................................................... 274  Creating the Deployment Descriptor File.................................................................................................. 275  Creating the Database Tables ...................................................................................................................... 276  Creating the Directory Structure................................................................................................................. 277  Running the Application.............................................................................................................................. 277  Describing Cookies.................................................................................................................................................279 Listing the Advantages and Disadvantages of Cookies........................................................................... 280  Considering the Situations to use Cookies ................................................................................................ 280  Using Cookies .........................................................................................................................................................281 Creating the Home.html File ....................................................................................................................... 281  Creating the AddCookieServlet.java File................................................................................................... 281  Creating the GetCookieServlet.java File..................................................................................................... 282  Creating the web.xml File ............................................................................................................................ 282  Running the Application.............................................................................................................................. 283  Exploring HttpSession ...........................................................................................................................................284 Creating a Session Object............................................................................................................................. 285  Locating a Session Object Associated with the Current Request............................................................ 285  Difference between the getSession() and getSession(Boolean) Methods...................................... 285 
  • 11.
    Table of Contents xv StoringInformation in a Session ................................................................................................................. 285  Attaching the Session Identity to the URLs............................................................................................... 286  Looking up Information in a Session.......................................................................................................... 287  Discarding Completed or Abandoned Sessions........................................................................................ 287  Listing the Advantages and Disadvantages of HttpSession.................................................................... 288  Considering the Situations to use HttpSession ......................................................................................... 288  Threading Issues of a Session............................................................................................................. 288  Sessions in a Clustered Environment................................................................................................ 288  Working with HttpSession....................................................................................................................................288 Creating the Login.html File........................................................................................................................ 289  Creating the DriverConnection.java File.................................................................................................... 289  Creating the UserDAO.java File.................................................................................................................. 290  Creating the Product.java File ..................................................................................................................... 290  Creating the ProductsDAO.java File .......................................................................................................... 291  Creating the LoginServlet.java File............................................................................................................. 291  Creating the UserHomeServlet.java File.................................................................................................... 292  Creating the GetProductsServlet.java File ................................................................................................. 293  Creating the AddProductServlet.java File ................................................................................................. 295  Creating the GetCartDetailsServlet.java File............................................................................................. 297  Creating the LogoutServlet.java File........................................................................................................... 298  Creating the web.xml File ............................................................................................................................ 299  Describing Servlet Context Attributes .................................................................................................................304 Context Attributes in a Distributed Container.......................................................................................... 304  Differences Between Parameters and Attributes............................................................................. 304  Describing Scopes of the Web Application Objects ...........................................................................................304 Exploring the Single Thread Model .....................................................................................................................305 Explaining the Need of Synchronization in Servlets................................................................................ 305  Implementing SingleThreadModel............................................................................................................. 306  Describing the ServletOutputStream Class.........................................................................................................307 Using ServletOutputStream......................................................................................................................... 307  Summary..................................................................................................................................................................309 Exercise ....................................................................................................................................................................310 Chapter 9: Implementing Filters, Listeners, and Wrappers.......................... 311 Exploring Filters......................................................................................................................................................312  Filter Interception Design Pattern............................................................................................................... 312  Filter API ........................................................................................................................................................ 313  The Filter Interface............................................................................................................................... 313  The FilterConfig Interface................................................................................................................... 314  The FilterChain Interface .................................................................................................................... 314 
  • 12.
    Table of Contents xvi WorkingWith Filters .................................................................................................................................... 314  Creating the Home.html Page............................................................................................................ 315  Creating the TestServlet.java File ...................................................................................................... 315  Creating the MyFilter1.java File......................................................................................................... 315  Creating the MyFilter2.java File......................................................................................................... 316  Configuring the Filters Application .................................................................................................. 316  Running the Application .................................................................................................................... 318  Changes in Servlet 2.4 in Filter Configurations......................................................................................... 320  Using Filters with the forward( ) and include( ) Methods ............................................................. 320  Describing Listeners...............................................................................................................................................321  Need of Event Programming and Importance of Listeners..................................................................... 321  Types of Listeners ......................................................................................................................................... 321  Describing the Listener Interfaces............................................................................................................... 322  The ServletContextListener Interface................................................................................................ 323  The ServletContextAttributeListener Interface................................................................................ 327  The HttpSessionListener Interface..................................................................................................... 332  The HttpSessionAttributeListener Interface .................................................................................... 332  The HttpSessionBindingListener Interface....................................................................................... 337  The HttpSessionActivationListener Interface .................................................................................. 340  The ServletRequestListener Interface................................................................................................ 340  The ServletRequestAttributeListener Interface................................................................................ 341  Describing Wrappers .............................................................................................................................................341  Need of Wrappers......................................................................................................................................... 341  Types of Wrapper Classes............................................................................................................................ 341  The ServletRequestWrapper Class .................................................................................................... 342 The ServletResponseWrapper Class.................................................................................................. 342 The HttpServletRequestWrapper Class............................................................................................ 343 The HttpServletResponseWrapper Class ......................................................................................... 343  Using Wrappers............................................................................................................................................. 344  Creating the Home.html Page............................................................................................................ 344  Creating the TestServlet.java File ...................................................................................................... 344  Creating the MyRequestWrapper.java File...................................................................................... 345  Creating the MyResponseWrapper.java File.................................................................................... 345  Creating the MyWrapperFilter.java File........................................................................................... 346  Creating the Web.xml File.................................................................................................................. 346  Deploying and Running the Application ......................................................................................... 347  Applet to Servlet Communication........................................................................................................................348  An Example Using Applet in Servlet.......................................................................................................... 348  Summary..................................................................................................................................................................351  Exercise ....................................................................................................................................................................351 
  • 13.
    Table of Contents xvii Chapter10: Introduction to JSP ..................................................................... 353 Understanding JSP ................................................................................................................................................. 354  Advantages of JSP over Servlets ................................................................................................................. 355  Introducing the Tag-Based Approach ........................................................................................................ 356  Describing the JSP Architecture .................................................................................................................. 357  JSP Model I architecture...................................................................................................................... 357  Describing the JSP Life-Cycle................................................................................................................................ 358  The Page Translation Stage.......................................................................................................................... 358  The Compilation Stage ................................................................................................................................. 360  The Loading & Initialization Stage ............................................................................................................. 360  The Request Handling Stage........................................................................................................................ 361  The Destroying Stage.................................................................................................................................... 361  Creating Simple JSP Pages..................................................................................................................................... 361  Summary.................................................................................................................................................................. 364  Exercise .................................................................................................................................................................... 364  Chapter 11: Working with JSP Basic Tags and Implicit Objects ................. 365 Exploring Scripting Tags ....................................................................................................................................... 366  Types of Scripting Tags ................................................................................................................................ 366  The Scriptlet Tag.................................................................................................................................. 366  The Declarative Tag............................................................................................................................. 367  The Expression Tag ............................................................................................................................. 367  Working with JSP Scripting Tags................................................................................................................ 368  Exploring Implicit Objects..................................................................................................................................... 370  Features of Implicit Objects ......................................................................................................................... 370  Types of Implicit Objects.............................................................................................................................. 370  The request Object ............................................................................................................................... 371  The response Object............................................................................................................................. 371  The out Object ...................................................................................................................................... 371  The page Object.................................................................................................................................... 371  The pageContext Object...................................................................................................................... 371  The application Object ........................................................................................................................ 371  The session Object................................................................................................................................ 371  The config Object ................................................................................................................................. 371  The exception Object ........................................................................................................................... 371  Working with Implicit Objects .................................................................................................................... 372  Exploring Directive Tags ....................................................................................................................................... 376  Types of Directive Tags................................................................................................................................ 377  The Page Directive Tag ....................................................................................................................... 377  The Include Directive.......................................................................................................................... 378 
  • 14.
    Table of Contents xviii TheTaglib Directive ............................................................................................................................ 378  Working with JSP Directive Tags................................................................................................................ 379  Summary.................................................................................................................................................................. 382  Exercise .................................................................................................................................................................... 382  Chapter 12: Working with JavaBeans and Action Tags in JSP ................... 383 What is JavaBean?..................................................................................................................................................384  Advantages of using JavaBeans .................................................................................................................. 384  Action Tags..............................................................................................................................................................384  Describing the Include Tag .......................................................................................................................... 385  Difference Between Include Directive and Include Action............................................................ 385  Describing the Forward Tag ........................................................................................................................ 386  Describing the Param Tag............................................................................................................................ 386  name ...................................................................................................................................................... 387  value ...................................................................................................................................................... 387  Describing the useBean Tag......................................................................................................................... 390  Describing the Attributes of the <jsp:useBean> Tag....................................................................... 390  Using the <jsp:useBean> Tag Attributes .......................................................................................... 391  Generating Servlets with the <jsp:useBean> Tag ............................................................................ 392  Describing the setProperty Tag................................................................................................................... 393  name ...................................................................................................................................................... 393  property ................................................................................................................................................ 393  value ...................................................................................................................................................... 394  param .................................................................................................................................................... 394  Describing the getProperty Tag................................................................................................................... 395  The name Attribute ............................................................................................................................. 395  The property Attribute........................................................................................................................ 395  Plugin.............................................................................................................................................................. 395  Params ............................................................................................................................................................ 396  Fallback........................................................................................................................................................... 396  Attribute ......................................................................................................................................................... 396  Body................................................................................................................................................................ 397  Element........................................................................................................................................................... 397  Text.................................................................................................................................................................. 398  Declaring a Bean in a JSP.......................................................................................................................................398  Creating a Bean.............................................................................................................................................. 398  Declaring the Bean in a JSP.......................................................................................................................... 399  Accessing the Bean Properties..................................................................................................................... 399  Generating Dynamic Content within a JSP................................................................................................ 400  Deploying and Running Application ......................................................................................................... 401 
  • 15.
    Table of Contents xix Summary..................................................................................................................................................................403  Exercise....................................................................................................................................................................404  Chapter 13: Enhancing the JSP Tags Support .............................................. 405 Custom Tags............................................................................................................................................................406  Need of Custom Tags ................................................................................................................................... 406  Elements of Custom Tags............................................................................................................................. 407  Tag Library Descriptor........................................................................................................................ 407  Tag Handler.......................................................................................................................................... 408  The Tag Extension API...........................................................................................................................................410  Empty Tags..............................................................................................................................................................411  Empty Tag Handler Life Cycle.................................................................................................................... 412  Using Empty Tags......................................................................................................................................... 412  Body Content Tag Interface...................................................................................................................................420  Body Tag Handler Life Cycle ...................................................................................................................... 420  Using BodyContent Tag .............................................................................................................................. 421  Iteration tag Interface.............................................................................................................................................429  Iteration Tag Handler Life Cycle................................................................................................................. 429  Simple Tags .............................................................................................................................................................433  Life Cycle of Simple Tag Handlers ............................................................................................................. 433  Differences between Simple Tag and Classic Tag Handlers ................................................................... 435  The TryCatchFinally Interface ..............................................................................................................................435  Summary..................................................................................................................................................................436  Exercise ....................................................................................................................................................................436  Chapter 14: Understanding JSP Expression Language ............................... 437 Basic Syntax of using EL........................................................................................................................................438  Types of EL Expressions............................................................................................................................... 438  Immediate and Deferred Expressions............................................................................................... 439  Value Expressions................................................................................................................................ 439  Method Expressions............................................................................................................................ 440  Tag Attribute Types...................................................................................................................................... 440  Resolving EL Expressions ............................................................................................................................ 441  Describing the EL Resolver Classes................................................................................................... 441  EL Operators.................................................................................................................................................. 443  Types of EL Operators ........................................................................................................................ 443  An Example of Using EL Operators.................................................................................................. 444  Implicit EL Objects........................................................................................................................................ 445  Example 1 of Using Implicit EL Objects ........................................................................................... 447  Example 2 of Using Implicit EL Objects ........................................................................................... 449 
  • 16.
    Table of Contents xx IntroducingFunctions with EL.............................................................................................................................451  Using EL Functions ................................................................................................................................................451  Summary..................................................................................................................................................................453  Exercise ....................................................................................................................................................................453  Chapter 15: Implementing Internationalization ............................................. 455 Introducing Internationalization ..........................................................................................................................456  Java and Internationalization................................................................................................................................456  Describing the Locale Class ......................................................................................................................... 456  Parameters of the Locale Object......................................................................................................... 457  Using the Locale Class ........................................................................................................................ 457  Describing the ResourceBundle Class ........................................................................................................ 458  The PropertyResourceBundle Class.................................................................................................. 459  The ListResourceBundle Class........................................................................................................... 461  Internationalizing Web Applications...................................................................................................................463  Creating the JSP Views................................................................................................................................. 464  Creating the index.html View............................................................................................................ 464  Creating the Home.jsp View .............................................................................................................. 464  Creating the I18NServlet Class.................................................................................................................... 465  Creating the Resource Bundles.................................................................................................................... 465  Configuring the Application........................................................................................................................ 466  Designing the Application Directory Structure ........................................................................................ 466  Running the Application.............................................................................................................................. 467  Summary..................................................................................................................................................................468  Exercise ....................................................................................................................................................................468  Chapter 16: Working with JSP Standard Tag Library (JSTL)....................... 469 Describing JSTL Core Tags....................................................................................................................................470  General-Purpose Tags .................................................................................................................................. 470  The <c:out> tag .................................................................................................................................... 470  The <c:set> Tag .................................................................................................................................... 472  The <c:remove> Tag............................................................................................................................ 473  The <c:catch> Tag................................................................................................................................ 473  Conditional and Looping Tags.................................................................................................................... 473  Types of Conditional and Looping Tags .......................................................................................... 473  Using Conditional and Looping Tags............................................................................................... 475  Networking Tags........................................................................................................................................... 480  The <c:import> Tag............................................................................................................................. 480  The <c:url> Tag.................................................................................................................................... 480  The <c:redirect> Tag............................................................................................................................ 481 
  • 17.
    Table of Contents xxi The<c:param> Tag.............................................................................................................................. 481  Describing the JSTL SQL Tags ..............................................................................................................................481  Types of JSTL SQL Tags ............................................................................................................................... 481  The <sql:query> Tag............................................................................................................................ 482  The <sql:update> Tag ......................................................................................................................... 482  The <sql:param> Tag .......................................................................................................................... 482  The <sql:dateParam> Tag................................................................................................................... 483  The <sql:setDataSource> Tag............................................................................................................. 483  The <sql:transaction> Tag .................................................................................................................. 483  Using JSTL SQL Tags.................................................................................................................................... 483  JSTL Formatting Tags.............................................................................................................................................486  Basic Formatting Tags .................................................................................................................................. 486  Types of Basic Formatting Tags......................................................................................................... 486 Using Basic JSTL Formatting Tags .................................................................................................... 488  Number Formatting Tags............................................................................................................................. 490  Types of Number Formatting Tags ................................................................................................... 490  Using Number Format Tags............................................................................................................... 491  Date Formatting Tags ................................................................................................................................... 494  Types of Date Formatting Tags.......................................................................................................... 494  Using Date Formatting Tags .............................................................................................................. 495  Time Zone Formatting Tags......................................................................................................................... 497  Types of Time Zone Formatting Tags............................................................................................... 497 Using TimeZone Formatting Tags..................................................................................................... 497  JSTL XML Tags .......................................................................................................................................................499  XML Core Tags.............................................................................................................................................. 499  The <x:parse> Tag ............................................................................................................................... 500  The <x:out> Tag................................................................................................................................... 500  The <x:set> Tag.................................................................................................................................... 500  XML Flow Control Tags............................................................................................................................... 500  The <x:if> Tag ...................................................................................................................................... 501 The <x:choose> Tag............................................................................................................................. 501 The <x:when> Tag............................................................................................................................... 501 The <x:otherwise> Tag........................................................................................................................ 501 The <x:forEach> tag ............................................................................................................................ 501  XML Transformation Tags........................................................................................................................... 502  The <x:transform> Tag........................................................................................................................ 502  The <x:param> Tag ............................................................................................................................. 502  Implementing JSTL Tags .......................................................................................................................................502  Summary..................................................................................................................................................................506  Exercise ....................................................................................................................................................................506 
  • 18.
    Table of Contents xxii Chapter17: Working with JavaServer Faces ................................................ 507 Introducing JSF .......................................................................................................................................................508  Explaining the Features of JSF ..............................................................................................................................509  Exploring the JSF Architecture..............................................................................................................................510  Describing JSF Elements ........................................................................................................................................511  UI Component ............................................................................................................................................... 511  Renderer ......................................................................................................................................................... 512  Validators....................................................................................................................................................... 513  Backing Beans................................................................................................................................................ 514  Converters...................................................................................................................................................... 514  Events and Listeners..................................................................................................................................... 515  Action Events ....................................................................................................................................... 515  Value-change Events........................................................................................................................... 516  Data Model Events .............................................................................................................................. 517  Phase Events......................................................................................................................................... 517  Message .......................................................................................................................................................... 517  Navigation...................................................................................................................................................... 518  Exploring the JSF Request Processing Life cycle ................................................................................................518  The Restore View Phase ............................................................................................................................... 519  The Apply Request Values Phase ............................................................................................................... 519  The Process Validations Phase .................................................................................................................... 520  The Update Model Values Phase ................................................................................................................ 520  The Invoke Application Phase..................................................................................................................... 520  The Render Response Phase ........................................................................................................................ 521  Exploring JSF Tag Libraries...................................................................................................................................521  JSF HTML Tags.............................................................................................................................................. 522  The <h:commandButton> Tag ........................................................................................................... 522  The <h:commandLink> Tag............................................................................................................... 523  The <h:dataTable> Tag ....................................................................................................................... 524  The <h:form> Tag................................................................................................................................ 525  The <h:graphicImage > Tag ............................................................................................................... 526  The <h:inputHidden> Tag.................................................................................................................. 526  The <h:inputSecret> Tag..................................................................................................................... 527  The <h:inputText> Tag ....................................................................................................................... 527  The <h:inputTextarea> Tag................................................................................................................ 528  The <h:message> Tag.......................................................................................................................... 528  The <h:messages> Tag........................................................................................................................ 529  The <h:outputFormat> Tag................................................................................................................ 530  The <h:outputLabel> Tag................................................................................................................... 530 
  • 19.
    Table of Contents xxiii The<h:outputLink> Tag..................................................................................................................... 530  The <h:outputText> Tag..................................................................................................................... 531  The <h:panelGrid> Tag....................................................................................................................... 531  The <h:panelGroup> Tag ................................................................................................................... 532  The <h:selectbooleanCheckbox> Tag................................................................................................ 533  The <h:selectManyCheckbox> Tag ................................................................................................... 534  The <h:selectManyListbox> Tag........................................................................................................ 535  The <h:selectManyMenu> Tag .......................................................................................................... 535  The <h:selectOneListbox> Tag........................................................................................................... 536  The <h:selectOneMenu> Tag ............................................................................................................. 537  The <h:selectOneRadio> Tag ............................................................................................................. 538  The <h:column> Tag ........................................................................................................................... 539  JSF Core Tags................................................................................................................................................. 540  The <f:actionListener> Tag................................................................................................................. 541  The <f:valueChangeListener> Tag .................................................................................................... 541  The <f:view> Tag................................................................................................................................. 541  The <f:attribute> Tag........................................................................................................................... 542  The <f:converter> Tag......................................................................................................................... 542  The <f:convertDateTime> Tag ........................................................................................................... 543  The <f:convertNumber> Tag.............................................................................................................. 544  The <f:facet> Tag ................................................................................................................................. 544  The <f:loadBundle> Tag ..................................................................................................................... 545  The <f:param> Tag .............................................................................................................................. 545  The <f:selectItem> Tag........................................................................................................................ 546  The <f:selectItems> Tag ...................................................................................................................... 546  The <f:subview> Tag........................................................................................................................... 547  The <f:validateDoubleRange> Tag.................................................................................................... 548  The <f:validateLength> Tag............................................................................................................... 548  The <f:validateLongRange> Tag ....................................................................................................... 548  The <f:validator> Tag.......................................................................................................................... 549  The <f:verbatim> Tag.......................................................................................................................... 549  The <f:phaseListener> Tag ................................................................................................................. 550  The <f:setPropertyActionListener> Tag ........................................................................................... 551  Exploring Backing Beans .......................................................................................................................................551  An Example of Using a Backing Bean ........................................................................................................ 551  Using a Backing Bean Method as an Event Handler................................................................................ 553  Working with Managed Beans.................................................................................................................... 554  JSF Input Validation...............................................................................................................................................555  Using the Validator Method ........................................................................................................................ 556 
  • 20.
    Table of Contents xxiv UsingValidators............................................................................................................................................ 556  Standard JSF Validators...................................................................................................................... 556  JSF Type Conversion..............................................................................................................................................557  Handling Page Navigation in JSF.........................................................................................................................558  Configuring JSF Applications ...............................................................................................................................559  Setting the web.xml File ............................................................................................................................... 559  Configuring the faces-config.xml File......................................................................................................... 560  Creating a Simple JSF Application .......................................................................................................................561  Creating the views..................................................................................................................................................562  Creating Backing Bean.................................................................................................................................. 564  Configuring the Application........................................................................................................................ 565  Summary..................................................................................................................................................................568  Exercise ....................................................................................................................................................................568  Chapter 18: Java EE Design Patterns ............................................................ 569 Describing the Java EE Application Architecture ..............................................................................................570  Introducing a Design Pattern................................................................................................................................571  Discussing the Role of Design Patterns ...............................................................................................................571  Exploring Types of Patterns..................................................................................................................................571  The Front Controller Pattern........................................................................................................................ 574  Problem................................................................................................................................................. 574  Forces..................................................................................................................................................... 574  Solution ................................................................................................................................................. 574  Strategies............................................................................................................................................... 576  Consequences....................................................................................................................................... 576  The Composite View Pattern....................................................................................................................... 577  Problem................................................................................................................................................. 577  Forces..................................................................................................................................................... 577  Solution ................................................................................................................................................. 577  Strategies............................................................................................................................................... 578  Consequences....................................................................................................................................... 579  The Composite Entity Pattern ..................................................................................................................... 579  Problem................................................................................................................................................. 579  Forces..................................................................................................................................................... 579  Solution ................................................................................................................................................. 579  Strategies............................................................................................................................................... 581  Consequences....................................................................................................................................... 581  The Intercepting Filter Pattern .................................................................................................................... 582  Problem................................................................................................................................................. 582  Forces..................................................................................................................................................... 582 
  • 21.
    Table of Contents xxv Solution................................................................................................................................................. 582  Strategies............................................................................................................................................... 583  Consequences....................................................................................................................................... 583  The Transfer Object Pattern ......................................................................................................................... 583  Problem................................................................................................................................................. 584  Forces..................................................................................................................................................... 584  Solution ................................................................................................................................................. 584  Strategies............................................................................................................................................... 585  Consequences....................................................................................................................................... 585  The Session Facade Pattern.......................................................................................................................... 586  Problem................................................................................................................................................. 586  Forces..................................................................................................................................................... 586  Solution ................................................................................................................................................. 586  Strategies............................................................................................................................................... 587  Consequences....................................................................................................................................... 587  The Service Locator Pattern ......................................................................................................................... 588  Problem................................................................................................................................................. 588  Forces..................................................................................................................................................... 588  Solution ................................................................................................................................................. 588  Strategies............................................................................................................................................... 589  Consequences....................................................................................................................................... 590  The Data Access Object Pattern................................................................................................................... 590  Problem................................................................................................................................................. 590  Forces..................................................................................................................................................... 590  Solution ................................................................................................................................................. 590  Strategies............................................................................................................................................... 592  Consequences....................................................................................................................................... 592  The View Helper Pattern.............................................................................................................................. 592  Problem................................................................................................................................................. 592  Forces..................................................................................................................................................... 592  Solution ................................................................................................................................................. 593  Strategies............................................................................................................................................... 593  Consequences....................................................................................................................................... 594  The Dispatcher View Pattern....................................................................................................................... 594  Problem................................................................................................................................................. 594  Forces..................................................................................................................................................... 594  Solution ................................................................................................................................................. 594  Strategies............................................................................................................................................... 595  Consequences....................................................................................................................................... 596 
  • 22.
    Table of Contents xxvi TheService To Worker Pattern.................................................................................................................... 596  Problem................................................................................................................................................. 596  Forces..................................................................................................................................................... 596  Solution ................................................................................................................................................. 596  Strategies............................................................................................................................................... 597  Consequences....................................................................................................................................... 597  Summary..................................................................................................................................................................598  Exercise ....................................................................................................................................................................598  Chapter 19: Implementing Security in Web Applications............................. 599 Exploring JAAS.......................................................................................................................................................600  Implementing Authentication with JAAS.................................................................................................. 600  Describing Authorization in Web Applications........................................................................................ 602  Implementing Data Integrity with JAAS API............................................................................................ 602  Exploring HTTP Authentication Mechanisms....................................................................................................602  Describing HTTP Basic Authentication...................................................................................................... 602  Describing Form-Based Authentication..................................................................................................... 603  Describing Client-Certificate Authentication ............................................................................................ 603  Describing HTTP Digest Authentication ................................................................................................... 604  Securing Web Applications...................................................................................................................................604  Describing Declarative Security .................................................................................................................. 604  Using Declarative Security ................................................................................................................. 604  Using Form-based Authentication for Declarative Security .......................................................... 612  Describing Programmatic Security ............................................................................................................. 615  Using Programmatic Security ............................................................................................................ 616  Summary..................................................................................................................................................................626  Exercise ....................................................................................................................................................................626  Appendix A: Introduction to DBMS................................................................ 627 Appendix B: Hospital Management System .................................................. 637 Answers............................................................................................................ 641 Index ................................................................................................................. 651 What’s on CD-ROM.......................................................................................... 657