Struts 2 AJAX jQuery JSON example

Struts 2 AJAX jQuery JSON example

Here you will see how to use AJAX and jQuery to send request to the server and get JSON response back from the server and process it on JSP page.


  • Create project name: Struts2AJAXjQuery. Below is final project structure:

Struts 2 AJAX jQuery JSON example

  • pom.xml file:
        <!-- JASON Plug-in -->
        <!-- Google json-simple to convert json -->
  • web.xml:
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

    <display-name>Archetype Created Web Application</display-name>



  • index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib prefix="s" uri="/struts-tags"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<title>Struts 2 AJAX jQuery JSON example</title>
<link rel="stylesheet"
    href="//" />
<script type="text/javascript"
<script src="//"></script>
<script type="text/javascript">
    function makeAjaxCall(){
        var firstName = $('#firstName').val(); 
        var lastName = $('#lastName').val(); 
        var location = $('#location').val(); 
        var json = {"firstName" : firstName,"lastName" : lastName,"location" : location}; 
            url: "jqueryAJAXAction"+"?jsonRequestdata="+JSON.stringify(json),
            type: 'POST',
            dataType: 'json',           
            error:function(jqXhr, textStatus, errorThrown){

<s:head />

    <h2 style="color: green">
        <s:text name="label.welcome" />
    <s:form action="" namespace="/" method="post"
        <s:textfield id="firstName" size="30" maxlength="50" label="First Name" value=""/>
        <s:textfield id="lastName" size="30" maxlength="50" label="Last Name" value=""/>
        <s:textfield id="location" size="30" maxlength="50" label="Location" value=""/>
        <s:submit type="button" label="Get Ajax data for server" onclick="makeAjaxCall();return false;"/>           

label.welcome = Struts 2 AJAX jQuery JSON example
package com.javahonk.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.opensymphony.xwork2.ActionSupport;

public class JqueryAJAXAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    private String firstName;
    private String lastName;
    private String location;
    private String jsonRequestdata;
    @Action(value = "/jqueryAJAXAction", 
        results = { @Result(name = "success", type = "json") })
    public String execute() throws Exception {
        //Pull request data
        JSONObject json = (JSONObject)new JSONParser()
        System.out.println("First Name=" + json.get("firstName"));
        System.out.println("Last Name=" + json.get("lastName"));
        System.out.println("Location=" + json.get("location"));
        //Set data in response
        return ActionSupport.SUCCESS;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getLocation() {
        return location;

    public void setLocation(String location) {
        this.location = location;

    public String getJsonRequestdata() {
        return jsonRequestdata;

    public void setJsonRequestdata(String jsonRequestdata) {
        this.jsonRequestdata = jsonRequestdata;

  • To run this application please configure tomcat server with eclipse using this tutorial. Right Click on project –> Run As –> Run on Server. You will see below page:

Struts 2 AJAX jQuery JSON example

  • Click “Get Ajax data from server” to get JSON response from server (Please note: If you want to pass data in request to the server just enter value in input fields). You will see below page with data:


For more information about Struts 2 AJAX jQuery JSON example please read this tutorial

download Download Project:  Struts2AJAXjQuery

2 thoughts on “Struts 2 AJAX jQuery JSON example

  1. Hi Am getting the below Error with above example

    org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *