Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:26:04+00:00 2026-06-07T01:26:04+00:00

I am an Ajax fresher Ajax function ajaxFunction() { if(xmlhttp) { var txtname =

  • 0

I am an Ajax fresher

Ajax

function ajaxFunction() {
  if(xmlhttp) { 
   var txtname = document.getElementById("txtname");
    xmlhttp.open("POST","Namelist",true);

    xmlhttp.onreadystatechange  = handleServerResponse;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send("txtname=" + txtname.value); 
  }
}

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
       document.getElementById("message").innerHTML=xmlhttp.responseText;
     }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

jsp

<form name="fname" action="Namellist" method="post">

    Select Category :
    <select name="txtname" id="txtname">
     <option value="Hindu">Hindu</option>
     <option value="Muslim">Muslim</option>
     <option value="Christian">Christian</option>
    </select>
    <input type="button" value="Show" id="sh" onclick="ajaxFunction();">
    <div id="message">here i want to display name</div><div id="message1">here i want to display meaning</div>
    </form>

servlet

String ct=null;  
ct=request.getParameter("txtname");
      Connection con=null;
      ResultSet rs=null;
      Statement st=null;
try{
con=Dbconnection.getConnection();
PreparedStatement ps=con.prepareStatement("select name meaning from (select * from namelist order by dbms_random.value)where rownum<=20 and category='+ct+'" );
rs=ps.executeQuery();

 out.println("name" + rs);
 **Here I have confusion,**

}
catch(Exception e)
{
    System.out.println(e);
}

How can i diaplay servlet value to jsp.
Please help me? or please provide some good tutorial links.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:26:06+00:00Added an answer on June 7, 2026 at 1:26 am

    You have to make below changes :-
    In Servlet :-
    Set the response content type as:- response.setContentType("text/xml"); in top section of the servlet. By setting this we can send the response in XML format and while retrieving it on JSP we will get it based on tag name of the XML.

    Do whatever operation you want in servlet…
    Save the value for ex-

    String uname=";
         uname="hello"; //some operation
        //create one XML string
        String sendThis="<?xml version='1.0'?>"
                +"<Maintag>"
                +"<Subtag>"
                +"<unameVal>"+uname+"</unameVal>"     
                +"</Subtag>"
                +"</Maintag>"
      out.print(sendThis);
    

    Now we’ll go to JSP page where we’ve to display data.

        function getXMLObject()  //XML OBJECT
            {
                var xmlHttp = false;
                try {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
                }
                catch (e) {
                    try {
                        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
                    }
                    catch (e2) {
                        xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
                    }
                }
                if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                    xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
                }
                return xmlHttp;  // Mandatory Statement returning the ajax object created
            }
        var xmlhttp = new getXMLObject();   //xmlhttp holds the ajax object
            function ajaxFunction() {
                if(xmlhttp) {
                    xmlhttp.open("GET","NameList",true); //NameList will be the servlet name
                    xmlhttp.onreadystatechange  = handleServerResponse;
    
                    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xmlhttp.send(null);
                }
            }
            function handleServerResponse() {
                if (xmlhttp.readyState == 4) {
                    if(xmlhttp.status == 200) {
                       getVal();
                    }
                    else {
                        alert("Error during AJAX call. Please try again");
                    }
                }
            }
           function getVal()
            {
                 var xmlResp=xmlhttp.responseText;
                 try{
    
                    if(xmlResp.search("Maintag")>0 )
                    {
                   var x=xmlhttp.responseXML.documentElement.getElementsByTagName("Subtag");
                        var xx=x[0].getElementsByTagName("unameVal"); 
                        var recievedUname=xx[0].firstChild.nodeValue;
                       document.getElementById("message").innerText=recievedUname;//here 
                    } 
                    }catch(err2){
                        alert("Error in getting data"+err2);
                    }
            }
    

    And here you are done. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ajax code- ... xmlhttp.open(GET,voting.php?qid=+qid+, uid=+uid+, type=+type,true); ... is this the correct way to sent
My Ajax code $(document).ready(function() { $(#sub).click(function() { $.ajax({ type: POST, url: jqueryphp.php, data: txt1=
var ajax = { xmlHTTP: function() { xml = new XMLHttpRequest(); return xml; },
// // // // // // // // Ajax returns from PHP xmlhttp.onreadystatechange=function() {
function ajax_test(str1){ var url = "None" jq.ajax({ type:'post', cache: false, url: 'http://....' + str1,
AJAX code: <script type=text/javascript> function doCalc(){ var roomX = $('#room_str').val(); var heightX = $('#height_str').val();
$.ajax({ type: POST, url: contacts.php, data: dataString, cache: false, success: function(data, status, settings) {
Newbie ajax question from this example : How is xmlhttp.responseText assigned before xmlhttp.open() and
$.ajax({ async:false, type: 'POST', url: itemURL, success: function(data,status,jqXHR) { responseObj = data; console.log('success function
$.ajax({ url: notifications.php, dataType: json, success: function(responseJSON) { if (responseJSON.length > 0) { document.title

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.