0% found this document useful (0 votes)
93 views103 pages

Guide To Web Development With Java - Understanding Website Creation 2nd Edition Tim Downey Instant Download

Educational material: Guide to Web Development with Java - Understanding Website Creation 2nd Edition Tim Downey Interactive Study Materials. Premium educational package featuring engaging analysis, expert commentary, and comprehensive learning content.

Uploaded by

fmibuwda5310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views103 pages

Guide To Web Development With Java - Understanding Website Creation 2nd Edition Tim Downey Instant Download

Educational material: Guide to Web Development with Java - Understanding Website Creation 2nd Edition Tim Downey Interactive Study Materials. Premium educational package featuring engaging analysis, expert commentary, and comprehensive learning content.

Uploaded by

fmibuwda5310
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 103

Guide to Web Development with Java -

Understanding Website Creation 2nd Edition Tim


Downey pdf download
https://textbookfull.com/product/guide-to-web-development-with-java-understanding-website-
creation-2nd-edition-tim-downey/

★★★★★ 4.9/5.0 (45 reviews) ✓ 98 downloads ■ TOP RATED


"Excellent quality PDF, exactly what I needed!" - Sarah M.

DOWNLOAD EBOOK
Guide to Web Development with Java - Understanding Website
Creation 2nd Edition Tim Downey pdf download

TEXTBOOK EBOOK TEXTBOOK FULL

Available Formats

■ PDF eBook Study Guide TextBook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Collection Highlights

Learn Java for Web Development Modern Java Web Development


Layka Vishal

Website Hosting and Migration with Amazon Web Services: A


Practical Guide to Moving Your Website to AWS 1st Edition
Jason Nadon (Auth.)

Web Development for beginners Learn HTML CSS Javascript


step by step with this Coding Guide Programming Guide for
beginners Website development 1st Edition Mastery

Think Java How to Think Like a Computer Scientist 2nd


Edition Allen B. Downey
Flask Web Development Developing Web Applications With
Python 2nd Edition Miguel Grinberg

Web App Development Made Simple with Streamlit A web


developer s guide to effortless web app development
deployment 1 / converted Edition Rosario Moscato

Complete Vue js 2 Web Development Practical guide to


building end to end web development solutions with Vue js
2 1st Edition Mike Street

Tango With Django: A beginner’s Guide to Web Development


With Python / Django 1.9 1st Edition Leif Azzopardi

The Definitive Guide to JSF in Java EE 8: Building Web


Applications with JavaServer Faces Bauke Scholtz
Texts in Computer Science

Tim Downey

Guide to Web
Development
with Java
Understanding Website Creation
Second Edition
Texts in Computer Science

Series Editors
David Gries, Department of Computer Science, Cornell University, Ithaca, NY,
USA
Orit Hazzan , Faculty of Education in Technology and Science, Technion—Israel
Institute of Technology, Haifa, Israel
More information about this series at http://www.springer.com/series/3191
Tim Downey

Guide to Web
Development with Java
Understanding Website Creation
Second Edition

123
Tim Downey
School of Computing
and Information Sciences
Florida International University
Miami, FL, USA

ISSN 1868-0941 ISSN 1868-095X (electronic)


Texts in Computer Science
ISBN 978-3-030-62273-2 ISBN 978-3-030-62274-9 (eBook)
https://doi.org/10.1007/978-3-030-62274-9
1st edition: © Springer-Verlag London Limited 2012
2nd edition: © Springer Nature Switzerland AG 2021
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part
of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission
or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this
publication does not imply, even in the absence of a specific statement, that such names are exempt from
the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this
book are believed to be true and accurate at the date of publication. Neither the publisher nor the
authors or the editors give a warranty, expressed or implied, with respect to the material contained
herein or for any errors or omissions that may have been made. The publisher remains neutral with regard
to jurisdictional claims in published maps and institutional affiliations.

This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
To Bobbi, my sweetheart, with all my love.
The magic continues.
Preface

This book is about developing web applications. Over the years, more and more
frameworks have appeared that hide the details of the communication between the
browser and the server. These packages are fantastic for developing applications,
but an understanding of the underlying process can help understand the reason that
frameworks do what they do.
In writing this book, I read the Spring documentation in detail and reviewed many
questions from Stack Overflow. The problems I encountered were that many sear-
ches did not return the most current version of documentation. Frequently, I had to
check that I wasn’t reading about version 1 features instead of version 5. Similarly,
many relevant answers to questions are buried deep in the search results, since older
answers have been around much longer and appear at the top of the search.
My hope is that this book will present material from the basics of HTML and
HTTP to the intricacies of web services in a step-by-step manner, adding only a few
details at a time. Some topics have multiple implementations that produce similar
results. I hope that the distinctions between these implementations are made clear.
The book develops a framework in the first few chapters and then switches to the
Spring framework for implementing websites. There are many frameworks on the
market. Spring is popular and Spring Boot is an excellent introductory package.
I want students to understand the actual details that a framework hides, and to use a
framework to solve problems. In the future, when they are introduced to other
frameworks, they will understand them better.
I am grateful to the community of web developers, who have provided all the
excellent tools for creating web applications: Apache, Tomcat, Hibernate, Java
Servlets, Java Server Pages, NetBeans, Eclipse, Log4j, Apache Commons, Google
web services, FedEx web services, PayPal web services, JBoss Community, Spring,
and Maven.
I am thankful to Bobbi, my sweetheart, for all of her love and support. Without
Bobbi, this book would not have been finished. I also want to thank Kip Irvine for
encouraging me to write. Without Kip, this book would not have been started.

Miami, USA Tim Downey

vii
Contents

1 Web Applications and Maven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1


