Click here to Skip to main content
15,889,863 members
Articles / REST
Tip/Trick

How to Get JSON Response in Struts 1

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
6 Oct 2016CPOL2 min read 38.3K   1
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)


Written By
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAn Alternative (Better?) Way Pin
Gullbyrd7-Oct-16 7:05
Gullbyrd7-Oct-16 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.