Many WEB developers often complain that java™ee is too complex to build new WEB components, that customizing existing components is not as simple as expected, and that even small changes require a restart of the application. This series provides solutions to these problems, using code generators, conventions, scripting languages, and Advanced JavaServer pages™ (JSP) features. In this article, you'll learn how to build reusable Ajax and Java components based on JSP tag files, which are easy to develop and deploy. After the changes, the JSP tag files are automatically recompiled by the Java EE Server without restarting the application. In addition, you have full control over the generated code and can easily customize these lightweight components because they are using JSP syntax.
This series contains 4 parts, showing a JSP based technology to generate JavaScript code, significantly reduce the amount of code that needs to be written manually, this is the 1th part. The sample application in this article shows how to generate JavaScript functions to send AJAX requests and handle AJAX responses. If you want to easily change your Ajax code, you can apply the simple techniques discussed here to the actual application. The broader goal of this article is to show how to use JSP tag files to generate JavaScript code for specific requirements, rather than just Ajax routines.
Working with frameworks and code generators
If you're lucky enough to find a component or framework that meets your needs, then use it. It doesn't matter if you don't find it, because you can always develop your own solution, or you can customize an existing piece of code. In either case, a good practice is to "parameterize" the code and put it into a reusable library rather than hard-code the parameters into your code. Sometimes, however, implementing generics is impractical, because it makes development more complex than simplified. When you put generic code into reusable components or frameworks, consider using code generators to more efficiently generate specific code.
Avoid copying & pasting during development
Suppose you need an application that uses Ajax to request some information on the site, the quickest (and certainly not the best) method is to find some free code such as listing 1, change the URL, and paste the code into the Web page. Many developers will do this, but this can lead to huge maintenance problems. If the application has hundreds of pages, the final result will be a large number of functions like the GetInfo () in Listing 1. The downside is that every time you need to add or change, such as error handling for AJAX requests, you have to manually modify all the pages and test them again. The good side is that you can easily avoid this maintenance problem by using libraries, frameworks, and code generators.
Listing 1. Ajax functions
function getInfo(country, city) {
var request = null;
if (window.ActiveXObject)
request = new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
request = new XMLHttpRequest();
else
return;
var url = "CityInfo.jsp?country=" + escape(country)
+ "&city=" + escape(city);
request.open("GET", url, true);
function processResponse() {
if (request.readyState == 4) {
if (request.status == 200) {
// ...
}
}
}
request.onreadystatechange = processResponse;
request.send(null);
}