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

Murach’s Java Servlets

and JSP (3rd Edition)


Introduction xvii

Section 1 Get started right


Chapter 1 An introduction to web programming with Java 3
Chapter 2 How to structure a web application with the MVC pattern 29
Chapter 3 How to use NetBeans and Tomcat 49

Section 2 Essential servlet and JSP skills


Chapter 4 A crash course in HTML5 and CSS3 89
Chapter 5 How to develop servlets 127
Chapter 6 How to develop JavaServer Pages 173
Chapter 7 How to work with sessions and cookies 201
Chapter 8 How to use EL 243
Chapter 9 How to use JSTL 269
Chapter 10 How to use custom JSP tags 307

Section 3 Essential database skills


Chapter 11 How to use a MySQL database 347
Chapter 12 How to use JDBC to work with a database 377
Chapter 13 How to use JPA to work with a database 421

Section 4 Advanced servlet and JSP skills


Chapter 14 How to use JavaMail to send email 459
Chapter 15 How to use SSL to work with a secure connection 483
Chapter 16 How to restrict access to a web resource 501
Chapter 17 More security skills 525
Chapter 18 How to work with HTTP requests and responses 543
Chapter 19 How to work with listeners 571
Chapter 20 How to work with filters 587
Chapter 21 How to work with JavaServer Faces 613

Section 5 The Music Store website


Chapter 22 An introduction to the Music Store website 643
Chapter 23 The applications of the website 673

Appendixes
Appendix A How to set up your PC for this book 691
Appendix B How to set up your Mac for this book 711
Expanded contents vii

Expanded contents
Section 1 Get started right
Chapter 1 An introduction to web applications
A typical web application................................................................................................4
The components of a web application.............................................................................6
How static web pages work..............................................................................................8
How dynamic web pages work...................................................................................... 10
Three approaches for Java web applications................................. 12
Servlet/JSP..................................................................................................................... 12
JSF.................................................................................................................................. 12
Spring Framework......................................................................................................... 12
An introduction to servlet/JSP web development......................... 14
The software components.............................................................................................. 14
The architecture............................................................................................................. 16
The standard directories and files.................................................................................. 18
Software for developing Java web applications.............................20
IDEs for developing Java web applications...................................................................20
Web servers for Java web applications..........................................................................22
Database servers for Java web applications...................................................................22
Tools for deploying Java web applications....................................................................24

Chapter 2 How to structure a web application


with the MVC pattern
Two patterns for servlet/JSP applications......................................30
The Model 1 pattern.......................................................................................................30
The Model 2 (MVC) pattern.......................................................................................... 32
A servlet/JSP application that uses the MVC pattern....................34
The user interface..........................................................................................................34
The HTML for the first page.........................................................................................36
The CSS for both web pages..........................................................................................38
The servlet for the back-end processing........................................................................40
The web.xml file............................................................................................................ 42
The User class................................................................................................................44
The JSP for the second page..........................................................................................46

Chapter 3 How to use NetBeans and Tomcat


How to get started with NetBeans...................................................50
How to start NetBeans...................................................................................................50
How to create a new web application............................................................................ 52
How to use the Projects window....................................................................................56
How to open and close projects..................................................................................... 58
How to build, deploy, and run a web application..........................................................60
How to work with HTML and JSP files.............................................62
How to add an HTML or JSP file.................................................................................. 62
How to edit an HTML or JSP file..................................................................................64
viii Expanded contents

How to work with Java files..............................................................66


How to add a Java class..................................................................................................66
How to add a servlet.......................................................................................................68
How to edit a Java file.................................................................................................... 70
How to work with XML files...............................................................72
How to edit the web.xml file..........................................................................................72
How to edit other XML files.......................................................................................... 72
Other skills for working with web applications.............................. 74
How to add existing files to a project............................................................................ 74
How to deploy a web application to a remote server..................................................... 74
How to work with a web application server................................................................... 76
How to add a class library or a JAR file to a project..................................................... 78
How to register a database connection..........................................................................80

Section 2 Essential servlet and JSP skills


Chapter 4 A crash course in HTML5 and CSS3
How to work with HTML....................................................................90
The starting HTML for a web page...............................................................................90
How to code HTML elements.......................................................................................92
How to use the HTML5 semantic elements..................................................................94
How to use the div and span elements with HTML5....................................................96
How to ensure cross-browser compatibility..................................................................98
How to code links........................................................................................................100
How to include images................................................................................................. 102
How to code tables....................................................................................................... 104
How to work with CSS.....................................................................106
How to provide CSS styles for an HTML page........................................................... 106
How to code the basic CSS selectors........................................................................... 108
How to code CSS rule sets and comments.................................................................. 110
How to use CSS to format a table................................................................................ 112
How to code HTML forms................................................................ 114
How to code a form...................................................................................................... 114
How to code text boxes................................................................................................ 116
How to code buttons.................................................................................................... 118
How to code check boxes and radio buttons................................................................ 120
How to code combo boxes and list boxes.................................................................... 122

Chapter 5 How to develop servlets


How to create and map a servlet.................................................... 128
How to create a servlet.................................................................................................128
How to map a servlet with the web.xml file................................................................ 130
How to map a servlet with an annotation.................................................................... 132
How to request a servlet................................................................. 134
How to use the HTTP GET method............................................................................ 134
How to use the HTTP POST method.......................................................................... 136
When to use the HTTP GET and POST methods....................................................... 136
Skills for working with servlets...................................................... 138
How to get the values of the parameters...................................................................... 138
How to get the real path for a file................................................................................ 140
Expanded contents ix

How to get and set request attributes........................................................................... 142


How to forward requests.............................................................................................. 144
How to redirect responses............................................................................................ 144
How to validate data........................................................................ 146
How to validate data on the client............................................................................... 146
How to validate data on the server.............................................................................. 146
How to work with the web.xml file.................................................. 150
A complete web.xml file.............................................................................................. 150
How to work with initialization parameters................................................................ 152
How to implement custom error handling................................................................... 156
More skills for working with servlets.............................................160
How the methods of a servlet work............................................................................. 160
Why you shouldn’t use instance variables in servlets................................................. 162
How to work with servlet errors.....................................................164
How to solve common servlet problems...................................................................... 164
How to print debugging data to the console................................................................ 166
How to print debugging data to a log file.................................................................... 168

Chapter 6 How to develop JavaServer Pages


A crash course in EL and JSTL...................................................... 174
How to code a JavaBean.............................................................................................. 174
How to use EL to get attributes and JavaBean properties........................................... 176
How to enable the core JSTL library........................................................................... 178
How to use the JSTL if tag.......................................................................................... 178
How to use JSP tags........................................................................180
How to code directives, scriptlets, and expressions.................................................... 180
How to code comments in a JSP.................................................................................. 182
How to use standard JSP tags with JavaBeans...........................184
An introduction to standard JSP tags.......................................................................... 184
How to code the useBean tag....................................................................................... 186
How to code the getProperty and setProperty tags..................................................... 188
How to include a file in a JSP.........................................................190
A JSP that includes a header and footer file................................................................ 190
Three techniques for including files in a JSP.............................................................. 194
How to fix common JSP errors.......................................................196
Chapter 7 How to work with sessions and cookies
An introduction to session tracking..............................................202
Why session tracking is difficult with HTTP..............................................................202
How session tracking works in Java............................................................................202
An application that needs session tracking.................................................................204
How to work with sessions.............................................................206
How to set and get session attributes...........................................................................206
More methods of the session object.............................................................................208
How to provide thread-safe access to the session object............................................. 210
How to work with cookies............................................................... 212
An introduction to cookies.......................................................................................... 212
How to create and use cookies..................................................................................... 214
How to view and delete cookies.................................................................................. 216
x Expanded contents

Four methods for working with cookies...................................................................... 218


A utility class for working with cookies......................................................................220
How to work with URL rewriting and hidden fields......................222
How to use URL rewriting to pass parameters...........................................................222
How to use hidden fields to pass parameters...............................................................224
The Download application..............................................................226
The user interface........................................................................................................226
The file structure..........................................................................................................228
The web.xml file..........................................................................................................228
The code for the JSPs...................................................................................................230
The code for the servlet...............................................................................................234

Chapter 8 How to use EL


An introduction to JSP Expression Language.............................244
Advantages of EL.........................................................................................................244
Disadvantages of EL....................................................................................................244
Essential skills for working with EL...............................................246
How to use the dot operator to work with JavaBeans and maps.................................246
How to use EL to specify scope..................................................................................248
How to use the [ ] operator to work with arrays and lists............................................250
How to use the dot operator to access nested properties............................................. 252
Other skills for working with EL.....................................................254
How to use the [ ] operator to access attributes...........................................................254
How to work with the other implicit EL objects.........................................................256
How to work with other EL operators.........................................................................260
How to disable EL........................................................................................................264
How to disable scripting..............................................................................................264

Chapter 9 How to use JSTL


An introduction to JSTL..................................................................270
The JSTL libraries....................................................................................................... 270
How to make the JSTL JAR files available to your application.................................. 270
How to code the taglib directive.................................................................................. 270
How to code a JSTL tag............................................................................................... 270
How to view the documentation for a library.............................................................. 272
How to work with the JSTL core library........................................ 274
How to use the out tag................................................................................................. 274
How to use the forEach tag.......................................................................................... 276
How to use the forTokens tag....................................................................................... 278
Four more attributes for looping..................................................................................280
How to use the if tag....................................................................................................282
How to use the choose tag...........................................................................................284
How to use the url tag..................................................................................................286
Other tags in the JSTL core library.............................................................................288
The Cart application........................................................................290
The user interface........................................................................................................290
The code for the business classes................................................................................292
The code for the servlets and JSPs..............................................................................296
Expanded contents xi

Chapter 10 How to use custom JSP tags


How to code a custom tag that doesn’t have a body...................308
The tag.........................................................................................................................308
The tag element............................................................................................................ 310
The tag class................................................................................................................. 312
How to code a custom tag that has a body................................... 314
The tag......................................................................................................................... 314
The tag element............................................................................................................ 316
The tag class................................................................................................................. 316
How to code a custom tag that has attributes.............................. 318
The tag......................................................................................................................... 318
The tag element............................................................................................................320
The tag class................................................................................................................. 322
How to code a custom tag that reiterates its body......................324
The tag.........................................................................................................................324
The tag element............................................................................................................ 326
The tag class................................................................................................................. 326
How to work with scripting variables.............................................330
An introduction to scripting variables......................................................................... 330
The TEI class for four scripting variables................................................................... 332
Classes, methods, and fields for working with custom tags......334
Methods and fields of the TagSupport class................................................................ 334
Methods and fields of the PageContext class............................................................... 336
Methods and fields of the BodyTagSupport class........................................................ 338
Methods and fields of the BodyContent class..............................................................340

Section 3 Essential database skills


Chapter 11 How to use a MySQL database
An introduction to MySQL..............................................................348
What MySQL provides................................................................................................348
Two ways to interact with MySQL.............................................................................. 350
How to use MySQL Workbench......................................................352
The Home tab of MySQL Workbench......................................................................... 352
How to open a database connection............................................................................ 354
How to start and stop the MySQL server.................................................................... 356
How to enter and execute a SQL statement................................................................. 358
How to enter and execute a SQL script.......................................................................360
The SQL statements for creating a database...............................362
How to create, select, and drop a database.................................................................. 362
How to create and drop a table....................................................................................364
How to insert multiple rows into a table......................................................................366
The SQL statements for data manipulation..................................368
How to select data from a single table.........................................................................368
How to select data from multiple tables...................................................................... 370
How to insert, update, and delete data......................................................................... 372
xii Expanded contents

Chapter 12 How to use JDBC to work with a database


How to work with JDBC...................................................................378
An introduction to database drivers............................................................................. 378
How to connect to a database......................................................................................380
How to return a result set and move the cursor through it.......................................... 382
How to retrieve data from a result set..........................................................................384
How to insert, update, and delete data.........................................................................386
How to work with prepared statements.......................................................................388
The SQL Gateway application........................................................390
The user interface........................................................................................................390
The code for the JSP....................................................................................................392
The code for the servlet...............................................................................................394
The code for the utility class........................................................................................ 398
How to work with connection pooling...........................................400
How connection pooling works...................................................................................400
How to make a connection pool available...................................................................400
How to implement a connection pool..........................................................................402
How to use a connection pool......................................................................................404
The Email List application..............................................................406
The user interface........................................................................................................406
The code for the JSP....................................................................................................406
The code for the servlet...............................................................................................408
The code for the database class................................................................................... 410
The code for the utility class........................................................................................ 416

Chapter 13 How to use JPA to work with a database


An introduction to JPA....................................................................422
A summary of JPA implementations........................................................................... 422
Entities and the entity manager................................................................................... 422
How to configure NetBeans to work with JPA............................................................424
How to configure a persistence unit.............................................................................426
How to code JPA entities................................................................428
How to code the class for an entity.............................................................................. 428
How to use getter and field annotations....................................................................... 430
How to code relationships between entities................................................................. 432
How to work with dates and times.............................................................................. 434
How to retrieve data.........................................................................436
How to get an entity manager factory.......................................................................... 436
How to retrieve an entity by primary key.................................................................... 438
How to retrieve multiple entities..................................................................................440
How to retrieve a single entity.....................................................................................442
How to modify data..........................................................................444
How to insert, update, or delete a single entity...........................................................444
How to update or delete multiple entities....................................................................446
A complete JPA class......................................................................448
The methods that modify data.....................................................................................448
The methods that retrieve data.................................................................................... 450
Expanded contents xiii

Section 4 Advanced servlet and JSP skills


Chapter 14 How to use JavaMail to send mail
An introduction to the JavaMail API..............................................460
How email works.........................................................................................................460
How to install the JavaMail API..................................................................................462
Code that uses the JavaMail API to send an email message.......................................464
How to create and send an email message..................................466
How to create a mail session........................................................................................466
How to create a message..............................................................................................468
How to address a message........................................................................................... 470
How to send a message................................................................................................ 472
Example classes that send an email message............................. 474
A helper class for sending an email with a local SMTP server................................... 474
A servlet that uses a helper class to send an email message....................................... 476
A helper class for sending an email with a remote SMTP server...............................480

Chapter 15 How to use SSL to work with a secure connection


An introduction to SSL....................................................................484
How SSL works...........................................................................................................484
How TLS works...........................................................................................................484
When to use a secure connection.................................................................................484
How SSL authentication works....................................................................................486
How to get a digital secure certificate.........................................................................488
How to configure a testing environment for SSL.........................490
How to create a certificate for testing..........................................................................490
How to enable SSL in Tomcat......................................................................................492
How to test a local SSL connection.............................................................................492
How to work with a secure connection.........................................494
How to request a secure connection............................................................................494
A JSP that uses a secure connection............................................................................496
How to return to a regular HTTP connection.............................................................496
How to switch from a local system to an Internet server............................................496

Chapter 16 How to restrict access to a web resource


An introduction to authentication..................................................502
How container-managed authentication works...........................................................502
Three types of authentication......................................................................................504
How to restrict access to web resources......................................506
How to add a security role...........................................................................................506
How to add a security constraint.................................................................................506
How to implement a security realm...............................................508
How to implement the UserDatabaseRealm................................................................508
How to implement the JDBCRealm............................................................................ 510
How to implement the DataSourceRealm................................................................... 514
How to allow access to authorized users...................................... 516
How to use basic authentication................................................................................... 516
How to use digest authentication................................................................................. 516
How to use form-based authentication........................................................................ 518
xiv Expanded contents

Chapter 17 More security skills


An introduction to website security..............................................526
Common website attacks............................................................................................. 526
How to prevent social engineering attacks.................................................................. 526
An introduction to cryptography................................................................................. 528
Common password attacks.......................................................................................... 530
How to protect passwords..............................................................532
How to hash passwords................................................................................................ 532
How to salt passwords.................................................................................................. 534
A utility class for hashing and salting passwords........................................................ 536
How to enforce password strength requirements........................................................ 538

Chapter 18 How to work with HTTP requests and responses


An introduction to HTTP.................................................................544
An HTTP request and response...................................................................................544
Common MIME types.................................................................................................546
Common HTTP request headers.................................................................................548
Common HTTP status codes....................................................................................... 550
Common HTTP response headers............................................................................... 552
How to work with the request.........................................................554
How to get a request header......................................................................................... 554
How to display all request headers.............................................................................. 556
How to work with the response......................................................558
How to set status codes................................................................................................ 558
How to set response headers........................................................................................ 558
Practical HTTP skills.......................................................................560
How to return a spreadsheet........................................................................................560
How to control caching................................................................................................560
How to compress a response with GZIP...................................................................... 562
How to return a binary file as an attachment..............................................................564
How to create virtual HTML pages............................................................................566

