Search code examples
javajsonhttp-headershttpresponsestruts-1

ArrayIndexOutOfBoundsException while writing JSON string to Printwriter in Struts


I am getting a List of rows from database and converting into JSONstring and writing it to PrintWritter to send output as JSON.

I was able to write some number of rows until now it is giving me ArrayIndexOutOfBoundsException at the line pointed in the below code.

Gson gson = new Gson();
String jsonString = gson.toJson(subAreaCriteriaList);
response.setHeader("X-JSON", jsonString);

PrintWriter writer = response.getWriter();
writer = response.getWriter();
writer.write(jsonString);//exception at this line
writer.flush();

Here is my action method

public Object doGetSubAreasifExist(BaseActionMapping mapping,
        ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        try {
            CurrentRoute route = new CurrentRoute();
            String areaID = request.getParameter("areaID");
            LoginService bbcService = (LoginService) Context.getInstance()
                                                            .getBean("LoginService");

            DetachedCriteria subAreaCriteria = DetachedCriteria.forClass(CabArea.class);
            subAreaCriteria.add(Restrictions.ne(CabArea.PROP_ID, Long.parseLong(areaID)));

            List<CabArea> subAreaCriteriaList = bbcService.findAll(subAreaCriteria);

            Gson gson = new Gson();
            String jsonString = gson.toJson(subAreaCriteriaList);
            response.setHeader("X-JSON", jsonString);

            PrintWriter writer = response.getWriter();
            writer = response.getWriter();
            writer.write(jsonString);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

here is my part of strut-config xml

<action path="/admin/cabsharing/getSubAreasifExist"
            type="com.ihexa.common.admin.cabsharing.action.CabsharingAction"
            name="actionForm">              
</action>

Here is my stacktrace

java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at org.apache.coyote.http11.InternalOutputBuffer.write(InternalOutputBuffer.java:701)
    at org.apache.coyote.http11.InternalOutputBuffer.sendStatus(InternalOutputBuffer.java:438)
    at org.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1626)
    at org.apache.coyote.http11.Http11Processor.action(Http11Processor.java:958)
    at org.apache.coyote.Response.action(Response.java:183)
    at org.apache.coyote.Response.sendHeaders(Response.java:379)
    at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:314)
    at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:274)
    at org.apache.catalina.connector.Response.finishResponse(Response.java:493)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:317)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

Solution

  • Based on the comment added by you I am able send certain amount of data but when i exceed it gives me the error, it seems like the header size (in your case it is response.setHeader("X-JSON", jsonString);) exceeded the maximum limit (refer Maximum on http header values? may be helpful) and the issue seems to be server specific.

    If you are using Apache Tomcat then you can refer maxHttpHeaderSize in Apache Tomcat Configuration Reference to modify conf/server.xml. Setting maxHttpHeaderSize in server.xml will fix your issue. Here is a sample server.xml file for maxHttpHeaderSize usage.

    You also have to close writer object as follows :

    writer.flush();
    writer.close();