• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ajax technique for a list of entries

 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I was just wondering, because i might be a little (more) outdated about this subject. What are the options for performing an ajax call and retrieveing back a list of entries.
Now i'm writing the html (<table><tr>, etc...) on a helper class in java and return it to the client as a String, then just use innerHTML to insert it in the div. I'm sure there must be another way, because this one is very tedious and error prone. Can somebidy shed some light on this, preferably with an example?
Thanks.
 
Mark Williams
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally what you do is have your servlet contain the Java code needed to generate your list of entries and then forward to a JSP to do the presentation part. So let the JSP handle putting your generated entries into the HTML to be passed back to the caller.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put it in another way.... When not using Ajax, you create the list on the servlet/helper class, and forward it to the JSP, then just c:forEach to present it. When using ajax, I create the list on the servlet/helper, use a helper class to construct the html used to display the list and then return it to the JSP as text/plain and use innerHTML to insert the HTML created in the helper class where I need it. The ajax technique I'm using is error prone (you can easily mess up the html code when you put it in a StringBuilder) adn hard to maintain. I'm sure the are other techniques for achieving this, and I'd like someone with more insight to give me some examples. Thanks.
 
Mark Williams
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not had trouble having the XMLHttpRequest talk directly to JSPs in the past. I just use the XMLHttpRequest.responseText attribute to set the innerHTML attribute of my div on the client.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand, I don't have a problem either with that. I'm interesting in something easier, less error prone, and more easily maintainable.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Potter wrote:I understand, I don't have a problem either with that. I'm interesting in something easier, less error prone, and more easily maintainable.

Are you allowed to use a JavaScript library like jQuery. The less code you have to write, the easier, less error prone, etc it is.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me put it in another way.... When not using Ajax, you create the list on the servlet/helper class, and forward it to the JSP, then just c:forEach to present it. When using ajax, I create the list on the servlet/helper

Why the difference? There's no need for any difference.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, no problem in using jQuery. I'm interested more about having to write the html part in java. What other solution is there for it? Except returning an xml and manipulate it on the client (I find returning a String and just insert it in the DOM with innerHTML much more convenient than XML). I've heard something about using JSON and c:set with c:forEach, but i'm unable to find a good example of this. Meaning that I don't know how to contruct a good JSON object. For example the equivalent of this xml:



And also I don't know how to use this object in the JSP (using jstl) to contruct a table.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Let me put it in another way.... When not using Ajax, you create the list on the servlet/helper class, and forward it to the JSP, then just c:forEach to present it. When using ajax, I create the list on the servlet/helper

Why the difference? There's no need for any difference.


Because in the servlet i'm doing something like this when using ajax:



While when not using Ajax:




AS far as I know, RequestDispatcher and Ajax don't mix. Am I wrong?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are very wrong. Whatever gave you that idea?

You can do it the exact same way regardless of whether the request was Ajax-initiated or not.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, cann you give me an example (code) how to use this


List l = createList(request.getParameter("param"));
request.setAttribute("list", l);
request.getRequestDispatcher("page.jsp").forward(request, response);


in the jsp? Let's say I'm in the callback function and I want to insert in a <div> with the id content, a table with the List i created in the servlet.
It seems that it will make my job a lot easier.
Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not understanding what you're not understanding.

Whatever gets returned as the response, no matter how it is generated, is what will be available as the responseText of the Ajax instance. Whether you use servlets, JSP, ASP, PHP, ColdFusion, CGI or Fred's Fabulous Framework is completely immaterial. Simply make sure that whatever's returned as the response is the HTML fragment that you wish to inject.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I still need to return an html fragment as response. I was hoping to return a list, or some kind of serialized form of it, and de-serialize it in JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that you don't know how to use JSP to do that, and that this actually has nothing at all to do with it being an Ajax request?

You'd do this the same way as in any other JSP: iterate over the list (deserializing? what's up with that? are you serializing something needlessly?) using <c:forEach> and create the options or list elements (or whatever HTML you like)

You're not clearly identifying what exactly it is that you are having a hard time with.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Is it that you don't know how to use JSP to do that, and that this actually has nothing at all to do with it being an Ajax request?

You'd do this the same way as in any other JSP: iterate over the list (deserializing? what's up with that? are you serializing something needlessly?) using <c:forEach> and create the options or list elements (or whatever HTML you like)

You're not clearly identifying what exactly it is that you are having a hard time with.


I have this code on the servlet:




Now, on page.jsp:


Ignoring the jQuery and taglib imports, what must I write in order to have the list shown after the button? (as a table, drop-down, whatever)?
Hope it's clearer now what I'm having trouble with.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are missing a JSP -- the one that creates whatever it is that you want to inject.

Your servlet should forward to a JSP that creates the HTML fragment that you want to inject into the main page.

It would also be easier to use .load() over $.ajax(), and to create an empty <div> element as a placeholder to contain the injected fragment.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me a link with some code example of this? Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makeAList.jsp:



The rendered list comes back as the response, and get's plunked in the DOM.

The sample code for chapter 8 at http://manning.com/bibeault has plenty of examples.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
The rendered list comes back as the response, and get's plunked in the DOM.


I was missing that part. Everything makes sense now (and working). Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic