Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / REST

How to Get JSON Response in Struts 1

3.00/5 (2 votes)
6 Oct 2016CPOL2 min read 39.2K  
The simplest way to return json objects to client using struts 1

Introduction

Sometimes, you have to integrate your mobile or other web applications to your previous web applications via webservices and the first thing that comes into your mind is to get a simple JSON type data, which is easy to parse and manipulate and does not put extra burden on the existing architecture.

If your legacy systems are built over struts 1 which is quite less used these days especially, after struts 2; almost everyone has moved to struts 2 because it is quite flexible as compared to struts 1. Most importantly, struts 2 have plugins for JSON and it's easy to build APIs in that.

Background

Struts 1 is quite rigid and you cannot integrate APIs right away, especially when it comes to returning response in JSON format, Struts 1 usually sends back a jsp file response against each action.

Using the Code

Now, I will map a simple request to an action and further to a jsp file. Then, I will manipulate the JSP file to get the required format.

In struts-config.xml, the action would go as usual. Like shown below.

Java
<action path="/callAction" type="com.myProjects.myActionClass">
<forward="success" path="/output.jsp"></forward>
</action> 

This is how your action class would be, I am simply putting values of string and int in JSONObject and that JSONObject into session, which I can retrieve in jsp file.

Java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import org.json.simple.JSONObject;

public class myAction extends Action
{
   public ActionForward execute ( 
        ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response ) throws IOException, ServletException
    {
        JSONObject returnObject = JSONObject();
        DynaValidatorForm myForm = (DynaValidatorForm) form;
        String receivedMessage = (String) myForm.get( "receivedMessage" );
        String responseMessage = "Your Message has been received";
        int number = 100
        returnObject.put("Sent Message", receivedMessage);
        returnObject.put("Response Message", responseMessage);
        returnObject.put("Number", number);
         
        return mapping.findForward( "success");
    }
}

Now output.jsp would be like this:

JavaScript
<%@ page language="java" 
contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="org.json.simple.JSONObject"%>

<%
	JSONObject obj = (JSONObject)request.getSession().getAttribute("json");
%>

<%=obj%>

The first line would tell client's browser to expect content in JSON Format.
Now from restClient Application, you can send the queryParams and path. The queryparams would be mapped to action form.

Query param would be like this:

receivedMessage=Hello

The response received at client's end would be like:

{
"Sent Message": "Hello"
"Number": 10
"Response Message": "Your Message has been received"
}

Points of Interest

The limitation over here is when client sends a request, it is limited to sending request parameters in query params, because the struts 1 is unable to process a request if data is sent in JSONObjects. Although I have observed that it does carry data content to filter class but afterwards, the filter is unable to resolve the action mapping from struts-config.xml because it is always expecting "ActionForm" objects in its parameters rather than any other object.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)