aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Trying to use AJAX to call a servlet 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 » HTML, CSS and JavaScript
Bookmark "Trying to use AJAX to call a servlet" Watch "Trying to use AJAX to call a servlet" New topic
Author

Trying to use AJAX to call a servlet

Funkaj Kohli
Greenhorn

Joined: Feb 24, 2011
Posts: 12

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:



Pankaj Kohli
OCJP 6
Its the question that drives us.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 61433
    
  67

Have you used Firebug to look at the script errors and request activity?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Funkaj Kohli
Greenhorn

Joined: Feb 24, 2011
Posts: 12

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

Joined: Jan 10, 2002
Posts: 61433
    
  67

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
This is your problem:
<script type="javascript/text">

Eric
Funkaj Kohli
Greenhorn

Joined: Feb 24, 2011
Posts: 12

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

Joined: Feb 24, 2011
Posts: 12

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.
 
wood burning stoves
 
subject: Trying to use AJAX to call a servlet