Spring MVC: A Tutorial
Second Edition
Paul Deck
Spring MVC: A Tutorial, Second Edition
Second Edition: April 2016
ISBN: 9781771970310
Copyright 2016 by Brainy Software Inc.
Cover image Dollar Photo Club
All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without written permission from the publisher, except for the inclusion of brief quotations in a review.
Trademarks
Oracle and Java are registered trademarks of Oracle and/or it's affiliates
UNIX is a registered trademark of the Open Group
Apache is a trademark of The Apache Software Foundation.
Firefox is a registered trademark of the Mozilla Foundation.
Google is a trademark of Google, Inc.
Throughout this book the printing of trademarked names without the trademark symbol is for editorial purpose only. We have no intention of infringement of the trademark.
Warning and Disclaimer
Every effort has been made to make this book as accurate as possible. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information in this book.
About the Author
Paul Deck is a Java and Spring expert who has developed large-scale enterprise applications and is currentlyan independent contractor. He is the co-author of How Tomcat Works.
Table of Contents
Introduction
Welcome to Spring MVC: A Tutorial, Second Edition .
Spring MVC is a module in the Spring Framework (or Spring for short) for rapidly developing web applications. The MVC in Spring MVC stands for Model-View-Controller, a design pattern widely used in Graphical User Interface (GUI) development. This pattern is not only common in web development, but is also used in desktop technology like Swing and JavaFX.
Sometimes called Spring Web MVC, Spring MVC is one of the most popular web frameworks today and a most sought-after skill. This book is for you if you want to learn how to develop Java-based web applications with Spring MVC.
Learning Spring MVC will be much easier if you already master Spring Framework as well as Servlet and JavaServer Pages ( JSP), the two Java technologies upon which Spring MVC is based. If youre new to Spring, Chapter 1 presents a short tutorial on Spring Framework. If you are not familiar with Servlet and JSP, dont despair. You can find two crash courses that will help: Appendix C, Servlet and Appendix D, JavaServer Pages. If you are interested to know more about Servlet and JSP, I recommend Servlet and JSP: A Tutorial, Second Edition (ISBN 9781771970273) by Budi Kurniawan.
The rest of this introduction talks about HTTP, web programming with Servlet and JSP, and the content of the book.
The Hypertext Transfer Protocol (HTTP)
The Hypertext Transfer Protocol (HTTP) enables web servers and browsers to exchange data over the Internet or an intranet. The World Wide Web Consortium (W3C), an international community that develops standards, is responsible for revising and maintaining this protocol. The first version of HTTP was HTTP 0.9, which was then replaced by HTTP 1.0. Superseding HTTP 1.0 is HTTP 1.1, which is defined in the W3Cs Request for Comments (RFC) 2616 . At the time of writing, HTTP 1.1 is still the most popular version of HTTP. The current version, HTTP/2, was released in May 2015. Table I.1 shows the HTTP versions and their release dates. Note that the second major version of HTTP is commonly written as HTTP/2 rather than HTTP 2.
Version | Release Date |
HTTP 0.9 | 1991 |
HTTP 1.0 | 1996 |
HTTP 1.1 | First released in 1997 and updated in 1999 |
HTTP/2 | May 2015 |
Table I.1: HTTP versions and release dates
An HTTP server (also known as a web server) runs 24x7 waiting for HTTP clients (normally web browsers) to connect to it and ask for resources. In HTTP it is always the client that initiates a connection, a server is never in a position to contact a client.
To locate a resource, an Internet user would click a link that contains a Uniform Resource Locator ( URL) or enter one in the Location box of his/her browser. Here are two examples of URLs:
http://google.com/index.html
http://facebook.com/index.html
The first part of the URLs is http , which identifies the protocol. Not all URLs use HTTP. For instance, these two URLs are valid even though they are not HTTP-based URLs:
mailto:joe@example.com
ftp://marketing@ftp.example.org
In general an HTTP-based URL has this format:
protocol
://[
host
.]
domain
[:
port
][/
context
][/
resource
][?
query string
| path variable
]
or
protocol
://
IP address
[:
port
][/
context
][/
resource
][?
query string |
path variable
]
The parts in square brackets are optional, therefore a URL can be as simple as http://yahoo.ca or http://192.168.1.9. An Internet Protocol (IP) address, by the way, is a numerical label assigned to a computer or another device. In other words, instead of typing http://google.com, you can use its IP address: http://173.194.46.35. To find out the IP address of a domain, use the ping program on your computer console:
ping google.com
An IP address is hard to remember, so people prefer to use the domain. A computer may host more than one domain, so multiple domains can have the same IP address. And, did you know that you cant buy example.com or example.org because they are reserved for documentation purpose?
The host part of a URL may be present and identify a totally different location on the Internet or an intranet. For instance, http://yahoo.com (no host) brings you to a different location than http://mail.yahoo.com (with a host). Over the years www has been the most popular host name and become the default. Normally, http://www. domainName is mapped to http:// domainName .
80 is the default port of HTTP. Therefore, if a web server runs on port 80, you dont need the port number to reach the server. Sometimes, however, a web server doesnt run on port 80 and you need to type the port number. For example, Tomcat by default runs on port 8080, so you need to supply the port number when requesting a resource:
http://localhost:8080/index.html
localhost is a reserved name typically used to refer to the local computer, i.e. the same computer the web browser is running on.
The context part of a URL refers to the application name, but this is also optional. A web server can run multiple contexts (applications) and one of them can be configured to be the default context. To request a resource in the default context, you skip the context part in a URL.
Finally, a context can have one or more default resources (ordinarily index.html or index.htm or default.htm). A URL without a resource name is considered to identify a default resource. Of course, if more than one default resource exists in a context, the one with the highest priority will always be returned when a client does not specify a resource name.
After a resource name comes one or more query string or path variable. A query string is a key/value pair that can be passed to the server to be processed. Two query strings are separated by an ampersand (&). A path variable is like a query string, but contains only the value part. Two path variables are separated with a forward slash (/).
Next page