DWR Java AJAX Applications - Sami Salkosuo - E-Book

DWR Java AJAX Applications E-Book

Sami Salkosuo

0,0
31,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

In Detail



AJAX enables rich desktop-like user interfaces in the browser and enables interactive interfaces that can even replace traditional user interfaces. DWR (Direct Web Remoting) is an Open Source Java framework for building AJAX applications. DWR's main idea is to hide AJAX implementation details, like XMLHttpRequest, from developers. By using DWR, you can concentrate on developing the application and business objects and leave AJAX details behind the scenes where they belong. DWR allows server-side Java classes to be used in the browser and JavaScript functions to be used on the server (Reverse AJAX).



This book will show competent Java programmers how to work with DWR to create powerful AJAX applications. It starts with a tutorial on DWR's main features and functions. Then it covers setting up the development environment. Finally, it concludes with some sample applications.



The later chapters are full of example code for sample applications, to aid comprehension.



This book covers all the theoretical and practical knowledge required for working with DWR. The book is packed with code and graphics with step-by-step explanations.

Approach



The initial part guides the reader through the features of DWR and how it works to make the developer ready for practical implementation. The later part contains plenty of code (with explanations) and graphical interface-driven step-by-step examples, which the reader can try out while surfing through the book to learn by doing.

Who this book is for



This book is written for competent Java developers and assumes that you are a professional rather than hobbyist. You should be familiar with the concepts of programming, Web 2.0, and AJAX.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 158

Veröffentlichungsjahr: 2008

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

DWR Java AJAX Applications
Credits
About the Author
About the Reviewers
Preface
What This Book Covers
What You Need for This Book
Who is This Book For
Conventions
Reader Feedback
Customer Support
Downloading the Example Code for the Book
Errata
Piracy
Questions
1. Introduction
What is AJAX?
DWR: AJAX for Java Developers
The DWR Community
Summary
2. DWR Features
Ease of Use
Reverse AJAX
Piggyback
Polling
Comet
DWR JavaScript Libraries
engine.js
util.js
gi.js
Converters
Creators, Filters, and Signatures
Integration with Other Projects
Security
Minimize Risks
Summary
3. Getting Started
Supported Browsers and Environments
Configuration
web.xml
dwr.xml
Using Creator and Its Attributes
Using the Converter Element
Working with Annotations
Using Fluent Configuration with DWR
Setting up a Development Environment
Development Environment with DWR and Firefox
Development Environment with Eclipse
Apache Geronimo
Testing and Debugging
Error Handling
Packaging and Deployment
Deployment Using the Administration Console
Deployment Using the Deployer Tool
Deployment Using Hot Deployment
Summary
4. User Interface: Basic Elements
Creating a Dynamic User Interface
Creating a New Web Project
Configuring the Web Application
Developing the Web Application
Testing the Web Application
Developing Web Pages
Callback Functions
Afterword
Implementing Tables and Lists
Server Code for Tables and Lists
Client Code for Tables and Lists
Testing Tables and Lists
Afterword
Implementing Field Completion
Server Code for Field Completion
Client Code for Field Completion
Afterword
Summary
5. User Interface: Advanced Elements
Creating Forms
Developing the User Interface
Creating the FormHandler Class
Testing the Form
Afterword
Building a Navigation Tree
Developing the User Interface
Creating the NavigationTree Class
Developing the User Interface, Part 2
Testing the Navigation Tree
Afterword
Map Scrolling Using DWR
Developing the User Interface
Creating the MapScroller Java Class
Testing the Map Scroller
Afterword
Summary
6. Backend Integration
Integrating a Database with DWR
Configuring the Database in Geronimo
Creating a CountryDerbyDB Java Class
Testing the Database Integration
Afterword
Integrating with Web Services
Developing the Web Service Client
Implementing the Web Service Call
Testing Web Services Integration
Afterword
Integrating with a Messaging System
Setting up Queues in Geronimo
Developing the OrderSystem Java Class
Setting up the Application for Messaging
Testing with the Backend OrderSystem
Afterword
Summary
7. Sample Applications
Collaborative Book Authoring
Starting the Project
Developing the User Interface
index.jsp
loginFailed.html
mainpage.jsp
Configuring the Web Application
Developing the Java Classes
Login.java
UserDatabase.java
Util.java
Book.java
BookDatabase.java
Testing Collaborative Book Authoring
Afterword
Chatroom
Starting the Project and Configuration
Developing the User Interface
Developing the Java Code
Testing the Chat
Afterword
Summary
Index

DWR Java AJAX Applications

Sami Salkosuo

DWR Java AJAX Applications

Copyright © 2008 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: October 2008

Production Reference: 1151008

Published by Packt Publishing Ltd.

32 Lincoln Road

Olton

Birmingham, B27 6PA, UK.

ISBN 978-1-847192-93-6

www.packtpub.com

Cover Image by Parag Kadam (<[email protected]>)

Credits

Author

Sami Salkosuo

Reviewers

Jason Crow

Matthew Henry

Sujit Pal

Acquisition Editor

Adil Ahmed

Development Editor

Usha Iyer

Technical Editor

Darshana D. Shinde

