(Ebook) Client-Server Web Apps with JavaScript and Java: Rich, Scalable, and RESTful by Saternos, Casimir ISBN 9781449369330, 1449369332 all chapter instant download
(Ebook) Client-Server Web Apps with JavaScript and Java: Rich, Scalable, and RESTful by Saternos, Casimir ISBN 9781449369330, 1449369332 all chapter instant download
com
https://ebooknice.com/product/client-server-web-apps-with-
javascript-and-java-rich-scalable-and-restful-55585992
DOWLOAD EBOOK
https://ebooknice.com/product/client-server-web-apps-with-javascript-
and-java-4670268
ebooknice.com
ebooknice.com
https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018
ebooknice.com
ebooknice.com
(Ebook) Master SAT II Math 1c and 2c 4th ed (Arco Master
the SAT Subject Test: Math Levels 1 & 2) by Arco ISBN
9780768923049, 0768923042
https://ebooknice.com/product/master-sat-ii-math-1c-and-2c-4th-ed-
arco-master-the-sat-subject-test-math-levels-1-2-2326094
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.com
Client-Server Web Apps with JavaScript and Java
by Casimir Saternos
Copyright © 2014 EzGraphs, LLC. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or [email protected].
Editors: Simon St. Laurent and Allyson MacDonald Indexer: Judith McConville
Production Editor: Kristen Brown Cover Designer: Karen Montgomery
Copyeditor: Gillian McGarvey Interior Designer: David Futato
Proofreader: Amanda Kersey Illustrator: Rebecca Demarest
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Client-Server Web Apps with JavaScript and Java, the image of a large Indian civet, and related
trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark
claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-36933-0
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
iii
HelloWorld.java (with Variables) 27
Development Best Practices 29
Coding Style and Conventions 29
Browsers for Development 29
Integrated Development Environments 30
Unit Testing 31
Documentation 31
Project 31
4. Java Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Java Language 58
Java Virtual Machine (JVM) 58
Java Tools 60
Build Tools 61
Benefits of Maven 63
Functionality of Maven 64
Version Control 65
Unit Testing 65
JSON Java Libraries 66
iv | Table of Contents
Projects 66
Java with JSON 66
JVM Scripting Languages with JSON 69
Conclusion 72
5. Client-Side Frameworks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Overview 75
Starting Point One: Responsive Web Design 77
HTML5 Boilerplate 78
Bootstrap 79
Starting Point Two: JavaScript Libraries and Frameworks 79
Browser Compatibility 79
Frameworks 80
Functionality 80
Popularity 81
Obtaining Starter Projects 82
Download Directly from Repositories 82
Download from Starter Sites 82
IDE-Generated Starter Projects 83
The Rise of the Front-End Engineer 83
Client-Side Templating 84
Asset Pipelines 84
Development Workflow 85
Project 85
Conclusion 88
Table of Contents | v
Conclusion 107
vi | Table of Contents
DOM Traversal and Manipulation 141
Utility Functions 142
Effects 142
Event Handling 143
Ajax 143
jQuery and Higher-Level Abstractions 143
Project 144
Basic HTML 145
JavaScript and jQuery 145
Conclusion 147
C. References. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Table of Contents | ix
Preface
xi
client tier. I wanted the title of this book to encompass more than front-end innovations
and to recognize the corresponding server-side design and web service messaging.
The method of communication captured by the popular acronym REST (Representa‐
tional State Transfer) does suggest the web service messaging style. But the definition
of REST as specified by its author Roy Fielding is very limiting. On his blog, Fielding
lists specific restrictions to REST that are commonly violated in so-called RESTful APIs.
And some even question whether a JSON API can be truly RESTful due to the fact that
it does not satisfy all of the constraints associated with the style of architecture. There
is a continuum by which REST services can be described; so that an API can be described
as RESTful only to the degree that it adheres to the constraints. REST does include client-
server as one of its constraints, and the verb and URL naming conventions are certainly
applicable.
So a JavaScript client consuming messages from a pragmatic “RESTful” API is a signif‐
icant part of the method of development. What about the server component?
Java Enterprise Edition (JEE) includes the JAX-RS API, which uses Java’s flavor of REST
(which is not inherently strict) and is demonstrable using the Jersey reference imple‐
mentation. But limiting to JAX-RS web application development ignores frameworks
and alternate JVM language solutions that are available and particularly appealing for
quick prototypes.
And so crystallizing the intentions of a book in a simple, catchy title is not an easy task.
Fortunately, James Ward did a presentation at OSCON 2012 in which he described the
development of “Client-Server Web Applications with HTML5 and Java.” He listed the
benefits of a method of web application development that is increasingly popular, a
method that I have been involved with in recent years on various projects. And the
phrase “client-server” is the key to understanding what this method is. It captures the
fundamental architectural changes that include aspects of the terms listed above, but
represents the distinct partitioning between the client and server and considers each of
the roles significant.
A client-server architecture of web applications requires a shift (in some cases seismic)
in the way programmers work. This book was written to enable developers to deal with
this revolution. Specifically, it is intended to provide a proper perspective in building
the latest incarnation of modern web applications.
xii | Preface
recent developments in JavaScript and how the language and its development process
compare with those of Java, this book is for you.
A bit of a balancing act is evident as you read this book. On the one hand, the most
important thing you can take away is a sense of the “big picture”—the influences and
trends causing a shift in the technologies in use. On the other hand, technologies are
often best understood by seeing specific examples. If you are interested in an overview
of how these technologies actually fit together, you will benefit from this book.
My goal in writing this is to help you to make informed decisions. Good decisions result
in the right technologies being used on new projects. They allow you to avoid pitfalls
caused by mixing incompatible technologies or having the wrong expectations about
the implications of a given decision. They help you to step into projects in process and
better support existing code. In short, informed decisions will make you a more pro‐
ductive programmer. They help you make effective use of your time in researching areas
of specific interest in your work now and in the future.
Preface | xiii
Chapter 10 documents the development of a project using JRuby and Angular.
The final chapters detail projects using traditional Java web application servers and
libraries.
Chapter 11 looks at the range of packaging and deployment options available in the Java
ecosystem.
Chapter 12 explores virtualization and innovations emerging from the management of
large server environments.
Chapter 13 draws attention to testing and documentation.
Chapter 14 wraps up with some final thoughts on responding to the tumultuous changes
to Internet-related technologies and software development.
Appendix A describes how to explore and manipulate Java classes interactively.
xiv | Preface
Code Examples
Projects and code examples in this book are hosted on https://github.com/java-
javascript/client-server-web-apps. You can view them online or download a .zip file for
local use. The assets are organized by chapter.
The code examples provided in this book are geared toward illustrating specific func‐
tionality rather than addressing all concerns of a fully functional application. Differ‐
ences include:
That said, this book is intended to help you get your job done. In general, you may use
the code in this book in your programs and documentation. You do not need to contact
us for permission unless you are reproducing a significant portion of the code. For
example, writing a program that uses several sections of code from this book does not
require permission. Selling or distributing a CD-ROM of examples from O’Reilly books
does require permission. Answering a question by citing this book and quoting example
code does not require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the
title, author, publisher, and ISBN. For example: “Client-Server Web Apps with JavaScript
and Java” by Casimir Saternos (O’Reilly). Copyright 2014 EzGraphs, LLC.,
978-1-449-36933-0.”
If you feel your use of code examples falls outside fair use or the permission given here,
feel free to contact us at [email protected].
Preface | xv
Long Command Formats
Code displayed inline will be adjusted to be readable in this context. One convention
used is that of backslashes to allow newlines in operating system commands. So for
instance, the following commands are equivalent and would execute the same way in a
bash session. (Bash is a standard operating system shell that you see when accessing a
Linux server or Mac OS X at the command line.)
ls -l *someVeryLongName*
...
ls -l \
*someVeryLongName*
The same convention also appears in other settings where OS commands are used, such
as Dockerfiles.
Similarly, JSON strings, being valid JavaScript, can be broken up to fit on multiple lines:
o={"name": "really long string here and includes many words"}
xvi | Preface
How to Contact Us
Every example in this book has been tested, but occasionally you may encounter prob‐
lems. Mistakes and oversights can occur and we will gratefully receive details of any that
you find, as well as any suggestions you would like to make for future editions. Please
address comments and questions concerning this book to the publisher:
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at http://oreil.ly/client-server-web-apps-js.
To comment or ask technical questions about this book, send email to bookques
[email protected].
For more information about our books, courses, conferences, and news, see our website
at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
Thank you to the following people:
• Meg, Ally, Simon, and the gang at O’Reilly for the opportunity to write this book.
• My brother Neal Saternos and Dr. James Femister for the early suggestions from
days gone by that I might be able to do the “programming thing.”
• Michael Bellomo, Don Deasey, and Scott Miller for their time and expertise as
technical reviewers.
• Charles Leo Saternos for taking a break from Lua game development to do some
fine image and design work.
• Caleb Lewis Saternos for inspiration in perserverence (early morning run anyone?)
and editorial work.
• David Amidon for the first opportunity to work as a software developer and Doug
Pelletier for first the opportunity to develop Java web apps.
Preface | xvii
• All the folks that headed up the projects that inspired this book, including managers
Wayne Hefner, Tony Powell, Dave Berry, Jay Colson, and Pat Doran, and chief
software architects Eric Fedok and Michael Bellomo.
• Geoffrey Grosenbach from PluralSight, Nat Dunn from Webucator, Caroline Kvit‐
ka (and others from Oracle and Java Magazine) for technical writing opportunities
over the past several years that led to the current one.
• My parents Leo and Clara Saternos for bringing me up in a loving household that
included a Radio Shack Color Computer when having a PC at home was still a
novelty and my sister Lori for reminders of important things that have nothing to
do with programming.
My love and thanks to my wonderful wife Christina and children Clara Jean, Charles
Leo, Caleb Lewis, and Charlotte Olivia for the consistent love, support, patience, and
inspiration while this project was underway.
Finally, J.S. Bach serves as a creative inspiration on many levels. Not the least of which
is the dedication that would appear at the beginning of his works—and so I say with
him, Soli Deo Gloria.
xviii | Preface
Visit https://ebooknice.com to
discover a wide range of
eBooks across various genres.
Enjoy exclusive deals and
discounts to enhance your
reading experience. Start your
digital reading journey today!
CHAPTER 1
Change Begets Change
1
Software development has changed. The now popular “Agile Manifesto” values:
It is now possible to quickly spin up web applications that prove—at least on a small
scale—the viability of a given technology. There is tremendous value to prototyping. As
Fred Brooks, author of The Mythical Man Month (Addison-Wesley Professional), fa‐
mously stated: “Plan to throw one away; you will, anyhow.” A prototype can allow for
early customer or end user interaction that helps solidify requirements early in the
process. It is no longer an insurmountable task to write a functional web application in
a matter of minutes.
Web Users
Modern web application users have well-defined expectations about how they will be
able to interact with a web application:
The Gartner group claims that in 2014, the personal cloud will replace the PC at the
center of users’ digital lives. There are many implications for web app development.
Users are more technologically savvy and have high expectations for site responsiveness.
They are less passive than in previous years and instead are interactive and engaged.
Websites need to be designed in a way that suggests no limitations in the ability of a
browser to mimic native application experience.
Users expect an application to be exposed in various ways and available in different
situations. Responsive design and support for multiple browsers, platforms, and devices
are the new norm. The use of JavaScript libraries and frameworks is essential to support
the wide variety of target clients.
The New York Times recently reported on the impatience of web users. Among its
findings: a company’s website will be visited less often than that of a close competitor if
it is slower by more than 250 milliseconds. Performance needs to be a key consideration
in web application development.
• Java Server Faces (JSF), an XML-based templating system and component frame‐
work with centralized configurable navigation.
• The Google Web Toolkit is another component framework that leverages the abil‐
ities of Java programmers by letting them focus on Java coding with little need to
directly modify HTML, CSS, or JavaScript.
Each of these frameworks has its place and has been used successfully in production
systems. But like many solutions that try to hide underlying complexities, their usage
is problematic in situations where you need greater control (such as the ability to inte‐
grate large amounts of JavaScript) or you do not conform to the framework assumptions
(for instance, availability of server sessions). This is because these solutions attempt to
hide the fundamental architecture of the Web, which uses an HTTP request-response
protocol following the client-server computing model.
Technology | 3
Browser innovations also led to a shift of responsibility from the server to the client. In
the late 1990s, Microsoft developed the underlying technologies that led to Ajax (a term
coined on February 18, 2005 by Jesse James Garrett). Ajax is an acronym for “asyn‐
chronous JavaScript and XML,” but is more generally applied to various technologies
used to communicate with the server within the context of a given web page. This
allowed small messages to be sent, which made better use of bandwidth when designing
JavaScript-based web applications. Browser performance has increased significantly due
to processor improvements and optimizations to JavaScript engines, so it has made
sense to offload more work from the server to the browser. User interface responsiveness
has evolved to a new level of sophistication.
Mobile device browsers have also provided an additional incentive to further isolate
client-side code from the server. In some cases, a well-designed application leveraging
responsive design principles can be created. If this is not an option, a single consistent
API available for all device clients is very appealing.
Roy Fielding’s doctoral dissertation in 2000 led Java EE 6 to new APIs that deviated from
the previous component-based trajectory. JAX-RS (Java API for RESTful Web Services)
and Jersey (a “production quality reference implementation”) are designed to create
applications reflecting a client-server architecture with RESTful communications.
Software Development
In the past, setting up a new Java project was a rather monumental task. A vast array of
configuration options made it tedious and error-prone. Very little was automated, as
the assumption was that each project would have unique characteristics that developers
would want to account for to meet their specific requirements.
Later influences led to innovations that made setting up a project much simpler. “Con‐
vention over configuration” was an influential mantra of the Ruby on Rails community.
Maven and other Java projects also chose sensible defaults and target easy setup for a
subset of popular use cases.
The availability of scripting languages on the JVM makes it possible to speed develop‐
ment by bypassing the somewhat rigorous type checking of Java. Languages like Groovy,
Python (Jython), and Ruby are loosely typed and constructed in a manner that requires
less code to accomplish equivalent functionality. So-called microframeworks like Sina‐
tra or Play provide minimal Domain Specific Languages (DSLs) to quickly write web
applications and services. And so today, it is a trivial task to set up a minimal set of web
services in a development environment.
The failure of enough large-scale waterfall-style software projects has also made it clear
that there are many advantages to producing a small-scale version of the final product.
A prototype (or prototypes) of the final product can serve many purposes:
• They are a specific, tangible asset representing the final system to be designed. As
such, they incorporate information that is otherwise stored in design documents,
diagrams, and other artifacts (and frequently in more informal locations like email
and people’s memories of water-cooler conversations).
• Prototypes are concrete implementations. As such, they present the requirements
in a much more tangible form. This can lead to a better understanding of the extent
and quality of the requirements gathered, and can suggest areas where there is need
of clarification.
• Prototypes can immediately expose potential points of failure that are not apparent
before attempting a specific implementation.
• The preceding benefits can lead to better estimates and scheduling due to a more
comprehensive understanding of what is intended.
In fact, to more accurately emphasize the challenges in the modern web environment,
we need to consider multiple devices and cloud deployments. See Figure 1-3.
The specific area of “medium specificity” that has been ignored in web development in
general (and in component frameworks in particular) is the stateless, client-server na‐
ture of the Web itself.
Prototyping
Prototyping works well with client-server web applications due to the clear separation
between tiers. As previously mentioned, prototypes can test and verify initial ideals.
They help clarify vague notions and facilitate clear communication regarding require‐
ments. They can inspire and generate new ideas as people interact with something more
concrete than a long text description or a series of pictures. Bad ideas and inconsistencies
can be quickly recognized and eliminated. Used correctly, prototypes can save time,
money, and resources and result in a better final product.
Developer Productivity
Besides the ability to prototype either the client portion or the server component (or
both), work can be split clearly, and development can progress in parallel. The separation
allows sections of code to be built in isolation. This prevents the problem in component
approaches where a server build is required every time a page is changed during de‐
velopment. Development tasks require less time and effort, changes are less complex,
and troubleshooting is simplified.
This is especially evident when a need arises to replace, upgrade, or relocate server-side
code. Such changes can be done independently, without affecting the client. The only
limitation is that the original interface, specifically the URL and message data structure,
must remain available.
Application Performance
User experience is greatly impacted by the perceived performance of a page in the
browser. Faster JavaScript engines allow the client to perform computationally intensive
operations so server workload can be effectively offloaded to the client. Ajax requires
relatively small amounts of data to be retrieved when needed so full page reloads can
occur infrequently and less data is sent in the intervening requests. Users perceive a
snappier, more immediate response as they interact with an application.
There are many benefits to stateless design that ease the lives of developers and support
staff. Resources dedicated to session management can be freed up. This simplifies load-
balancing and configuration that would otherwise be required. Servers can be easily
added to accommodate increased load allowing for horizontal scalability. This replaces
the unwieldy process of hardware upgrades traditionally used to increase throughput
and performance.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com