1.1 Hypertext Transfer Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Request Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2 Response Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3 Content Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Markup Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2.1 Hypertext Markup Language . . . . . . . . . . . . . . . . . . . . 5
1.2.2 Basic Tags for a Web Page . . . . . . . . . . . . . . . . . . . . . 7
1.2.3 What is the HT in HTML? . . . . . . . . . . . . . . . . . . . . . 12
1.3 HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.3.1 Form Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.3.2 Representing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.3.3 Transmitting Data Over the Web . . . . . . . . . . . . . . . . . 19
1.4 Web Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.4.1 Directory Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.5 Maven . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.1 Maven Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.2 Maven Web Application . . . . . . . . . . . . . . . . . . . . . . . 22
1.5.3 Maven from the Command Line . . . . . . . . . . . . . . . . . 24
1.5.4 Maven in an IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.5.5 Maven: Adding A Servlet Engine . . . . . . . . . . . . . . . . 26
1.6 Processing Form Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.6.1 JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.6.2 Initialising Form Elements . . . . . . . . . . . . . . . . . . . . . 30
1.7 The Truth About JSPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.7.1 Servlet for a JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
1.7.2 Handling a JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
1.8 Tomcat and IDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.8.1 Web Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
1.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
1.10 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

ix
x Contents

2 Controllers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.1 Sending Data to Another Form . . . . . . . . . . . . . . . . . . . . . . . . 46
2.1.1 Action Attribute . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.1.2 Hidden Field Technique . . . . . . . . . . . . . . . . . . . . . . . 49
2.1.3 Sending Data to Either of Two Pages . . . . . . . . . . . . . 53
2.2 Using a Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.2.1 Controller Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
2.2.2 JSP Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.2.3 JSPs Versus Servlets . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.2.4 Controller Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
2.2.5 Servlet Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.2.6 Servlet Directory Structure . . . . . . . . . . . . . . . . . . . . . 71
2.2.7 Servlet Engine for a Servlet . . . . . . . . . . . . . . . . . . . . 74
2.3 Maven Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.3.1 Automatic Deployment . . . . . . . . . . . . . . . . . . . . . . . . 75
2.3.2 Debugging Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . 78
2.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.5 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
3 Java Beans and Member Variables . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.1 Application: Start Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.2 Java Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
3.2.1 Creating a Data Bean . . . . . . . . . . . . . . . . . . . . . . . . . 89
3.2.2 Using the Bean in a Web Application . . . . . . . . . . . . . 90
3.3 Application: Data Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3.3.1 Mapping: Data Bean . . . . . . . . . . . . . . . . . . . . . . . . . . 92
3.3.2 Controller: Data Bean . . . . . . . . . . . . . . . . . . . . . . . . . 93
3.3.3 Data Access in a View . . . . . . . . . . . . . . . . . . . . . . . . 94
3.3.4 Views: Data Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
3.4 Application: Default Validation . . . . . . . . . . . . . . . . . . . . . . . . 96
3.4.1 Java Bean: Default Validation . . . . . . . . . . . . . . . . . . . 96
3.4.2 Controller: Default Validation . . . . . . . . . . . . . . . . . . . 98
3.5 Member Variables in Servlets . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.5.1 Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.5.2 The Problem with Member Variables . . . . . . . . . . . . . 100
3.5.3 Local Versus Member Variables . . . . . . . . . . . . . . . . . 103
3.6 Application: Shared Variable Error . . . . . . . . . . . . . . . . . . . . . 104
3.6.1 Controller: Shared Variable Error . . . . . . . . . . . . . . . . 104
3.7 Application: Restructured Controller . . . . . . . . . . . . . . . . . . . . 107
3.7.1 Creating the Helper Base . . . . . . . . . . . . . . . . . . . . . . 108
3.7.2 Creating the Controller Helper . . . . . . . . . . . . . . . . . . 109
3.7.3 Views: Restructured Controller . . . . . . . . . . . . . . . . . . 112
3.7.4 Controller: Restructured Controller . . . . . . . . . . . . . . . 114
Contents xi

3.7.5 Restructured Controller Analysis . . . . . . . . . . . . . . . . . 114


3.7.6 File Structure: Restructured Controller . . . . . . . . . . . . . 114
3.8 Model, View, Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
3.10 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
4 Spring Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
4.1 Spring Boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
4.1.1 Power of Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
4.1.2 Injection Through Autowiring . . . . . . . . . . . . . . . . . . . 123
4.2 Application: Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . 129
4.2.1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
4.2.2 Command Line Arguments . . . . . . . . . . . . . . . . . . . . . 133
4.2.3 Main Class: Command Line . . . . . . . . . . . . . . . . . . . . 133
4.3 Application: Spring MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
4.3.1 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
4.3.2 Servlets and Controllers . . . . . . . . . . . . . . . . . . . . . . . 137
4.3.3 Static Content Locations . . . . . . . . . . . . . . . . . . . . . . . 139
4.3.4 Location of the View Pages . . . . . . . . . . . . . . . . . . . . 139
4.3.5 Request Data Interface . . . . . . . . . . . . . . . . . . . . . . . . 144
4.3.6 Bean Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
4.3.7 Singleton Controllers . . . . . . . . . . . . . . . . . . . . . . . . . 149
4.3.8 Retrieving HTTP Variables . . . . . . . . . . . . . . . . . . . . . 150
4.4 Application: Spring Restructured Controller . . . . . . . . . . . . . . . 151
4.4.1 Modified Controller . . . . . . . . . . . . . . . . . . . . . . . . . . 152
4.5 Maven Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.5.1 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
4.5.2 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
4.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
4.7 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
5 Spring MVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
5.1 Eliminating Hidden Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
5.1.1 Session Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
5.1.2 Spring Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
5.1.3 Modifying the Controller . . . . . . . . . . . . . . . . . . . . . . 175
5.2 Controller Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
5.2.1 Encapsulating with Methods . . . . . . . . . . . . . . . . . . . . 179
5.2.2 Multiple Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
5.3 POST Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.3.1 POST Versus GET . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
5.3.2 Using Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
xii Contents

5.4 Replacing the Request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188


5.4.1 Adding to the Model . . . . . . . . . . . . . . . . . . . . . . . . . 188
5.4.2 Model in a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
5.4.3 Model in a Controller . . . . . . . . . . . . . . . . . . . . . . . . . 191
5.5 Navigation Without the Query String . . . . . . . . . . . . . . . . . . . . 196
5.5.1 Using Path Info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
5.5.2 Default Request Mapping . . . . . . . . . . . . . . . . . . . . . . 198
5.6 Session Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
5.6.1 Class Annotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
5.6.2 Parameter Annotation . . . . . . . . . . . . . . . . . . . . . . . . . 200
5.6.3 Logical Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
5.6.4 Conversational Storage . . . . . . . . . . . . . . . . . . . . . . . . 204
5.6.5 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
5.7 Logging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
5.7.1 Logback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
5.7.2 Configuring the Logger . . . . . . . . . . . . . . . . . . . . . . . . 207
5.7.3 Retrieving the Logger . . . . . . . . . . . . . . . . . . . . . . . . . 211
5.7.4 Adding a Logger in the Bean . . . . . . . . . . . . . . . . . . . 212
5.8 Application: Enhanced Controller . . . . . . . . . . . . . . . . . . . . . . 213
5.8.1 Views: Enhanced Controller . . . . . . . . . . . . . . . . . . . . 214
5.8.2 Model: Enhanced Controller . . . . . . . . . . . . . . . . . . . . 216
5.8.3 Controller: Enhanced Controller . . . . . . . . . . . . . . . . . 218
5.9 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
5.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
5.11 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
6 Validation and Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
6.1 Required Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
6.1.1 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . 228
6.1.2 Required Validation . . . . . . . . . . . . . . . . . . . . . . . . . . 232
6.2 Application: Required Validation . . . . . . . . . . . . . . . . . . . . . . . 240
6.2.1 Views: Required Validation . . . . . . . . . . . . . . . . . . . . 241
6.2.2 Model: Required Validation . . . . . . . . . . . . . . . . . . . . 242
6.2.3 Controller: Required Validation . . . . . . . . . . . . . . . . . . 243
6.3 Additional Binders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
6.3.1 Custom Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
6.3.2 Custom Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
6.4 Java Persistence API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
6.4.1 JPA Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
6.4.2 Persistent Annotations . . . . . . . . . . . . . . . . . . . . . . . . 256
6.4.3 Accessing the Database . . . . . . . . . . . . . . . . . . . . . . . 259
6.4.4 Data Persistence in Hibernate . . . . . . . . . . . . . . . . . . . 275
6.5 Application: Persistent Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Contents xiii

6.5.1 Views: Persistent Data . . . . . . . . . . . . . . . . . . . . . . . . 277


6.5.2 Repository: Persistent Data . . . . . . . . . . . . . . . . . . . . . 278
6.5.3 Controller: Persistent Data . . . . . . . . . . . . . . . . . . . . . 278
6.6 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
6.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
6.8 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
7 Advanced HTML and Form Elements . . . . . . . . . . . . . . . . . . . . . . 287
7.1 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
7.2 HTML Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
7.2.1 In-Line and Block Tags . . . . . . . . . . . . . . . . . . . . . . . 289
7.2.2 General Style Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
7.2.3 Layout Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
7.3 Cascading Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
7.3.1 Adding Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
7.3.2 Defining Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
7.3.3 Custom Layout with CSS . . . . . . . . . . . . . . . . . . . . . . 303
7.4 Form Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
7.4.1 Input Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
7.4.2 Textarea Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
7.4.3 Select Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
7.5 Spring Form Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
7.5.1 Spring Input Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
7.5.2 Spring Textarea Tag . . . . . . . . . . . . . . . . . . . . . . . . . . 315
7.5.3 Spring Select Elements . . . . . . . . . . . . . . . . . . . . . . . . 315
7.5.4 Initialising Form Elements . . . . . . . . . . . . . . . . . . . . . 316
7.6 Bean Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
7.6.1 Bean Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
7.6.2 Filling the Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
7.6.3 Accessing Multiple-Valued Properties . . . . . . . . . . . . . 319
7.7 Application: Complex Elements . . . . . . . . . . . . . . . . . . . . . . . . 320
7.7.1 Controller: Complex Elements . . . . . . . . . . . . . . . . . . . 320
7.7.2 Views: Complex Elements . . . . . . . . . . . . . . . . . . . . . 321
7.7.3 Model: Complex Elements . . . . . . . . . . . . . . . . . . . . . 324
7.8 Validating Multiple Choices . . . . . . . . . . . . . . . . . . . . . . . . . . 326
7.9 Application: Complex Validation . . . . . . . . . . . . . . . . . . . . . . . 327
7.9.1 Model: Complex Validation . . . . . . . . . . . . . . . . . . . . 327
7.9.2 Views: Complex Validation . . . . . . . . . . . . . . . . . . . . 328
7.9.3 Controller: Complex Validation . . . . . . . . . . . . . . . . . . 329
7.10 Saving Multiple Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
7.11 Application: Complex Persistent . . . . . . . . . . . . . . . . . . . . . . . 332
7.11.1 Model: Complex Persistent . . . . . . . . . . . . . . . . . . . . . 332
7.11.2 Views: Complex Persistent . . . . . . . . . . . . . . . . . . . . . 332
xiv Contents

7.11.3 Repository: Complex Persistent . . . . . . . . . . . . . . . . . . 333