Copy Editor

Sumathi Sridhar

Editorial Team Leader

Mithil Kulkarni

Project Manager

Abhijeet Deobhakta

Project Coordinator

Rajashree Hamine

Indexer

Rekha Nair

Proofreader

Chris Smith

Production Coordinator

Rajni R. Thorat

Cover Work

Rajni R. Thorat

About the Author

Sami Salkosuo is a Software IT Architect at IBM Software Group, Finland. He has over ten years of experience in Java, Web, and integration technologies.

Sami has written several articles for IBM developerWorks, and is also the co-author of an IBM Redbook: Portalizing Domino Applications.

I am thankful to the reviewers Sujit Pal, Matthew Henry, and Jason Crow for their comments that helped me improve my writing. My thanks to Rajashree Hamine, Usha Iyer, Darshana Shinde, and others at Packt Publishing for making this book come true.

My sincere thanks also for my family for their patience and support.

About the Reviewers

Jason Crow is the lead Java Developer for Office Depot, Inc. in Delray Beach, Florida. He specializes in blending his expertise in Java, DWR, HTML, CSS, and jQuery to enhance usability and to bring dynamic features to officedepot.com. He actively contributes back to the community through his blog http://greatwebguy.com.

Matthew Henry is the Programming Services Manager at LeTourneau University. Matthew has worked in IT and computer related fields as a programmer for 30 years. Matthew co-authored Upgrading to Lotus Notes 7 and has written various articles for specific computer industry magazines.

Sujit Pal started programming some 20 years ago, and has never looked back since. He currently works at Healthline Networks, Inc., a search vertical focused on health, as part of its Research and Development team. Apart from his work, his favorite pastime is to explore new software technologies, techniques, and languages, and he writes about his experiments at sujitpal.blogspot.com.

Preface

AJAX enables a rich desktop-like user interface in the browser and enables interactive interfaces that can even replace traditional user interfaces. Communication between browser and server is done in the background and because only the data is transferred between the browser and the server, AJAX applications seem to be, and are actually, fast and responsive to the users.

DWR, Direct Web Remoting, is an Open Source Java framework, licensed under the commercial-friendly Apache Software License v2 for building AJAX applications. DWR's main idea is to hide AJAX implementation details, like XMLHttpRequest and such, from developers. Developers can concentrate on developing the application and business objects and leave the AJAX details behind the scenes where they belong.

