title: Can't set up a proper ajax call using struts2 and ajax
body: <p>I've been trying to get this code to work. THe basic idea is I want to create an ajax request when the textbox whose id is "user" to go back to the server (apache) via an ajax call.</p>
  
  <p>The error I get is a 404 when doing a <code>http://localhost:8080/CheckUserAvailabilityAction.do?username=ds</code> request.</p>
  
  <p>This is my code.
  Struts2 class CheckUserAvailabilityAction.java:</p>
  
  <pre><code>public class CheckUserAvailabilityAction extends ActionSupport implements
      ServletRequestAware {
  
  //struts action
  public String execute() {
  
      String output = "success";
      /*
          SOME CODE
      */  
      return output;
  
  }
  }
  </code></pre>
  
  <p>Here's my jsp:</p>
  
  <pre><code>    &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;
  
  &lt;%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
      prefix="bean"%&gt;
  &lt;%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
      prefix="html"%&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script src="js/scripts.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="js/jquery-2.1.1.min.js"&gt;&lt;/script&gt;
  
  &lt;script&gt;
      var url = "HelloWorld.jsp";
      function makeRequest(url) {
          if (window.XMLHttpRequest) {
              httpRequest = new XMLHttpRequest();
          } else if (window.ActiveXObject) { // IE
              try {
                  httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e) {
                  try {
                      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch (e) {             }
              }
          }
  
          if (!httpRequest) {
              return false;
  
          }
  
          httpRequest.onreadystatechange = alertContents();
          httpRequest.open('GET', url);
          httpRequest.send();
  
          //Redirecting to the HelloUser.jsp
          window.location.replace(url);
  
      }
      function alertContents() {
  
          if (httpRequest.readyState === 4) {
  
              if (httpRequest.status === 200) {
  
                  alert(httpRequest.responseText);
  
              } else {
  
                  alert('There was a problem with the request.');
  
              }
  
          }
  
      }
      $(function(){
          $("#username").blur("/CheckUserAvailability");
      });
  
      function checkUserAvailability(){
          var name = $("#username").val();
  
          $.get(
                  "/CheckUserAvailabilityAction.do", 
                  { 'username': name },
                  function () {
                      var response = $(data).find("response").text();
                       $("#response").hide();
                       $("#response").empty();
                       $("#response").append(response);
                       $("#response").show("slow");               
                  } 
          );
      }
  
  
  
  
  
  &lt;/script&gt;
  
  &lt;link rel="stylesheet" type="text/css" href="css/styles.css"&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;New user - Registration form&lt;/title&gt;
  &lt;/head&gt;
  &lt;body id="bodyStyling"&gt;
      &lt;%
          if ("noSexSelected".equalsIgnoreCase(request
                  .getParameter("register"))) {
      %&gt;
      &lt;div id="registrationFailure"&gt;
          &lt;h4&gt;Please select a Sex&lt;/h4&gt;
      &lt;/div&gt;
      &lt;%
          }
      %&gt;
      &lt;form name="myForm" action="checkUserNameAvailability" method="post"&gt;
          &lt;!-- onsubmit="return validateForm(this);" --&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;&lt;h2 id="IntroductionReg"&gt;New User Registration&lt;/h2&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;Name:&lt;/td&gt;
                  &lt;td&gt;&lt;input name="username" id="username" type="text"
                   onblur="checkUserAvailability()" /&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;label id="usrLabel"&gt;&lt;/label&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;Password:&lt;/td&gt;
                  &lt;td&gt;&lt;input name="password" type="password" onclick= /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;Confirm password:&lt;/td&gt;
                  &lt;td&gt;&lt;input name="passwordConfirmation" type="password"
                      onclick="this.value''" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;Age:&lt;/td&gt;
                  &lt;td&gt;&lt;input name="age" onclick="this.value=''" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;Sex:&lt;/td&gt;
                  &lt;td&gt;&lt;select name="sex"&gt;
                          &lt;option value=""&gt;- Select -&lt;/option&gt;
                          &lt;option value="M"&gt;M&lt;/option&gt;
                          &lt;option value="F"&gt;F&lt;/option&gt;
  
                  &lt;/select&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;Address:&lt;/td&gt;
                  &lt;td&gt;&lt;input name="address" onclick="this.value=''" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;td class="myTableDataStyling"&gt;E-mail:&lt;/td&gt;
                  &lt;td&gt;&lt;input name="email" onclick="this.value=''" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;input type="submit" value="Register user" /&gt;
      &lt;/form&gt;
      &lt;div id="outputDiv"&gt;&lt;/div&gt;
  &lt;div id="response"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my struts.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
  &lt;struts&gt;
  
      &lt;!-- Here, I declare the actions for the com.tcs.controller package classes --&gt;
      &lt;package name="damo" extends="struts-default"&gt;
          &lt;action name="process" class="com.tcs.controller.User"&gt;
              &lt;result name="success"&gt;/process.jsp&lt;/result&gt;
          &lt;/action&gt;
  
  
          &lt;action name="login" class="com.tcs.action.LoginAction"&gt;
              &lt;result name="success"&gt;HelloUser.jsp&lt;/result&gt;
              &lt;result name="error"&gt;Login.jsp&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="register" class="com.tcs.action.RegisterUserAction"&gt;
              &lt;result name="success"&gt;process.jsp&lt;/result&gt;
              &lt;result name="error"&gt;process.jsp&lt;/result&gt;
          &lt;/action&gt;
  
          &lt;action name="checkUserNameAvailability" class="com.tcs.action.CheckUserAvailabilityAction"&gt;
              &lt;result name="success" type='redirect'&gt;register.action&lt;/result&gt;
              &lt;result name="error"&gt;process.jsp&lt;/result&gt;
          &lt;/action&gt;
  
  
  
      &lt;/package&gt;
  
  
  
  
  
  
  &lt;/struts&gt;
  </code></pre>
  
  <p>I'm struggling to get my checkUserAvailability function to make a valid request, I'm lost since I have minimal knowledge of struts. </p>
  
  <p>Help is greatly appreciated</p>
  
  <p>Thanks.</p>