Chapter 19 How to work with listeners


How to use a ServletContextListener............................................572
How to code a class for the listener............................................................................. 572
How to register the listener.......................................................................................... 574
How to code a JSP that uses the attributes set by the listener..................................... 576
How to work with other types of listeners....................................578
A summary of the listener interfaces.......................................................................... 578
The methods of the listener interfaces.........................................................................580
The methods of the event objects................................................................................ 582

Chapter 20 How to work with fliters


An introduction to filters.................................................................588
How filters work........................................................................................................... 588
Two benefits of filters................................................................................................... 588
When to use filters....................................................................................................... 588
How to add a filter............................................................................590
How to code a filter......................................................................................................590
How to configure a filter..............................................................................................592
Expanded contents xv

Two filter classes.............................................................................596


How to code a filter that performs request-side processing........................................596
How to code a filter that performs response-side processing...................................... 598
Other skills for working with filters................................................600
How to wrap a request or response..............................................................................600
How to use an initialization parameter........................................................................604
How to restrict access by IP address...........................................................................606

Chapter 21 How to work with JavaServer Faces


An introduction to JSF.................................................................... 614
A summary of JSF versions......................................................................................... 614
How to add a JSF library to a project.......................................................................... 614
How to used managed beans......................................................... 616
How to create a managed bean.................................................................................... 616
How to set the scope of a bean.................................................................................... 618
How to use standard JSF tags.......................................................620
How to code the head and body tags...........................................................................620
How to display data from a managed bean.................................................................. 622
How to code the inputText tag.....................................................................................624
How to validate data.................................................................................................... 626
How to code a form action........................................................................................... 628
The Email List application..............................................................630
The user interface........................................................................................................630
The EmailList bean..................................................................................................... 632
The index.xhtml file.....................................................................................................634
The thanks.xhtml file...................................................................................................636

Section 5 The Music Store website


Chapter 22 An introduction to the Music Store website
The user interface............................................................................644
The Home and Catalog pages......................................................................................644
The code for the Home page........................................................................................644
The business layer...........................................................................648
The class diagrams.......................................................................................................648
The Product class.........................................................................................................648
The controller layer..........................................................................652
The CatalogController class........................................................................................ 652
The structure....................................................................................656
The directory structure................................................................................................656
The web.xml file.......................................................................................................... 658
The context.xml file..................................................................................................... 658
The database....................................................................................662
The database diagram..................................................................................................662
The SQL script for the database..................................................................................662
The data layer...................................................................................666
The class diagrams.......................................................................................................666
The ProductDB class...................................................................................................666
xvi Expanded contents

The JPA version compared to the JDBC version.........................670


The similarities............................................................................................................ 670
The differences............................................................................................................ 670

Chapter 23 The applications of the website


The user interface for end users.................................................... 674
The Download application........................................................................................... 674
The Cart application.................................................................................................... 674
The user interface for administrators............................................680
The Admin Login page................................................................................................680
The Admin Menu page................................................................................................682
The Process Invoices application.................................................................................684
The Reports application...............................................................................................684

Appendix A How to set up your PC for this book


How to install the source code for this book...............................................................692
How to install the JDK................................................................................................694
How to install NetBeans..............................................................................................696
How to install Tomcat..................................................................................................698
How to configure a Tomcat server...............................................................................700
How to test NetBeans and Tomcat...............................................................................704
How to install MySQL.................................................................................................706
How to create the databases for this book...................................................................708
How to restore the databases for this book..................................................................708

Appendix B How to set up your Mac for this book


How to install the source code for this book............................................................... 712
How to install the JDK................................................................................................ 714
How to install NetBeans.............................................................................................. 716
How to install Tomcat.................................................................................................. 718
How to configure a Tomcat server...............................................................................720
How to test NetBeans and Tomcat...............................................................................724
How to install the MySQL Community Server...........................................................726
How to install MySQL Workbench.............................................................................728
How to create the databases for this book................................................................... 730
How to restore the databases for this book.................................................................. 730
How to update the password for the root user............................................................. 732

You might also like