DWR allows server-side Java classes to be used in the browser (it's like an RPC between JavaScript functions and the server-side Java) and also allows JavaScript functions to be used in the server (Reverse AJAX). DWR dynamically generates JavaScript functions from Java classes via XML-based configuration, which can be called from browser via the DWR JavaScript library. A DWR servlet on the server side receives requests and calls the actual Java implementation. DWR includes a couple of JavaScript libraries that are required for DWR to work, and are also helpful for developers.

The term Reverse AJAX is used when a server is used to query and/or control the client browser behavior. DWR supports three different methods to do reverse AJAX in applications: Piggyback, Polling (by the client), and Comet (server push).

You may have an on-going project where you may want to use a framework such as JSF or Spring for building the whole solution. In these cases, AJAX and DWR are just a part of the overall picture, and so DWR needs to integrate with other frameworks nicely, and does that successfully!

The DWR project has thought about security very thoroughly. The DWR framework has taken into account many security issues and there is a lot of discussion about security at the DWR website.

This book is written for professional Java developers who are interested in learning DWR and AJAX framework. It starts with a tutorial on DWR's main features and functions. Then it covers setting up the development environment. It concludes with some sample applications.

The later chapters are full of example code for sample applications, to aid comprehension.

What This Book Covers

Chapter 1 is a brief introduction to AJAX technology and DWR. It also discusses the DWR community and describes briefly what information can be found about DWR on the Internet.

Chapter 2 describes DWR features that we use in the samples of this book—a high-level view of how DWR makes a developer's life easier. It discusses reverse AJAX, DWR JavaScript libraries, converters, creators, filters, and signatures. It also contains a section on integrating DWR with other projects and another on security.

Chapter 3 sets the stage for development by describing how to set up the development environment and how to test and debug our sample applications. It covers DWR-supported browsers and environments, configuration, error handling, packaging, and deployment.

Chapter 4 is the first chapter dedicated to sample code. The examples in this chapter include typical user interface elements such as tables and lists, and how they can be implemented using DWR. It also has an example for field completion.

Chapter 5 discusses how to use DWR in more advanced user interface elements such as forms, navigation tree, and scrolling a map.

Chapter 6 shows how DWR applications are integrated to a database, a web service, or a messaging system.

Chapter 7 includes two sample applications: Collaborative Book Authoring, which shows how DWR is used to create a web based multi-user authoring environment, and Chatroom—a typical multi-user chat room application using DWR.

What You Need for This Book

This book is for professional Java developers and architects who want to learn about DWR by examples. Several skills are needed or are beneficial to get the most out of this book.

First of all, Java development skills are needed. Especially web development using Java technologies like Java Enterprise Edition (JEE), Servlets, and JSPs. Experience about other web technologies like JavaScript, HTML, and CSS is also useful.

Eclipse tooling should be familiar and other useful skills are knowledge about JEE application servers and experience about common technologies like XML.

Knowledge about the basics of AJAX technology is helpful. However, the basics of AJAX are introduced in this book.

Who is This Book For

This book is written for competent Java developers and assumes that you are a professional rather than a hobbyist. You should be familiar with the concepts of programming, Web 2.0, and AJAX.

Reader Feedback

Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply drop an email to <[email protected]>, making sure to mention the book title in the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer Support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the Example Code for the Book

Visit http://www.packtpub.com/files/code/2936_Code.zip to directly download the example code.

Note

The downloadable files contain instructions on how to use them.

Errata

Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide the location address or website name immediately so we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with some aspect of the book, and we will do our best to address it.

Chapter 1. Introduction

Learning by doing is a key if you want to benefit from this book. Since the target audience is the developer community, much of this book consists of examples using DWR in action. Chapters 1 to 3 introduce the main features of DWR, and discuss how to get the development work started. Chapters 4 to 7 are full of sample code, and focus on the source code samples and applications.

This chapter introduces AJAX technology and a widely used Java framework for building AJAX applications: Direct Web Remoting, commonly known as DWR. The introductory sections on both AJAX and DWR are brief since AJAX is already a well-known technology and most of us have at least heard about it and know what it stands for. The introduction to DWR is presented in a short "executive summary" before we dive into more details and examples on DWR in the later chapters.

The following sections are discussed in this chapter:

What is AJAX?DWR: AJAX for Java DevelopersThe DWR Community

What is AJAX?

AJAX is the abbreviation for Asynchronous JavaScript and XML. This gives an almost comprehensive explanation of the technology, except that XML is not required. The term AJAX surfaced around February 2005 and was first used by Jesse James Garrett (http://www.adaptivepath.com/ideas/essays/archives/000385.php), long after the building blocks of AJAX, JavaScript and XML, were available and in wide use.

The principle idea of AJAX is in the word "Asynchronous". This feature enables rich a desktop-like user interface in the browser and enables interactive interfaces that can even replace traditional user interfaces. Communication between browser and server is done in the background. Moreover, as only the data is transferred between the browser and the server, AJAX applications are actually fast and responsive to users. The following figures display how a typical request-response application works (upper diagram), and how AJAX applications work compared to the request-response application (lower diagram).

The main component of AJAX technology is XmlHttpRequest, which is a JavaScript object, first supported by Mozilla in 2002. The concept was originally developed by Microsoft in 1999 for Internet Explorer 5.0 and it was then called XMLHTTP. The following link provides the details and history of XmlHttpRequest:http://en.wikipedia.org/wiki/XMLHttpRequest.

XmlHttpRequest is used to transfer data between client and server asynchronously. The following figure shows the AJAX sequence diagram and how XmlHttpRequest is used:

AJAX is dealt with in detail in many books such as Head Rush Ajax, a "brain friendly guide" to AJAX and also on Internet sites. Further, the assumption is that most of you already know AJAX and you are reading this book for reasons other than just learning AJAX basics.

DWR: AJAX for Java Developers

Direct Web Remoting (http://www.directwebremoting.org), is an Open Source Java framework, licensed under commercial-friendly Apache Software License v2 (http://www.apache.org/licenses/LICENSE-2.0.html) for building AJAX applications. DWR's main idea is to hide AJAX implementation details such as XMLHttpRequest from developers. Developers can concentrate on developing the application and business objects and leave AJAX details behind the scenes where they belong.

DWR allows server-side Java classes to be used in a browser (it's like RPC between JavaScript functions and server-side Java) and also allows JavaScript functions to be used in a server (Reverse AJAX). Through an XML-based configuration, DWR dynamically generates JavaScript functions of Java classes, which can be called from the browser via a DWR JavaScript library. A DWR servlet on the server side receives requests and calls the actual Java implementation.

The following figure displays the positioning of DWR in user applications and is taken from the DWR website (http://directwebremoting.org/dwr/overview/dwr).

In the previous figure, the JavaScript function eventHandler() responds to some browser event like clicking a button. Event handlers use the AjaxService object and call the getOptions() method just as if AjaxService were a normal JavaScript object. A developer can implement client-side and server-side code and leave the communication between the client and the server to DWR.

Calling server-side Java from JavaScript causes a lot of things to happen and also requires a lot to happen behind the scenes:

When a DWR-enabled web page is requested, DWR dynamically generates JavaScript functions from Java classes based on configuration.

When the function eventHandler() gets called, say when clicking a button, the developer calls a dynamically generated function (AjaxService.getOptions(populateList) as shown in the previous figure) and the DWR JavaScript library takes the parameters, serializes them, and calls the DWR servlet on the server.

The DWR servlet receives a request and, based on configuration, the servlet instantiates a Java object such as AjaxService, and calls the required method, for example, AjaxService.getOptions(populateList).When the Java method is finished, the return value (a String array) is returned to the DWR servlet, and servlet serializes the return value and sends the response to the DWR JavaScript function on the browser.The DWR JavaScript function receives the response on the browser and based on the parameter, populateList