Home United States USA — software A Simple AJAX Website in Java

A Simple AJAX Website in Java

199
0
SHARE

Learn how to make an AJAX website in Java.
Join the DZone community and get the full member experience. During the Covid New Year of 2020, I created a simple prototype website using AJAX, with the goal of moving Java developers away from JavaScript and ideally also text-based HTML templates. The developer will however need to understand the structure of an HTML page as well as CSS selectors. I made my original solution a little more general, moved certain parts into the Ujorm framework, and am presenting the result here for your further inspiration. For the prototype to make any sense, I created a web page for testing regular expressions. Let’s take a look at the code, worked into a regular Java servlet: The doGet() method is responsible for assembling the entire HTML page, containing a simple form with two (text) inputs, a submit button, and a panel to display the results. In my implementation I made use of object model classes of the Element type from the Ujorm library. The advantage of this solution is how sturdy its tolerance is towards typos in HTML code, safe data entry (including special characters) and last but not least, how easy it is to connect other services – with all the convenience and resources of Java programming language. It’s good to be aware that objects of type Element send their data directly to the Writer, and so have minimal memory usage (in the default configuration). But if anybody wants, they can assemble the HTML page using any other approach, and it’s also simple to move this implementation out of the servlet – for example to Spring MVC. Let’s go back to the example of our current source code: one thing worth mentioning is line 8, which inserts a short piece of JavaScript into the header of the output HTML page.

Continue reading...