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 7079805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:43:01+00:00 2026-05-28T06:43:01+00:00

I am trying to implement ajax in jsp(avoid form submit) using struts2. I used

  • 0

I am trying to implement ajax in jsp(avoid form submit) using struts2. I used ajax code to pass request via url to struts2 action. But the response from struts2 is not get populated in jap. Its showing “null” value. My code to call a action in jsp using AJAX is as Follows.

    function ajaxEditFunctionCall(){  
 var xmlHttp;
     var url = "ajaxcall.action?stateName="+frm.stateName.value;  
  try{   
    xmlHttp=new XMLHttpRequest();   
  }catch (e){
      try{ 
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
      }catch (e){ 
          try{  
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
            }catch (e){
                alert("Your browser does not support AJAX!");
                return false;
            }
      }
  }
  alert(1);
  xmlHttp.onreadystatechange = showMessage; 
      alert(2);

  xmlHttp.open("GET", URL, true); 

       alert(3);
       xmlHttp.send(null);  
  }

      function showMessage() { 
       alert("Inside Show Message1");
         alert(xmlhttp.readyState);
             if(xmlhttp.readyState==4)  
            { 
             alert("Inside Show Message2&ReadyState4");
                 alert(xmlhttp.responseText);  
            }  
       }  

   Included following code in Action Class:

public String ajaxcall() throws Exception{

     System.out.println("Inside AjaxCall");
     String errorXml = "This is a Sample to Check";  

     response.setContentType("text/html"); 
     response.setHeader("Cache-Control", "no-cache"); 
         response.setContentType("text/html");   
     response.getWriter().write(errorXml); 

     return null;

}

Code included in Struts.xml:

  <action name="ajaxcall" class="com.logic.action.CustomerAction" method="ajaxcall">
       <result name="success" >/pages/customer/addCustomer.jsp</result> 
   </action>

I think the error is in the action class-response statement and in struts.xml. Can any one please help me to fix this issue. Thanks in Advance.

  • 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-05-28T06:43:02+00:00Added an answer on May 28, 2026 at 6:43 am

    Here, the correction needs to be done to the JavaScript function. When you say var xmlHttp, it has a scope inside the function ajaxEditFunctionCall and not for the showMessage. Also, xmlhttp in showMessage() is not same as xmlHttp object in ajaxEditFunctionCall. So, keep the var xmlHttp declaration global and make the corrections. Here is the working code:

    <script type="text/javascript">
                var xmlHttp;
                function ajaxEditFunctionCall(){
    
                    var URL = "ajaxcall.action?stateName=State1";
                    try{
                        xmlHttp=new XMLHttpRequest();
                    }catch (e){
                        try{
                            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
                        }catch (e){
                            try{
                                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                            }catch (e){
                                alert("Your browser does not support AJAX!");
                                return false;
                            }
                        }
                    }
                    //alert(1);
                    xmlHttp.onreadystatechange = showMessage;
                    //alert(2);
    
                    xmlHttp.open("GET", URL, true);
    
                    //alert(3);
                    xmlHttp.send(null);
                }
    
                function showMessage() {
                    //alert("Inside Show Message1");
                    //alert(xmlHttp.readyState);
                    if(xmlHttp.readyState==4)
                    {
                        alert("Inside Show Message2&ReadyState4");
                        alert(xmlHttp.responseText);
                    }
                }  
            </script>
    

    The Java code is:

    public class CustomerAction extends ActionSupport implements ServletResponseAware {
    
        HttpServletResponse response;
    
        public String ajaxcall() {
    
            System.out.println("Inside AjaxCall");
            String errorXml = "This is a Sample to Check";
    
            response.setContentType("text/html");
            response.setHeader("Cache-Control", "no-cache");
            try {
                response.getWriter().write(errorXml);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
    
            return null;
        }
    
        public void setServletResponse(HttpServletResponse response) {
            this.response = response;
        }
    }
    

    The struts.xml is:

    <struts>
        <constant name="struts.devMode" value="true" />
        <package name="default" extends="struts-default">
            <action name="ajaxcall" class="com.logic.action.CustomerAction" method="ajaxcall">
                <result name="success" >/pages/customer/addCustomer.jsp</result>
            </action>
        </package>
    </struts>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a simple request to Wikipedia's API using AJAX (XMLHttpRequest).
i am creating a web project using JSP, and is trying to implement a
I'm trying to implement a workaround to the problem of using AJAX with multi-part
I am using spring2.5. and trying to implement a custom CommonsMultipartResolver for ajax upload.
I'm trying to implement a client-side ajax login on Asp.Net MVC. I used to
While trying to implement some jQuery UI Tabs using AJAX I keep running into
Yesterday I was trying to implement a Listener for a SelectManyListbox using Ajax in
so i'm currently trying to implement a currency conversion script using Jquery, curl, ajax
I am trying to implement AJAX in my Google App Engine application, and so
I am trying to implement ajax back/forward button support and therefore writing variables after

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.