Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to call Servlet using ajax

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to call Servlet using ajax

    Excuse me,
    My name is Pragathi.
    What am i asking is how to call servlet using ajax sample examples.
    So please give me suggestions to me and mail me some examples on it.

    I have a form that is designed in a JSP. This form has 2 dropdown lists such that first one has fixed options and second one has to be populated dynamically (according to the option selected by user in first dropdown list) from the without database . This is the task that I have to perform.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: How to call Servlet using ajax

    Hi,

    I understand what you want to do in your application. I guess you have not searched in web.

    Please refer to Call servlet from Ajax - Stack Overflow
    Thanks and regards,
    Sambit Swain

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to get output for this ajax using code. Code is shown bellow.

    Hi,
    i'm beginner for this ajax program. So please give me suggestions and help me also.
    Here my doubt is, here is two drop down lists so when 1st one(dropdown) is in static position and 2nd(dropdown) is in dynamical position by usage of 1st one. And also reversal.
    In this following code i use this (findState.php?). But with out that .php file & with out data base(DB) how can i get out put by using ajax.


    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <input type="text" name="othstate" value="" class="textBox">
    <form method="post" name="form1">
    <select style="background-color: #ffffa0" name="country" onchange="getState(this.value)">
    <option>Select Country</option>
    <option value="223">USA</option>
    <option value="224">Canada</option>
    <option value="225">England</option>
    <option value="226">Ireland</option>
    </select>

    <select style="background-color: #ffffa0" name="state">
    <option>Select Country First</option>
    </select>
    <script>
    function getState(countryId)
    {
    var strURL="findState.php?country="+countryId;
    var req = getXMLHTTP();
    if (req)
    {
    req.onreadystatechange = function()
    {
    if (req.readyState == 4)
    {
    // only if "OK"
    if (req.status == 200)
    {
    document.getElementById('statediv').innerHTML=req. responseText;
    } else {
    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
    }
    }
    }
    req.open("GET", strURL, true);
    req.send(null);
    }
    }
    </script>
    </body>
    </html>

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to call Servlet using ajax

    Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.

    Please don't start multiple threads for the same topic. If the question has evolved, just say so, and keep going. You can also ask ask a mod to retitle or move the thread, if appropriate.

  5. #5
    Member Ganeprog's Avatar
    Join Date
    Jan 2014
    Location
    Chennai,India
    Posts
    80
    My Mood
    Love
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: How to call Servlet using ajax

    hi,

    document.getElementById('statediv').innerHTML=req. responseText;

    In this statement, you are not created the div element. So create div element within body tage like below

    <div id="statediv"></div>

    Try this it may helpful

    Thanks,

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: How to call Servlet using ajax

    Create a div id for the 2nd drop down. responsetext should be set in the inner HTML of 2nd drop down element.

    Follow the link ajax - populate dropdown on change of another using servlet - Stack Overflow

    Sorry, currently I dont have the exact workspace and jar files ready to code and test the solution before giving it to you. Please refer to the link above and give a try, hopefully by day after tomorrow I will set up a new project with your content and provide a better solution.
    Thanks and regards,
    Sambit Swain

Similar Threads

  1. send and receive byte array form servlet to another servlet via url
    By diga88 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 11th, 2013, 09:40 PM
  2. Call service() from init() in servlet
    By krupam790 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2013, 09:00 PM
  3. Replies: 0
    Last Post: October 31st, 2012, 06:22 AM
  4. Replies: 0
    Last Post: March 11th, 2012, 04:57 PM
  5. JSP freezes after AJAX call !!
    By java_freek in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 16th, 2010, 10:31 AM