• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Html in serlets for ajax apps?

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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\">";
out.println(Html);
}

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).



.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



(peace)
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
http://yourpath/yourapp/ajaxcontroller?operation=checkUserExists&username=foo

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
http://yourpath/yourapp/ajaxcontroller?operation=loadUserInfo&userId=10


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.


(peace)
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Grow a forest with seedballs and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic