File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Html in serlets for ajax apps? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Html in serlets for ajax apps?" Watch "Html in serlets for ajax apps?" New topic

Html in serlets for ajax apps?

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

I am in need of some advice, I am working on an ajax app. At the moment when I want to retrieve some html I have it hard coded into the servlet and simply print that out to the xml. See example below:

if(strtype.equals("home")) {
Html = "<div id=\"MainContent\"><div class=\"differentcorners\"><h1>Overview</h1></div><div id=\"TitlePadding2\">"
+ "<div id=\"OverviewCreditFormat\">Credit: £" + credit
+ "<input type=\"button\" value=\"TopUp Now\" class=\"creditBtn\"/></div><div id=\"OverviewTblColor\">"
+ "<div id=\"Alerts\">Alerts</div><form action=\"#\" id=\"OverviewForm\">";

My question, is the good practise ? Is there a better way of handling html in ajax applications ? As you can see from the above example I have a credit variable which has the amount of credit, so I am placing variables into the html before printing. I know presentation should be seperate from logic but the line has been blurred in ajax (well, for me atleast).

Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
You can put your code in a JSP since the JSP will be translated to a Servlet at the end. this is much faster and you will be able to modify it when needed without the need to redeploy the application.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

The best practice is to handle the request from your servlet and then forward to a JSP to build the HTML.

If you look up MVC (Model, View, Controller) you will find several explanations and tutorials.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

And just because it's an Ajax request doesn't make MVC any less useful*. Remember, a JSP does not have to be a complete HTML page*. A JSP can return whatever HTML fragment is appropriate for the response.

* This appears to be a common misconception.

[Asking smart questions] [About Bear] [Books by Bear]
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Regarding using a JSP as i said i think its the best and fastest way to do respond to your AJAX requests if you have little thing calls, however if your application uses AJAX extensively it would be better if you implement a dedicated Servlet to respond to AJAX requests, the servlet will act as a front controller, and you will differentiate between AJAX calls using a parameter for example "operation", and passing additional parameters if needed

so for example if you for example if you want to check if the user already exists you would call

and if you want for example make another AJAX call to get user information you would use the same path but change the operation value

from the server side you will have a servlet that reads the operation parameter from the request and then gets the correct handler for that operation by sending the operation string to a factory object for example, which will give you back the handler, then you can invoke a certain method that object to get the response string to be sent back to the user. to support what i am advicing here is an abstract view

first you need an interface that has a method named geteAjaxResponse(HttpServletRequest request) you can pass other parameters if needed.

then in your AjaxFrontController you can define a servlet with the following logic

and finally for each AJAX operation you will have a class that extends IAjaxProcessor and define the getAjaxResponse() method.

i ommitted the code needed to create the factory since its obvious, in addition using some naming conventions a lot of code could be reduced, and you could use a configuration file to specify the names and classes used for AJAX so that you will not have to recompile your code if you will have to add a new AJAX operation.

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Thanks for th replies guys. I have a similar layout omar, just that I dont have a factory.
I will post my proccess and you can take a look and tell me how I can integrate the JSP files into the ajax.

User makes ajax request -> request recieved by servlet which gets the DB stuff and hardcoded html and prints it out as XML -> javascript updates page.

Now, is there a way of returning chunks of JSP code as xml without hard coding it into the servlet ? I know there are ways of including chunks of JSP in jsps to prevent repetition. Can the same be achieved with servlets ? Or is there a different way of handling such an issue ?
I agree. Here's the link:
subject: Html in serlets for ajax apps?
It's not a secret anymore!