File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to use AJAX to call a servlet

 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I am toally new to AJAX and somewhat-new to "servlets and jsp."

I am trying to send a request from an html page to a servlet on a button click. The request should update only <div id="dataDiv>, but nothing happens when i click the button. It just sits there like nothing happened.

I inserted some good'ol SOP's in the Servlet to get something on the server but it seems that the servlet isn't getting called. I am tired and lost. Please help.

I am using Mozilla 5.0, apache tomcat and jdk 6.

Code for index.html:





Code for Servlet(class DataServlet):





code for web.xml:

 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to use a submit button, you need to cancel the click action.

Also your Ajax code is not going to work with older versions of IE.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you used Firebug to look at the script errors and request activity?

 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:If you are going to use a submit button, you need to cancel the click action.


So should I be using:

<Button type="button" onClick="UpdateData()">Click me</Button>

instead of:

<input type="submit"....>

Because I tried this and it still isn't working.

Next inserted a bunch of alert() tags in the UpdateData() function to check if its getting called alright but it seems to me that the function isn't getting invoked at all.

new code:


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Have you used Firebug to look at the script errors and request activity?



Your URL looks suspect. You should be using a server-relative URL starting with the context path.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your problem:
<script type="javascript/text">

Eric
 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Have you used Firebug to look at the script errors and request activity?



No I haven't. Just downloaded it. I know I have an html/javascript issue but can't really see why is this function not being called?!
 
Funkaj Kohli
Greenhorn
Posts: 12
Firefox Browser Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:This is your problem:
<script type="javascript/text">

Eric


LOL!

Thanks a lot Eric! That fixed it

ps - i never thought this day would come.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic