Akshatha S R
Akshatha S R

Reputation: 1345

How to get the request body and response body in apache struts2? - post using $.ajax

I am using ajax to call the webservice and struts to get the request parameters. This much is working fine, but I am trying to get the response in terms of json or I just need a success or failure flag in return from the server. Here is my code.. Ajax part index.jsp

function funcName(str, str1){
    $.ajax({
        url : "${pageContext.request.contextPath}/webservicename",
        type : "post",
        data : {
            'key' : str,
            'Status' : str1
        },
        success : getResponse,
        error : function(xhr) {
            alert("An error occured: " + xhr.status + " " + xhr.statusText);
        }

    });
}

function getResponse(xhr){
    $("#responseBody").html(xhr);
}

The service webservicename is mapped to an action in struts.xml to a java class named DataAction DataAction.java

public class DataAction implements ServletRequestAware{
    HttpServletRequest httpServletRequest = null;

    @Override
    public void setServletRequest(HttpServletRequest arg0) {
        // TODO Auto-generated method stub
        httpServletRequest = arg0;
    }

    public String execute() {
        String key = httpServletRequest.getParameter("key");
        String status = httpServletRequest.getParameter("status");
        System.out.println(key +" "+ status);
        return "success";
    }

}

Till here it is working, I modified the DataAction class and tried to write something to the response using Print writer but it is showing the following error

java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

The modified code

public class DataAction implements ServletRequestAware, ServletResponseAware{
        HttpServletRequest httpServletRequest = null;
        HttpServletResponse httpServletResponse = null;


@Override
public void setServletRequest(HttpServletRequest arg0) {
    // TODO Auto-generated method stub
        httpServletRequest = arg0;
}



@Override
public void setServletResponse(HttpServletResponse arg0) {
    // TODO Auto-generated method stub
        httpServletResponse = arg0;
}

public String execute() {

    try {
            PrintWriter writer = httpServletResponse.getWriter();
            writer.println("hello server");
            writer.flush();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String key = httpServletRequest.getParameter("key");
        String status = httpServletRequest.getParameter("status");
        System.out.println(key +" "+ status);
        return "success";
    }

}

How to get the response body and also the request body?

Upvotes: 1

Views: 2216

Answers (1)

Roman C
Roman C

Reputation: 1

Since you writing to the http servlet response, the response is already commited, so you can't use any dispatcher result such as "success".

But you can return result NONE to prevent result execution after the action, and it's probably only the option.

Note, if you have any other interceptor on the stack, such as workflow interceptor, then it might return INPUT result, which is also a dispatcher result and should be excluded.

Upvotes: 1

Related Questions