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 Ajax Hello World example using Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Ajax Hello World example using Servlet" Watch "Ajax Hello World example using Servlet" New topic
Author

Ajax Hello World example using Servlet

Ravi Sree
Ranch Hand

Joined: Jan 24, 2010
Posts: 62

Hi All,

I tried a simple AJAX example I found on net.

Here are the files:

ajax.js


com.ajax.HelloWorld.java


index.jsp


web.xml


directory structure is:-
Ravi Sree wrote:
index.jsp
ajax.js
/src
|
com/ajax/HelloWorld.java
/WEB-INF
|
web.xml
/lib
/classes
|
com/ajax/HelloWorld.class


It is not working as expected, rather i unable to see any change.
On the click of the button in the bottom-left side of IE, an error message is displayed:-


Line : 10
Char : 1
Error: Object expected
Code : 0
URL: http://localhost:8080/AjaxHW/


I am using WinXP SP2, Jdk 1.6, Tomcat 5 and IE 7 for now.
I unable to figure out if I am missing something.
I will appreciate your opinions on this.

Regards,
Sree
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Why did you put your directory structure in a quote instead of code tags that would preserve its formatting?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

I'd suggest trying it with IE8 or Firefox with Firebug, which will give you better script debugging.
Ravi Sree
Ranch Hand

Joined: Jan 24, 2010
Posts: 62

Hi,

Thanks Bear,
I did as you suggested and found that the error was mine.
Following where the changes that I did :-

1. Remove the <script> tags in ajax.js
2. Replace with xmlHttpRequest.setRequestHeader for xmlHttpRequest.setRequestHandler in ajax.js.
One of the major typo mistakes i did
3. I wrote to call doPost inside the same resulting a unwanted recurring loop, I meant to call the doPost from doGet.
4. Change the script type from text/javascript to javascript/text in index.jsp (I dont think it is much of a change though).

Thanks again, JavaRanch rocks

Regards,
Sree
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ajax Hello World example using Servlet