Closed Thread
Results 1 to 2 of 2

Thread: Java Ajax Hello World needed

  1. #1
    Marcinnnn is offline Newbie
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Java Ajax Hello World needed

    Hi,

    I just need simple example of using AJAX with Java in web developing. Lets say we have just simple page
    Code:
    <html>
    <header>
    \\ stuff like title, meta, etc.
    </header>
    <body>
    <div onclick="doAjax();" > whatever </div>
    </body>
    </html>
    We have of course class in a package:
    Code:
    package app.rpc;
    
    public class myClass extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, 
    	           HttpServletResponse response) 
    	        throws IOException, ServletException { 
    	        
    	       String something = request.getParameter("something");
    	       
    	           response.setContentType("text/xml"); 
    	           response.setHeader("Cache-Control", "no-cache"); 
    	           response.getWriter().write("It works! " + something); 
    	    }		
    }
    I think that this class is just OK. And we of course need to have some javascript:

    Code:
    // yea I know that this will not work with ie6 but leave it
    function initRequest() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            isIE = true;
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
    return false; 
    }
    
    // I think here problem comes
    
    function doAjax() {
               var url = "myTomcatContext/app.rpc.myClass?something=Hello-World"; // I'm not sure if path is correct          
               var req = initRequest();
               req.onreadystatechange = function() {
                   if (req.readyState == 4) {
                       if (req.status == 200) {
                           parseMessages(req.responseXML); // and not sure of this
                       } else if (req.status == 204){
                           clearTable();
                       }
                   }
               }
    
               req.open("GET", url, true); // I would like also POST version - if someone can help I'll be thankful
               req.send(null);
        alert(response+ '!'); \\ I want all data ( "It works! Hello-world!" should be here)
    }
    Thanks in advance,
    Marcin

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Java Ajax Hello World needed

    you can use responseText property instead, i think its working fine all you need to do is to see the results:
    if you are using firefox get firebug plugin it will help you see the requests and responses by your page
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How do you run Hello World in C++? [Expert Needed]
    By jwxie518 in forum The Lounge
    Replies: 8
    Last Post: 07-03-2011, 01:28 PM
  2. AJAX / Java / Flash
    By outsid3r in forum General Programming
    Replies: 2
    Last Post: 07-22-2010, 05:55 PM
  3. Java Bot Dev Needed!
    By Justen in forum Services for Buy/Sell/Trade
    Replies: 0
    Last Post: 05-31-2010, 09:48 PM
  4. Replies: 0
    Last Post: 10-19-2007, 09:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts