This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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).
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.
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
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.
Joined: Sep 07, 2008
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.
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 ?