Easy integration of Ajax with Java EE

Source: Internet
Author: User

With the advent of Ajax, the need to dispel the mysteries of this hot technology and address the problems that arise in its use has arisen. Patrick Gan, a senior IT expert, took the opportunity to explore the potential impact of introducing Ajax into Java™ee WEB applications throughout the development lifecycle. Waking up to the problem of adopting Ajax patterns based on asynchronous communication helps set the right path for effective integration with Ajax.

Asynchronous JavaScript + XML (Ajax) is a fairly new term (some people say it's a new bottle of old wine) and is a big controversy among different Web development communities, including the Java EE community. Ajax technology improves application availability by eliminating excessive WEB page refreshes. And Ajax takes advantage of both client and server-side code, presenting a virtually seamless user interface to Web users. Ajax is touted as a major catalyst for Web development revival (or Web 2.0).

As an attentive Java EE developer, you may have read a lot of how-to articles about Ajax and are excited about the possible improvements it can bring to your application. But how does Ajax model based on asynchronous communication fit into your Java EE application? This article will help you answer this question by studying the impact of introducing Ajax into the design, development, execution, and testing phases of a Java EE application. My goal is not to discourage the use of Ajax or to imply that the problems you might encounter are inherent in Ajax technology. Instead, I'm here to help you plan and mitigate these problems so that you can use Ajax more effectively and smoothly.

Solving design Flaws

For quite some time, the Java community has struggled to apply good design patterns to WEB-related application development. One of the most widely used patterns is the model-view-controller (MVC). Some open source frameworks, such as Apache Struts, are based on this design pattern/architecture (see Resources). Many of the advantages of MVC include problem isolation and reduction of redundant code.

Problem isolation uses a pre-configured interface throughout the application architecture, allowing each developer in an application development project to focus on its own specific role. For example, the developer of the model layer focuses on technologies such as JDBC, Enterprise JavaBean (EJB) components, or Java classes that interface with the underlying data persistence technology. View layer developers focus on Java Server Page (JSP) technology, tag libraries, and other techniques related to presentation. The controller layer isolates and coordinates the model and view, routing incoming requests to back-end persistence calls, while maintaining clear isolation of the problem. Figure 1 illustrates the MVC architecture:

Figure 1. MVC Architecture

Introducing Ajax into the Java EE WEB application is useful for isolating the problem (and the isolation of developer roles). In some cases, Ajax brings a lot of JavaScript code back to the View layer (JSP) page. Table 1 describes the view layer without Ajax and also points to the required code (assuming the controller layer is implemented by the servlet, and the view layer is implemented by the JSP technology.) (The next section deals with development dilemma I'll explain the difference between synchronous and asynchronous requests.) )

Table 1. MVC without Ajax: The number of code associated with a typical view-layer sequence

Before calling the synchronization request Prepare the script code required for the form submission Whether
Calling an asynchronous request Form submission is caused by a button or link call, and the value of the DOM element is automatically set to HttpRequest (via get or post). No: All you need is a way to invoke the page submission.
Handling Responses to synchronization requests After server-side code execution is complete, an object is typically sent back to the JSP (either through HttpRequest or in httpsession). At this point, you can access this object (through scripts or some tag libraries) through HttpRequest or httpsession in the JSP, and you can display the object's contents by writing very few scripts. Yes: the least script.

Comparing tables 1 and 2, table 2 describes the MVC view layer with Ajax, assuming that the control layer is implemented by the servlet, and the view layer is implemented by the JSP technology.

Table 2. Ajax MVC: The number of code associated with a typical view-layer sequence

Before calling the asynchronous request You need to use JavaScript code to retrieve the value of the DOM element required by the Ajax call. Is
Calling an asynchronous request You need to create XMLHttpRequest with JavaScript code and associate (previously collected) DOM element values with it and send (Xmlhttprequest.send ()). Is
Handling Responses to asynchronous requests After server-side code execution completes, the request is obtained in JavaScript code (from the XML response stream) and the value is populated appropriately with the appropriate DOM element. Is

As you can see, with Ajax, the view layer has increased scripting, resulting in three obvious flaws:

JSP requires a lot of JavaScript code. This design destroys the isolation of the role problem. The design brings back a single JSP (Pattern 1 method: A stack of HTML, CSS code, pictures, and scripting code), which is an inverse pattern that is extremely difficult to read and maintain (see Resources).

There are several options to avoid or at least mitigate these design flaws:

The design-time mind remembers reuse: Unfortunately, writing code that is specific to Ajax support is often difficult to avoid. Plan and design your scripting code so that it can be reused to the maximum extent possible.

Using the client MVC method: You can combine the client MVC method with the Ajax in Action written by Dave Crane (see Resources). This approach can help isolate the problem, but adds complexity, so use it carefully.

Using the AJAX framework: There are multiple open source Ajax frameworks, such as Direct Web Remoting (see Resources), which work well to integrate AJAX schemas into Java EE applications with minimal coding.

Re-evaluate the correctness of the design: In fact, Ajax provides the properties of a desktop application for a WEB application. If most client interactions in a WEB application utilize Ajax, the application might be best designed as a desktop application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.