File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How can I use ajax in my JSP to load page fragment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How can I use ajax in my JSP to load page fragment" Watch "How can I use ajax in my JSP to load page fragment" New topic
Author

How can I use ajax in my JSP to load page fragment

Adam J Smith
Greenhorn

Joined: Apr 23, 2008
Posts: 20
I am trying to use ajax to refresh a page fragment located in the same JSP. I have been told I can do this by placing my fragment within div tags. Here is what I have now, am I on the right track? Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

What happened when you ran it?

The issues I can see on a very quick inspection are that your ready state function is not correctly testing for successful completion (doesn't check status code), and while you say that you want to load an HTML fragment with the response, you are instead loading it as the value of a form element.

Is this just code you found somewhere, or did you write it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Adam J Smith
Greenhorn

Joined: Apr 23, 2008
Posts: 20
I borrowed the javascript from w3schools. As for the rest I tried to guess what it might look like as I can not find an example online that performs this type of action. I re-read the w3Schools tutorial again and found something closer to what I am trying to do. I made a few changes.

The table I want to display I extracted into its own jsp. I extracted the javascript functions into their own js and added the function to test the ready state. Now when I run it I get the ajax functionality I expect when I change the text but my jsp with the table in it is throwing a NullPointerException. I think the attributes I am setting in my servlet are not making it to my table jsp. Do I possibly need to pass my request to the jsp called by my ajax? If I do how would I do that?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

Originally posted by Adam J Smith:
my jsp with the table in it is throwing a NullPointerException. I think the attributes I am setting in my servlet are not making it to my table jsp. Do I possibly need to pass my request to the jsp called by my ajax? If I do how would I do that?


Once once the server, an Ajax request is like any other. Your servlets and JSP don't know and don't care whether it's an Ajax request or not. So you'd go about writing your code just like any "normal" request, and debug it in the same way.

If you are forwarding from the servlet to the JSP just as you would in any other request, any scoped variables you set on the request will propagate to the JSP (just like normal).

Now as far as Ajax...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61662
    
  67

I recommend reading this article in order to get your mind wrapped around Ajax and how it works.

Once you understand the fundamentals, most senior web developers will highly encourage you to use a Javascript library to do your Ajax for you. Ajax is rather tricky in that there are many nuances and browser pitfalls. I recommend one of Prototype or jQuery, with the latter being my own library of choice.

Using jQuery, all that Ajax code needed to grab some content from the server and stuff it into a target element on the page condenses down into:



A bit more concise, no?
siddharth paul
Greenhorn

Joined: Apr 30, 2008
Posts: 20
Thanks
let me study the examples.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I use ajax in my JSP to load page fragment