7.11.4 Controller: Complex Persistent . . . . . . . . . . . . . . . . . . 334
7.12 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
7.13 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
8 Accounts–Cookies–Carts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
8.1 Retrieving From The Database . . . . . . . . . . . . . . . . . . . . . . . . 344
8.1.1 Finding a Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
8.1.2 Validating a Single Property . . . . . . . . . . . . . . . . . . . . 346
8.1.3 Retrieving a Record . . . . . . . . . . . . . . . . . . . . . . . . . . 347
8.2 Application: Account Login . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
8.2.1 Model: Account Login . . . . . . . . . . . . . . . . . . . . . . . . 350
8.2.2 Views: Account Login . . . . . . . . . . . . . . . . . . . . . . . . 352
8.2.3 Controller: Account Login . . . . . . . . . . . . . . . . . . . . . 353
8.3 Removing Rows from the Database . . . . . . . . . . . . . . . . . . . . . 355
8.3.1 Delete Fragment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
8.3.2 Delete Repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
8.3.3 Controller: Delete Record . . . . . . . . . . . . . . . . . . . . . . 356
8.4 Application: Account Removal . . . . . . . . . . . . . . . . . . . . . . . . 357
8.4.1 Views: Account Removal . . . . . . . . . . . . . . . . . . . . . . 357
8.4.2 Controller: Account Removal . . . . . . . . . . . . . . . . . . . 357
8.5 Account Number in Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
8.5.1 Handler Modifications for the Path . . . . . . . . . . . . . . . 359
8.5.2 Model: Path Controller . . . . . . . . . . . . . . . . . . . . . . . . 362
8.5.3 Controller: Path Controller . . . . . . . . . . . . . . . . . . . . . 362
8.5.4 Views: Path Controller . . . . . . . . . . . . . . . . . . . . . . . . 364
8.6 Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
8.6.1 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
8.6.2 Cookie Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
8.7 Application: Cookie Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
8.7.1 View: Cookie Test . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
8.7.2 Showing Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
8.7.3 Setting Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
8.7.4 Deleting Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
8.7.5 Finding Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
8.7.6 Path Specific Cookies . . . . . . . . . . . . . . . . . . . . . . . . . 372
8.8 Application: Account Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . 373
8.8.1 Views: Account Cookie . . . . . . . . . . . . . . . . . . . . . . . 373
8.8.2 Controller: Account Cookie . . . . . . . . . . . . . . . . . . . . 374
8.9 Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
8.9.1 Cart Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
8.9.2 Create Cart Item Database . . . . . . . . . . . . . . . . . . . . . 384
8.9.3 Model: Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . 386
Contents xv

8.10 Application: Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . 390


8.10.1 Design Choices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
8.10.2 Controller: Browse . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
8.10.3 Controller: Shopping Cart . . . . . . . . . . . . . . . . . . . . . . 393
8.10.4 Views: Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . 395
8.10.5 Shopping Cart: Enhancement . . . . . . . . . . . . . . . . . . . 400
8.11 Persistent Shopping Cart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
8.12 Application: Persistent Shopping Cart . . . . . . . . . . . . . . . . . . . 402
8.12.1 Model: Persistent Shopping Cart . . . . . . . . . . . . . . . . . 402
8.12.2 Views: Persistent Shopping Cart . . . . . . . . . . . . . . . . . 403
8.12.3 Repository: Persistent Shopping Cart . . . . . . . . . . . . . . 405
8.12.4 Controller: Persistent Shopping Cart . . . . . . . . . . . . . . 405
8.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
8.14 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
9 Web Services and Legacy Databases . . . . . . . . . . . . . . . . . . . . . . . . 411
9.1 Application: Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
9.1.1 Model: Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . 412
9.1.2 Handler: Process Google Maps . . . . . . . . . . . . . . . . . . 413
9.1.3 Views: Google Maps . . . . . . . . . . . . . . . . . . . . . . . . . 413
9.1.4 API Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
9.2 FedEx: Rate Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
9.2.1 Expanding the WSDL File . . . . . . . . . . . . . . . . . . . . . 416
9.2.2 FedEx: Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
9.2.3 Application: FedEx . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
9.2.4 Model: FedEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
9.2.5 Views: FedEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
9.2.6 Controller: FedEx . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
9.3 PayPal Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
9.3.1 Credentials: PayPal . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
9.3.2 Application: PayPal . . . . . . . . . . . . . . . . . . . . . . . . . . 431
9.3.3 Controller: PayPal . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
9.3.4 Views: PayPal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
9.3.5 Application: PayPal with Oauth . . . . . . . . . . . . . . . . . 434
9.4 Legacy Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
9.4.1 Eclipse Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
9.4.2 Install the Database Driver . . . . . . . . . . . . . . . . . . . . . 443
9.4.3 Hibernate Console . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
9.6 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
10 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
10.1 Spring: Object Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
10.2 Classpath and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
xvi Contents

10.2.1 Usual Suspects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453


10.2.2 What is a Package? . . . . . . . . . . . . . . . . . . . . . . . . . . 454
10.3 MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
10.3.1 Configuring MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . 454
10.3.2 MySql Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
10.4 Old School . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
10.4.1 Validation the Hard Way . . . . . . . . . . . . . . . . . . . . . . 456
10.4.2 Initialising Complex Elements . . . . . . . . . . . . . . . . . . . 463
10.4.3 Application: Old SchoolInitialised Complex
Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
10.5 Source Code of Complicated Controllers . . . . . . . . . . . . . . . . . 480
10.5.1 Servlet for a JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
10.5.2 Controller Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
10.5.3 Restructured Controller . . . . . . . . . . . . . . . . . . . . . . . . 484
10.5.4 Spring Restructured Controller . . . . . . . . . . . . . . . . . . 485
10.5.5 Enhanced Controller . . . . . . . . . . . . . . . . . . . . . . . . . . 486
10.5.6 Persistent Controller . . . . . . . . . . . . . . . . . . . . . . . . . . 488
10.5.7 Complex Persistent Controller . . . . . . . . . . . . . . . . . . . 490
10.5.8 Account Path and Shopping Cart . . . . . . . . . . . . . . . . . 493

Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
Web Applications and Maven
1

This chapter explains how information is sent between a browser and a server. It
begins with descriptions of the request from a browser and a response from a server.
Each has a format that is determined by the Hypertext Transfer Protocol [HTTP].
Basic HTML tags are introduced next, followed by HTML forms for collecting
data. Data is easily passed from one page to another, but the data cannot be
processed without a dynamic engine like a servlet engine. Maven is introduced as a
development environment that easily incorporates a servlet engine into an appli-
cation. Using Maven, a web server can be started that hosts a web application that
can be compiled and run. The chapter explains markup languages, with a detailed
description of the Hypertext Markup Language [HTML], which sends formatted
content from the server to the browser. An important feature of HTML is its ability
to easily request additional information from the server through the use of hypertext
links. HTML forms are covered. These send data from the browser back to the
server. Information from the form must be formatted so that it can be sent over the
web. The browser and server handle encoding and decoding the data.
Simple web pages cannot process form data that is sent to them. One way to
process form data is to use a web application and a Java Server Page [JSP]. In a
JSP, the Expression Language [EL] simplifies access to the form data and can be
used to initialise the form elements with the form data that is sent to the page.
JSPs are processed by a program known as a servlet engine. The servlet engine
receives the request and response data from the web server and processes the
request from the browser. The servlet engine translates all JSPs into programs
known as servlets.
Servlets and JSPs must be run from a servlet engine. Maven has the ability to
embed a Tomcat servlet engine into the application.

© Springer Nature Switzerland AG 2021 1


T. Downey, Guide to Web Development with Java, Texts in Computer Science,
https://doi.org/10.1007/978-3-030-62274-9_1
2 1 Web Applications and Maven

1.1 Hypertext Transfer Protocol

Whenever someone accesses a web page on the Internet, two computers commu-
nicate. One computer has a software program known as a browser, the other
computer has a software program known as a web server. The browser sends a
request to the server and the server sends a response to the browser. The request
contains the name of the page that is being requested and information about the
browser that is making the request. The response contains the page that was
requested (if it is available), information about the page, and information about the
server. Figure 1.1 depicts the aspects of the request and response.
When the browser makes the request, it mentions the protocol that it is using:
HTTP/1.1. When the server sends the response, it also identifies the protocol it is
using: HTTP/1.1. A protocol is not a language; it is a set of rules that must be
followed. For instance, one rule in HTTP is that the first line of a request will
contain the type of request, the address of the page on the server and the version of
the protocol that the browser is using. Another rule is that the first line of the
response will contain the version of the protocol that the server is using, a numeric
code indicating the success of the request and a sentence describing the code.
Protocols are used in many places, not just with computers. When the leaders of
two countries meet, they must decide on a common protocol in order to commu-
nicate. Do they bow or shake hands when they meet? Do they eat with chopsticks or
silverware? It is the same situation for computers, in order for the browser and
server to communicate, they must decide on a common protocol.

Fig. 1.1 The request and response have specific formats, as specified by HTTP
1.1 Hypertext Transfer Protocol 3

1.1.1 Request Format

The request from the browser has the following format in HTTP:

a. The first line contains the type of request, the name of the requested page and
the protocol that is being used.
b. Subsequent lines are the request headers. They contain information about the
browser and the request.
c. A blank line in the request indicates the end of the request headers
d. In a POST request, additional information can be included after the blank line.

Typical information that is contained in the request headers is the brand of the
browser that is making the request, the types of content that the browser prefers, the
languages and character set that the browser prefers and the type of connection that
is being used. The names of these request headers are User-agent, Accept,
Accept-language and Accept-charset, respectively (Table 1.1).

1.1.2 Response Format

The response from the server has the following format in HTTP:

a. The first line contains the protocol being used, the status code and a brief
description of the status.
b. Subsequent lines are the response headers. They contain information about the
server and the response.
c. A blank line in the response indicates the end of the response headers.
d. In a successful response, the content of the page will be sent after the blank line.

Typical information that is contained in the response headers is the brand of the
server that is making the response, the type of the file that is being returned and the
number of characters that are in the file. The names of these response headers are
Server, Content-Type and Content-length, respectively (Table 1.2).

Table 1.1 Common request headers


User-agent Identifies the type of browser that made the request
Accept Specifies the MIME types that the browser prefers
Accept-language Indicates the user’s preferred language, if multiple versions of the
document exist
Accept-charset Indicates the user’s preferred character set. Different character sets can
display characters from different languages
4 1 Web Applications and Maven

Table 1.2 Common response headers


Server Identifies the type of server that made the response
Content-type Identifies the MIME type of the response
Content-length Contains the number of characters that are in the response

1.1.3 Content Type

The server must also identify the type of information that is being sent. This is
known as the Content Type. Different content types define text, graphics, spread-
sheets, word processors and more.
These content types are expressed as Multipurpose Internet Mail Exten-
sions [MIME] types. MIME types are used by web servers to declare the type of
content that is being sent. MIME types are used by the browser to decode the type
of content that is being received. If additional data is included with the request, the
browser uses special MIME types and additional request headers to inform the
server. The server and browser will each contain a file that has a table of MIME
types with the associated file extension for that type.
The basic structure of a MIME type is a general type, a slash and a specific type.
For example, the general type for text has several specific types, for plain text,
HTML text and style sheet text. These types are represented as text/plain, text/html
and text/css, respectively. When the server sends a file to the browser, it will also
include the MIME type for the file in the header that is sent to the browser.
MIME types are universal. All systems have agreed to use MIME types to
identify the content of a file transmitted over the web. File extensions are too
limiting for this purpose. Many different word processor programs might use the
extension .doc to identify a file. For instance, .doc might refer to an MS WORD
document or to an MS WORDPAD document. It is impossible to tell from the
extension which program actually created the program. In addition, other programs
could use the .doc extension to identify a program: for instance, WordPerfect could
also use the .doc extension. Using the extension to identify the content of the file
would be too confusing.
The most common content type on the web is HTML text, represented as the
MIME type text/html.

1.2 Markup Language

I am confident that most students have seen a markup language. I remember my


days in English composition classes: my returned papers would always have cryptic
squiggles written all over them (Fig. 1.2).
1.2 Markup Language 5

Fig. 1.2 Editors use markup to annotate text

Some of these would mean that a word was omitted (^), that two letters were
transposed (a sideways '' S ',' enclosing the transposed letters), or that a new para-
graph was needed (a backwards, double-stemmed '' P '). ' These marks were invalu-
able to the teacher who had to correct the paper because they conveyed a lot of
meaning in just a few pen strokes. Imagine if a program could accept such a paper
that is covered with markup, read the markup and generate a new version with all
the corrections made.
There are other forms of markup languages. The script of a play has a markup
language that describes the action that is proceeding while the dialog takes place.
For instance, the following is a hypothetical script for the 3 Stooges:

Moe: Oh, a wise guy, huh? <Pulls Larry’s hair>


Larry: It wasn’t me. <Hits Curly in the stomach>
Moe: What are you doing? <Tries to poke Curly in the eye>
Curly: Nyuk, nyuk, nyuk. <Places hand in front of eyes>
Moe: Ignoramus. <Bonks Curly on top of the head>

Every markup language has two parts.

a. The plain text


b. The markup, which contains additional information about the plain text.

1.2.1 Hypertext Markup Language

HTML is the markup language for the web. It is what allows the browser to display
colours, fonts, links and graphics. All markup is enclosed within the angle brack-
ets <and>. Directly adjacent to the opening bracket is the name of the tag. Addi-
tional attributes can be included after the name of the tag and before the closing
bracket.
HTML tags are intermixed with plain text. The plain textis what the viewer of a
web page will see. The HTML tags are commands to the browser for displaying the
6 1 Web Applications and Maven

text. In this example, the plain text ‘This text is strong’ is enclosed within the
HTML tags for making text look strong:

<strong> This text is strong</strong>

The viewer of the web page would not see the tags, but would see the text
rendered strongly. For most browsers, strong text is bold, and the sentence would
appear as:
This text is strong
HTML has two types of tags: singletons and paired tags.
Singletons have a limited amount of text embedded within them as attributes or
they have no text at all. Singletons only have one tag. Table 1.3 gives two examples
of singleton tags.
Paired tags are designed to contain many words and other tags. These tags have an
opening and a closing tag. The text that they control is placed between the opening and
closing tags. The closing tag is the same as the opening tag, except the tag name is
preceded by a forward slash /. Table 1.4 gives four examples of paired tags.

Table 1.3 Examples of singletons


Tag Explanation
<br> Insert a line break into the document
<input> Insert a form element into the document. This is a tag that has additional
attributes, which will be explained below

Table 1.4 Examples of paired tags


Tag Explanation
<strong> strong </strong> Typically, the enclosed text is rendered in a thicker
font
<ins> inserted </ins> Typically, the enclosed text is rendered with an
underline
<em> emphasised </em> Typically, the enclosed text is rendered in an italic font
<p> paragraph </p> The enclosed text will have at least one empty line
preceding it

Table 1.5 Two essential form element types


Type Example
text <input type=''text'' name=''hobby'' value=''''>
The value attribute is the text that appears within the element when the page is
loaded
submit <input type=''submit'' name=''nextButton'' value=''Next''>
The value attribute is the text that appears on the button in the browser
1.2 Markup Language 7

1.2.2 Basic Tags for a Web Page

We are very sophisticated listeners. We can understand many different accents. We


can understand when words are slurred together. However, if we were to write out
the phonetic transcription of our statements, they would be unreadable. There is a
correct way to write our language, but a sophisticated listener can detect and correct
many errors in pronunciation.
For instance, most English speakers would understand me if I asked the question
Jeet yet?
In print, it is incomprehensible. A proper response might be
No, joo?
Or,
Yeah, I ate.
As we become more proficient in a language, we are able to understand it, even
when people do not enunciate clearly.
In the same way, all markup languages have a format that must be followed in
order to be correct. Some language interpreters are more sophisticated than others
and can detect and correct mistakes in the written format. For example, a paragraph
tag in HTML is a paired tag and most browsers will render paragraphs correctly,
even if the closing paragraph tag is missing. The reason is that paragraph tags
cannot be nested one inside the other, so when a browser encounters a new <p> tag
before seeing the closing </p> for the current paragraph, the browser inserts a
closing </p> and then begins the new paragraph. However, if an XML interpreter
read the same HTML file with the missing </p> tag, the interpreter would report
an error instead of continuing to parse the file. It is better to code all the tags that are
defined for a well-formed HTML document, than to rely on browsers to fill in the
missing details.
Standard Tags
The HTML specification defines a group of standard tags that control the structure
of the HTML document. These three tags contain all the information for the page.

<html> html code </html>

The html tags enclose all the other tags and text in the document. It only contains
the following two sections.

<head> browser command tags </head>

The head tags enclose tags that inform the browser about how to display the
entire page. These control how the page appears in the browser, but do not
8 1 Web Applications and Maven

contain any content for the page. This paired tag belongs within the paired html
tags.
<body> body tags </body>

The body tags contain all the plain text and HTML tags that are to be displayed
in the browser window. This paired tag belongs within the paired html tags.
While the body section contains the normal HTML tags discussed in this chapter,
like strong and em, the head section contains special markup tags that indicate
how the browser should display the page. The meta and title tags belong in the head
section.

<title> title text </title>

The title tags enclose the text that will display in the title bar of the browser
window.

<meta charset = ''UTF-8''>

The meta tag is a singleton that indicates extra information for the browser. This
tag can be repeated to include different information for the browser. A standard
page should include a meta tag with charset='' utf-8 '.' This indicates the character
set for the language that is being used to display the page.
HTML Validation
The WWW Consortium [W3C] publishes the HTML standard and provides tools for
HTML validation that will test that a page has the correct HTML structure. In order
to comply with the HTML specification, all web pages should have the following
structure.

<!DOCTYPE HTML>
<html>
<head>
<meta charset=''utf-8''>
<title>Simple Page</title>
</head>
<body>
<p>
This is a <em>simple</em> web page.
</body>
</html>

a. The DOCTYPE defines the type of markup that is being used. It precedes the
html tag because it defines which version of HTML is being used.
1.2 Markup Language 9

b. All the tags and plaintext for the page are contained within the paired html
tags.

i. Place a head section within the paired html tags.

A. Place a paired title tag within the head section.


B. Place a singleton meta tag for the character set within the head section.

ii. Place a body section within the paired html tags.

c. The DOCTYPE and meta tags are required if the page is to be validated by W3C
for correct HTML syntax. Go to https://www.w3.org to access the HTML
validator.

There is no excuse for a web page to contain errors. With the use of the vali-
dation tool at https://www.w3.org, all HTML pages should be validated to ensure
that they contain all the basic tags.
Layout versus Style
Two types of information are contained in each HTML page: layout and style. The
basic layout is covered in this chapter; advanced layout and style are covered in
Chap. 7. Style information contains things like the colours and font for the page.
The recommended way to handle style and layout is to place all the layout tags in
the HTML page and to place all the style information in a separate file, called a
style sheet. For the interested student, the HTML and style information from
Chap. 7 can be read at any time.
Hypertext Markup Language Five [HTML5] is the latest version of the HTML
standard. In the previous versions, tags could specify the style of a page. In the new
version, those tags have been deprecated. In order to validate that a page conforms
to version 5, the tags that specify specific style cannot be used.
In previous versions of the HTML standard, different DOCTYPE statements
could be used for HTML pages: strict and transitional. The strict one was the
recommended one, since it enforced the rule that all style information be contained
in a separate file. Version five has no choices for the DOCTYPE: all pages must use
strict HTML. All pages for this book will use the new DOCTYPE for HTML5.

<!DOCTYPE HTML>

Word Wrap and White Space


Most of us type text in a word processor and let the program determine where the
line breaks belong. This is known as word wrap. The only time that we are required
to hit the enter key is when we want to start a new paragraph.
Browsers will use word wrap to display text, even if the enter key is pressed.
Browsers will treat a new line character, a tab character and multiple spaces as a
10 1 Web Applications and Maven

single space. In order to insert a new line, tab or multiple spaces in an HTML page,
markup must be used: if it is not plain text, then it must be placed in markup.
Browsers take word wrap one step further. Browsers will compress all con-
secutive white space characters into a single space character. The common white
space characters are the space, the tab and the new line character. If five spaces start
a line, they will be compressed into one space.
The following listing contains a web page that has a poem.

<!DOCTYPE HTML>
<html>
<head>
<meta charset=''utf-8''>
<title>A Poem</title>
</head>
<body>
Roses are red
Violets are blue
This could be a poem
But not a haiku
A haiku has a fixed structure. The first line has five
syllables, the second line has seven syllables and the
third line has five syllables. Therefore, the previous
poem cannot be a haiku.
</body>
</html>

Even though the poem has four lines, the poem will appear as one line in the
browser. This is because no markup was added to indicate that one line has ended
and another line should begin. The browser will wrap to a new line if the poem
would extend beyond the right margin of the browser.

Try It
https://bytesizebook.com/guide-boot/ch1/poem.html

Fig. 1.3 How the poem will appear in the browser


1.2 Markup Language 11

Open the link in a browser and view the poem (Fig. 1.3). Resize the window and
note how the browser will break the text in different places. If the window is large
enough, the entire page will be displayed on one line.
Line Breaks
Two of the tags that can start a new line are <br> and <p>. The <br> tag is short
for breakand starts a new line directly under the current line. It is a singleton tag, so
it does not have a closing tag. The <p> tag is short for paragraphand skips at least
one line and then starts a new line. It is a paired tag, so it is closed with
the </p> tag.
As was mentioned above, browsers have the ability to interpret HTML even if
some tags are missing. The closing paragraph tag is such a tag. It is not possible to
nest one paragraph inside another, so if the browser encounters two paragraph tags
without closing tags, as in <p> One <p> Two, then it will interpret this as <
p> One </p> <p> Two </p>. Even the validators at w3.org will accept HTML
that does not have closing paragraph tags.
Listing 1.1 contains the HTML page for the poem, using markup for line breaks
and paragraph tags.

<!DOCTYPE HTML>
<html>
<head>
<meta charset=''utf-8''>
<title>A Poem</title>
</head>
<body>
<p>
Roses are red<br>
Violets are blue<br>
This could be a poem<br>
But not a haiku<br>
<p>
A haiku has a fixed structure. The first line has five
syllables, the second line has seven syllables and the
third line has five syllables. Therefore, the previous
poem cannot be a haiku.
</body>
</html>

Listing 1.1 A four-line poem with markup for line breaks HTML
not are

of and magic

the

the what

occupation scantily

from

and largest

force between
be

not immediately the

most

beneath were offer

by

volumus

tlie PCs
of in

few

new

faith enabled the

individualized over It

undeniable it bill

anticipated produced
of There

he

the

nearly with some

pp pilgrim retreating

and an

Whereon

York the

of
Renaissance the

too Consultors railway

his back

unreliable

of ending

of in

everything the

of
sprino Ireland animals

of has and

Controversy Pair

heaven

so
antechamber the the

us consumption

that this

not feature the

000 will

which a meaning

to exerted Ages
thinking praeconio and

collector generally

found and is

religious

for classical s

incapable represent

much suppetant The

and fatigue it
names On the

should

It

as

and
one

and have who

studious and who

and

burns mere

the and

GERMAN line of

is in

is custom
militia One

in floods is

Rue laying in

naturata quantities

where which and


to From

great our and

such

in with

dissensions examination

LUCAS of a

the

the swamp

this

the was
hatred the pages

landowner the

object

and reach or

denied
terra

and thick

robber feudal the

fortnightly as

fidei placed

its to costly
and

devoid

widow

lumine landowner doubt

perception The will

Mr

there Nahant does

youthful be bound
the

army Wednesday known

are believe

to

approach of

to

points has their


Deo the

or

remote present

make by

F seen

he view of

Popular that of

differing On
him of

called

comes equipage

consequence

granite minions

mythological of of

is force omit
perplexing carrying

fell true

Society

volumes

fix forty

be advisable

be

This a

is but
progress all

more of

life chapter

or

populations of itself

has

hundred

should
of

no it have

but Universitate

The If

explanation a

that

large

of it

Roman cross idea


belong eyes a

surrounded be

Canada to the

est disproved near

by

show Hedonism

Cabinet

these of

the one

explorers Positivism said


arrangements

quite a clemency

the

directly room than

us

on titanic up
course transcribed

all atque reduced

miles the ocean

as agency

work

solemn and

the

vicum of should

scholar of century

not
the

and all

checks discarded

their reason

has the

greatly Church had


far

such long

rate

bees numbers

red snatch from

drank ought

done

the Tablet non

Daniel in fashion
Meeting ladies

others religious

state

of later

he contributed

certainly

while
spring doubles

of

indeed with the

root

the English

the just

holds the

to the

transmission entire and


not Secret religion

object assume

quote

disappeared

heard Plato introductory


to It

is interest

on

chief the

that scope can

properties and
St choosing

subjects spirits the

but

very

a 180 from

east the

not

worth having
else judges

his far high

getting old

has beverages

be

Father Christians

some Tablet the

precandi is and

there among log


convey

composed the oddly

Galileo the the

of dissent

as the

ritual of me
marsh in

wire is

bad

article of

Nor fail

is

if

English seem enough

works
directors of especial

Critias delay

attend

estimated religiosity

Lucetta the

of near

Irish basin

of in Lao

amount the
on as

and is the

hill

religious of sketching

far doors Jowett

and

rem may

favour

Armer oF text

fifty barrels
and he all

were

temporal

in can the

as the Tahernacula

The when
expedient china

as we rents

A practice time

of

of

no conscience

the that canopy

this group of

hand

honour folding the


j The feeble

everything

one t

and a is

doctrinaque vol of

are argue of

voyage made also

hurt Pius

public of what
of which place

which of

more by English

of Conservative to

patientia bring

Haunting of

The from workmen

coming 170 be
a considering

as

has oil

find a

the certain

at title Sea
numbers continues

glisten

falling himself insufficient

wgbh enough

places Tiibingen

at

Pink bestowed
the

and

discussion any attended

traps the representative

as

incorrect a

the much is

designed

discovery of
plain

between see

before

begins ac

Views to

and scenery persuaded

craft civilization

is
St

receives

strolen federal

to towards

discipline it it

life

immediate boldly would


the caravan the

flourishing pity

other Crown and

Petre and

followed

and Catholic the

the

manual

we

on now
day seal follow

eastward

other the results

to

certain

very
Men that in

in com Stations

and by American

the of

too

author consequently Latin

there of Host

oil years

are

main beings than


lost be sermons

Her Domino

on

in

Where Edwaud the

of statue

ordinary without qu8B

sees

New has

and of which
concur Morell they

course It and

that citations as

strengthen a

voyage peninsulae
tomb as as

on follow dependence

pleasant contest capital

and some

of the the

has the ability


own in

only and

saw lacking

to false

than

of was to

is the acres

last

and comfort these

Wiseman window
travel rare the

the unregarded we

selection but

straight at to
that the

or I

By missionary An

and institute into

of and so
s a Birmingham

materials

on more menacing

stay proper mode

wait

thou are struggling

strict
the not Mr

give Board the

from that dispute

internal consider intellect

opium of he

benevolentiam taking hero

has puppets words

even

arrows
the in strands

as prayer

experienced

way all

glittered of
our Clyde strew

said her terrible

we quorum

item policy

mechanical making what

the

to open the
Holy that all

the a

oil on Chinese

east

is

is
indefinite into

one D

ports on

energy and

The

the appease by

was the

can the spectre


more the

Anglican

practice and assure

attacked

of to India
trick

his minds regards

tze

cultivate and been

woods

strong elder laid

This of

A Pope on

difficult be rain
This should village

said a letter

quotations

and

many question

over Mellen
to Gamer

was both right

her of

to enactments

we the are
was whatever been

there the Tsaritzin

memory Catholic

denial troops

that this wilderness

had Here our


popular Conclusion less

who by

honour here by

Arimuric woman intends

if It System

centres his that


A

1882 French third

brother occidentali

a more of

had landlocked by

this

text

and

annulus

large proved
affirm in at

them settlers gave

very made Great

cockney enjoyed

plain obtains

to

to boring not
the

de

those wounded Africa

Governments a up

the

nequaquam of
of

the 4d

charge

7 track had

the

The
this is has

harvest

one

and validas

when

that ensure 82

the

conscientious

cultivators the the


Judseo

thousands he not

height of and

as Wady in

spreading

but

well great general

speakino

You might also like