aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Handling an ajax app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Handling an ajax app" Watch "Handling an ajax app" New topic
Author

Handling an ajax app

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

I am working on an AJAX application. I am trying to workout how I should handle/structure the XML/JSP. The basic jist is the user makes a request, the server recieves and proccesses the XML with whatever values and the chunk of JSP code which is then processed by the JS. My questions are:

1) Should I separate the XML and JSP proccesing into different classes ?
2) How do you include JSP chuncks of code in a serlvet/class without hard coding it into the class/servlet ?
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
Posts: 32

Correct me if I am reading wrong here..

So you would actually create an xml on client and send it to server where it gets processed?
Even though there is technically nothing wrong about it, I feel that its too much of work. You should rather do a get / post and read the values in servlet through request.getParameter() / getAttribute()

XML part of AJAX is typcially employed for sending the response back to client .. where javascript has built in mechanism to read the tag contents through doc.getElementByTagName()

You might want to read a simple AJAX tutorial and how servlet + js communicate with each other...


Vijay dharap,
<a href="http://dharapvj.wordpress.com" rel="nofollow">Blog</a>
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
One important detail: JSP runs at server machine only, not at client machine only. All what you have in client side is a HTML DOM tree. So you can in fact only return at highest a HTML string which you can insert in the HTML DOM tree. JSP isn't going to evaluated in the client side at any way.

Though I prefer returning data, in either XML or JSON format, not a complete block of HTML. Let Javascript create/modify the desired HTML elements the usual way.
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
Posts: 32
Bauke Scholtz wrote:Though I prefer returning data, in either XML or JSON format, not a complete block of HTML. Let Javascript create/modify the desired HTML elements the usual way.


Very True..

Only rant about this approach is, when you need a complicated page structure with quite a few divs, tables or controls inside tables, Javascript code the to create that can quickly become huge. That's when I prefer forwarding my final control from java to JSP .. JSP Container will interpret and create and HTML out of JSP.. which would be sent over the wire to client and would be just inserted in the right place in DOM using Javascript.
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Sorry, maybe I should have been more specific. What I mean is the user sends a GET request, the server receieves that request, the servlet gets all the values needed from DB, gets the relevant JSP code and places it into XML so the JS can do what needs to be done.
The problem with allowing JS to handle all the html is its going to make it huge and complicated on maintainance, so I'd prefer the server just place it into an xml tag and the JS just inserts it into the html.
So here are my questions again:

1) Should I separate the XML and JSP proccesing into different classes ?
2) How do you include JSP chuncks of code in a serlvet/class without hard coding it into the class/servlet ?
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
Posts: 32
shaf maff wrote:Sorry, maybe I should have been more specific. What I mean is the user sends a GET request, the server receieves that request, the servlet gets all the values needed from DB, gets the relevant JSP code and places it into XML so the JS can do what needs to be done.


Ha.. Now its little more clear!

shaf maff wrote:The problem with allowing JS to handle all the html is its going to make it huge and complicated on maintainance, so I'd prefer the server just place it into an xml tag and the JS just inserts it into the html.
So here are my questions again:

1) Should I separate the XML and JSP proccesing into different classes ?
2) How do you include JSP chuncks of code in a serlvet/class without hard coding it into the class/servlet ?


Now.. if your processed HTML is indeed complex for JS to handle and most importantly maintain, why not take the approach that I suggested in prior post.
1. User makes req. (GET / POST) [This is actually XMLHttpRequest]
2. Servlet gets all the data from DB.
3. SErvlet sets values up in req by using request.setAttribute() .. Probably you just setup your DTO in attribute.
4. Servlet forwards to JSP
5. JSP useBean would take the DTO and use data from DTO to create complex html out of it.
6. JSP Container will flush html to client. [No additional code @ servlet required for this]
7. In JS, the callback function receives HTML string as responseXML. Note, HTML IS also an XML so we are fine.
8. Use JS logic to just set the html you just received into innerHTML of some div.

And thats it!

Hope that helps..
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Ah, I cant believe I didnt think of that! Thanks vijay! I have another question, I have a servlet called AjaxController which receieves the requests etc - I forward the requests to a class called AjaxProcessor. The AjaxProcessor is going to handle many different things so I will be using alot of IF statements. See the code below followed by a question:


My question is, is this a good way of doing this ? Or is there a better way ? Note that I will be using the daos' to access the content.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61418
    
  67

Bauke Scholtz wrote:One important detail: JSP runs at server machine only, not at client machine only. All what you have in client side is a HTML DOM tree. So you can in fact only Though I prefer returning data, in either XML or JSON format, not a complete block of HTML. Let Javascript create/modify the desired HTML elements the usual way.


Returning either of data or an HTML fragment is a viable use of the tool (Ajax). I'm a big fan of returning JSON data when appropriate, and just as big a fan of returning an HTML fragment (usually generated with JSP) when appropriate. Writing and maintaining complicated script at the client to create new elements when one can leverage the power of JSP seems like a poor use of the tools at hand.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vijay Dharap
Ranch Hand

Joined: Mar 18, 2004
Posts: 32
shaf maff wrote:Ah, I cant believe I didnt think of that! Thanks vijay!

You are welcome!

shaf maff wrote:My question is, is this a good way of doing this ? Or is there a better way ? Note that I will be using the daos' to access the content.

Well... Let me ask you a question... Are you willing to incorporate a standard framework in your application? Because what you have in place is a rudimentary MVC framework. In stead of you coding it in that fashion if you take help of any std fwk like JSF / Struts, the control of the flow can be moved out of the code to xml file .. that way you avoid all the if else logic in the code.

It would be initial pain to move your logic into the framework once.. but the gains are infinite.

e.g. logic like...
if "newuser" then newuseraction
if home then homeaction
goes into navigation rules of jsf files and you just deal with actual implementation of newuseraction / homeaction etc classes.

+ you get other benefits like conversion of your html input into your DTO directly etc..

Hope this helps..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling an ajax app