Tim Downey
Web Development
with Java
Using Hibernate,JSPs,and Servlets
*£) Springer
Web Development with Java
Tim Downey
Web Development
with Java
Using Hibernate, JSPs and Servlets
Tim Downey, BS, MS
Florida International University
Miami, FL 33199, USA
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
Library of Congress Control Number: 2007925710
ISBN: 978-1-84628-862-3
e-ISBN: 978-1-84628-863-0
Printed on acid-free paper
© Springer-Verlag London Limited 2007
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as
permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced,
stored or transmitted, in any form or by any means, with the prior permission in writing of the pub-
lishers, or in the case of reprographic reproduction in accordance with the terms of licences issued by
the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be
sent to the publishers.
The use of registered names, trademarks, etc. in this publication does not imply, even in the absence
of a specifi c statement, that such names are exempt from the relevant laws and regulations and therefore
free for general use.
The publisher makes no representation, express or implied, with regard to the accuracy of the informa-
tion contained in this book and cannot accept any legal responsibility or liability for any errors or
omissions that may be made.
9 8 7 6 5 4 3 2 1
Springer Science+Business Media
springer.com
To Bobbi, my sweetheart, with all my love.
Preface
I have been teaching web development for ten years. I started with Perl.
I can still remember the behemoth programs that contained all the
logic and HTML. I remember using a text editor to write the program.
Debugging consisted of a lot of print statements. It was a fun time, full
of exploration, but I do not miss them.
Five years ago, I made the move to Java and Java servlets. Life became
much simpler with the use of NetBeans. It has been a critical component
in developing Web applications using Java. Debugging a web application
in NetBeans is just as easy as debugging any Java application.
This book is meant for students who have a solid background in
programming, but who do not have any database training. Until two
years ago, my students used a glorifi ed HashMap to save data. Then a
former student gave me the word: Hibernate. For anyone with a pro-
gramming background in Java, using Hibernate to save data to a rela-
tional database is a simple task.
I have always been a proponent of automating the common tasks
that Web applications perform. There are many packages that can sim-
plify the job of a Web developer: Log4j, BeanUtils and Hibernate. I have
created additional classes that can automate additional tasks.
Readers of this book should have a good background in Java pro-
gramming. The book uses HTML, HTML Forms, Cascading Style Sheets
and XML as tools. Each topic will receive an introduction, but the full
scope of the area will not be explored. The focus of the book is on Java
Servlets that use Java Server Pages and connect to a MySQL database
using Hibernate. No SQL will be covered in the book, except for a short
section in the Appendix for those who want to see what Hibernate is
doing.
I am grateful to the community of web developers, who have pro-
vided all the excellent tools for creating web applications: Apache,
Tomcat, Hibernate, Java Servlets, Java Server Pages, NetBeans, Log4j,
Commons.
I am thankful to Bobbi, my sweetheart, for all of her love and support.
Without Bobbi, this book would not have been fi nished. I also want to
thank Kip Irvine for encouraging me to write. Without Kip, this book
would never have been started.
Tim Downey
Miami, FL
vii
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
vii
1 Browser – Server Communication . . . . . . . . . . . . . . .
1.1 Hypertext Transfer Protocol . . . . . . . . . . . . . .
1.1.1 Request Format . . . . . . . . . . . . . . . . .
1.1.2 Response Format . . . . . . . . . . . . . . . .
1.1.3 Content Type . . . . . . . . . . . . . . . . . . .
1.2 Markup Language . . . . . . . . . . . . . . . . . . . .
1.2.1 Hypertext Markup Language . . . . . . . . . .
1.2.2 Basic Tags for a Web Page . . . . . . . . . . .
1.2.3 What Is the HT in HTML? . . . . . . . . . . .
1.3 HTML Forms . . . . . . . . . . . . . . . . . . . . . . .
1.3.1 Form Elements . . . . . . . . . . . . . . . . . .
1.3.2 Representing Data . . . . . . . . . . . . . . . .
1.3.3 Transmitting Data over the Web . . . . . . . .
1.4
Processing Form Data . . . . . . . . . . . . . . . . . .
1.4.1 Web Application . . . . . . . . . . . . . . . . .
JSP . . . . . . . . . . . . . . . . . . . . . . . . .
1.4.2
1.4.3
Initialising Form Elements . . . . . . . . . . .
1.5
The Truth About JSPs . . . . . . . . . . . . . . . . . .
1.5.1 Servlet for a JSP . . . . . . . . . . . . . . . . .
1.5.2 Handling a JSP . . . . . . . . . . . . . . . . . .
Tomcat and NetBeans . . . . . . . . . . . . . . . . . .
1.6
1.6.1 Creating a Project in NetBeans . . . . . . . . .
1.6.2 Web Project in NetBeans . . . . . . . . . . . .
1.7
Summary . . . . . . . . . . . . . . . . . . . . . . . . .
1.8 Chapter Review . . . . . . . . . . . . . . . . . . . . . .
2 Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Sending Data to Another Form . . . . . . . . . . . . .
2.1.1 Action Attribute . . . . . . . . . . . . . . . . .
2.1.2 Hidden Field Technique . . . . . . . . . . . .
2.1.3 Sending Data to Either of Two Pages . . . . .
2.1
1
1
2
2
3
3
4
5
10
12
12
14
14
16
16
17
19
21
22
24
27
27
27
29
29
32
32
33
35
39
ix
x
3
Contents
2.2 Using a Controller . . . . . . . . . . . . . . . . . . . .
2.2.1 Controller Details . . . . . . . . . . . . . . . .
JSP Controller . . . . . . . . . . . . . . . . . .
2.2.2
2.2.3
JSPs versus Servlets . . . . . . . . . . . . . . .
2.2.4 Controller Servlet . . . . . . . . . . . . . . . .
2.2.5 Servlet Access . . . . . . . . . . . . . . . . . .
2.2.6 Servlet Directory Structure
. . . . . . . . . .
2.2.7 Servlet Engine for a Servlet . . . . . . . . . .
Servlets in NetBeans
2.3
. . . . . . . . . . . . . . . . . . .
2.3.1 Source Packages . . . . . . . . . . . . . . . . .
2.3.2
Including Source Files in a WAR File . . . . .
2.3.3 Web Application Files . . . . . . . . . . . . . .
2.4
Summary . . . . . . . . . . . . . . . . . . . . . . . . .
2.5 Chapter Review . . . . . . . . . . . . . . . . . . . . . .
Java Bean: Default Validation
Java Beans and Controller Helpers . . . . . . . . . . . . . .
3.1 Application: Start Example . . . . . . . . . . . . . . .
Java Bean . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2
3.2.1 Creating a Data Bean . . . . . . . . . . . . . .
3.2.2 Using the Bean in a Web Application . . . . .
3.3 Application: Data Bean . . . . . . . . . . . . . . . . . .
3.3.1 Controller: Data Bean . . . . . . . . . . . . . .
3.3.2 Accessing the Bean in the JSP . . . . . . . . .
3.3.3
JSPs: Data Bean . . . . . . . . . . . . . . . . .
3.4 Application: Default Validation . . . . . . . . . . . . .
3.4.1
. . . . . . . . .
3.4.2 Controller: Default Validation . . . . . . . . .
3.5 Member Variables in Servlets . . . . . . . . . . . . . .
3.5.1 Threads . . . . . . . . . . . . . . . . . . . . . .
3.5.2 The Problem with Member Variables . . . . .
3.5.3 Local versus Member Variables . . . . . . . .
3.6 Application: Shared Variable Error . . . . . . . . . . .
. . . . . . .
3.7 Reorganising the Controller
. . . . . . . . . . . . . .
3.7.1 Creating the Helper Base . . . . . . . . . . . .
3.7.2 Creating the Controller Helper . . . . . . . .
3.7.3
JSPs: Reorganised Controller . . . . . . . . . .
3.7.4 Controller: Reorganised Controller . . . . . .
3.8 Application: Reorganised Controller . . . . . . . . . .
3.9 Model, View, Controller . . . . . . . . . . . . . . . . .
3.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . .
3.11 Chapter Review . . . . . . . . . . . . . . . . . . . . . .
3.6.1 Controller: Shared Variable Error
42
43
45
49
49
52
55
57
58
58
59
59
60
61
64
64
66
67
69
70
71
72
73
74
74
75
77
77
78
79
80
80
83
84
85
88
89
90
91
91
92
4 Enhancing the Controller . . . . . . . . . . . . . . . . . . . .
Logging in Web Applications . . . . . . . . . . . . . .
4.1.1 Logging with Log4j . . . . . . . . . . . . . . .
4.1.2 Confi guring Log4j . . . . . . . . . . . . . . . .
4.1.3 Retrieving the Logger . . . . . . . . . . . . . .
Eliminating Hidden Fields . . . . . . . . . . . . . . .
4.2.1 Retrieving Data from the Session . . . . . . .
4.1
4.2
95
95
96
96
100
101
102