A Simple AJAX Website in Java
Join the DZone community and get the full member experience.Join For Free
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.
If the servlet receives a request without this parameter, it delegates the processing to the original doGet() method, otherwise, it calls the doAjax() method to prepare a JSON response. For this purpose, it uses an object of type JsonBuilder from the Ujorm framework. Calling the method JsonBuilder.write(key, value) writes the name of the attribute and its value as JSON. The name of the attribute, in this case, is the CSS selector defining the target HTML element, and the value contains a valid HTML code fragment, replacing the original contents of the selected element.
Published at DZone with permission of Pavel Ponec. See the original article here.
Opinions expressed by DZone contributors are their own.