This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67634
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
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?
 
Adam J Smith
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67634
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

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
Sheriff
Posts: 67634
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
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?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
let me study the examples.
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic