title: How to redirect to another JSP after getting success result using Ajax call in Struts 2
body: <p>I am using Struts 2 to register a user.</p>
  
  <p>I am using Ajax call to register a user, after getting register I am able to get the success message in success function at Ajax call.  But its not getting redirect to other JSP based on <code>struts.xml</code> result <code>type=success</code>.</p>
  
  <p><strong><code>register.jsp</code>:</strong></p>
  
  <pre><code>&lt;s:form id="form"&gt;
       &lt;s:textfield name="email" id="email" label="Email" required="true" onkeyup="loadXMLDoc()" onblur="userNameCheck()"&gt;&lt;/s:textfield&gt;
       &lt;s:textfield name="mobile" id="mobile" label="Mobile"&gt;&lt;/s:textfield&gt;
       &lt;s:textfield name="username" id="username" readonly="true"
        label="Username"&gt;&lt;/s:textfield&gt;
       &lt;s:textfield name="first_name" id="first_name"
        label="First Name"&gt;&lt;/s:textfield&gt;
       &lt;s:textfield name="last_name" id="last_name"
        label="Last Name"&gt;&lt;/s:textfield&gt;
       &lt;s:password name="password" id="password" label="Password"&gt;&lt;/s:password&gt;
       &lt;s:password name="uconfirm_password" id="confirm_password"
        label="Confirm"&gt;&lt;/s:password&gt;
       &lt;div class="button"&gt;
        &lt;input type="reset" name="cancel" id="cancel" value="Cancel"&gt;
        &lt;input type="submit" name="save" id="save" value="Save"&gt;
       &lt;/div&gt;
      &lt;/s:form&gt;
  </code></pre>
  
  <p><strong>Ajax call:</strong></p>
  
  <pre><code>$.ajax({ // Ajax function to call the action 
      type : 'POST',
      contentType : 'application/json',
      url : 'user',
      dataType : "html",
      data : registerFormToJSON(),
      success : function(html) {
          alert("Successfully Registered");
  
      },//End of success function in ajax call
  
      error : function(err) {
  
          console.log("Fail");
          return true;
      } //End of error function in ajax call
  });
  return false;
  }
  });
  
  
  function registerFormToJSON() {
  
      var data = JSON.stringify({
  
          "userbean" : {
              "username" : $("#username").val(),
              "password" : $("#password").val(),
              "firstname" : $("#first_name").val(),
              "lastname" : $("#last_name").val(),
              "confirmpassword" : $("#confirm_password").val(),
              "email" : $("#email").val(),
              "mobile" : $("#mobile").val(),
  
          }
  
      }); //End of getting form values and storing in json variable
      alert(data);
      return data;
  
  }//End of function registerFormToJSON()
  </code></pre>
  
  <p><strong>Action Class Method:</strong></p>
  
  <pre><code>public class UserController implements Action {
      private UserBeans userbean;
  
      /**
       * @return the userbean
       */
      public UserBeans getUserbean() {
          return userbean;
      }
  
      /**
       * @param userbean
       *            the userbean to set
       */
      public void setUserbean(UserBeans userbean) {
          this.userbean = userbean;
      }
  
      @Override
      public String execute() {
          return SUCCESS;
      }
  
      public String register() {
          UserService service = new UserServiceImpl();
          userbean = service.addorUpDateUser(userbean);
          // response.setContentType("json");
          return ActionSupport.SUCCESS;
      }
  }
  </code></pre>
  
  <p><strong>DAO Class method:</strong></p>
  
  <pre><code>public UserBeans addOrUpdateUser(UserBeans userBean) {
      properties.load(inputStream);
      con = conn.createConnection();
      System.out.println("name is mine" + userBean.getUsername());
      String encryptedPassword = PasswordUtil.encryptPassword(userBean.getPassword());
      String query = properties.getProperty("users_query");
      System.out.println(query);
      System.out.println("connection:" + con);
      preparedstatement = con.prepareStatement(query);
      preparedstatement.setString(1, userBean.getUsername());
      preparedstatement.setString(2, encryptedPassword);
      preparedstatement.setString(3, userBean.getEmail());
      preparedstatement.setString(4, userBean.getFirstname());
      preparedstatement.setString(5, userBean.getLastname());
      preparedstatement.setString(6, userBean.getMobile());
      preparedstatement.setString(7, userBean.getConfirmpassword());
      preparedstatement.executeUpdate(); // execute insert statement
      logger.info("UserDaoImpl  addOrUpDate Ends");
      return userBean;
  }
  </code></pre>
  
  <p><strong><code>struts.xml</code>:</strong></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;constant name="struts.action.extension" value="," /&gt;
      &lt;package name="default" extends="struts-default,json-default"
          namespace="/"&gt;
          &lt;action name="users" class="com.sample.controller.UserController"
              method="register"&gt;
              &lt;interceptor-ref name="defaultStack" /&gt;
              &lt;interceptor-ref name="json"&gt;
                  &lt;param name="enableSMD"&gt;true&lt;/param&gt;
              &lt;/interceptor-ref&gt;
              &lt;result name="success" type="json"&gt;/login.jsp&lt;/result&gt;
          &lt;/action&gt;
  &lt;/package&gt;
  &lt;/struts&gt;
  </code></pre>