0

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)
Cœur
  • 37,241
  • 25
  • 195
  • 267
haripcce
  • 180
  • 2
  • 2
  • 15
  • If you are using struts2.x then you may use struts inbuilt support for JSON, refer [struts2 and json example](http://www.mkyong.com/struts2/struts-2-and-json-example/) – Visruth Sep 27 '14 at 12:11
  • @VisruthCV i have pointed out the exception ouccrrring at which line number. Error is not about the response sending back but is raised inside method. And it has nothing to do with struts version .I am able send certain amount of data but when i exceed it gives me the error – haripcce Sep 27 '14 at 12:14
  • it seems to be struts1.x. – Visruth Sep 27 '14 at 12:27
  • Can you post the contents in `jsonString` (the one which generated this exception). – Visruth Sep 27 '14 at 13:26

1 Answers1

2

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();
Community
  • 1
  • 1
Visruth
  • 3,430
  • 35
  • 48
  • 1
    Actually it was not necessary to write in header for sending back json response. I have change the line response.setHeader("X-JSON", jsonString); to response.setHeader("X-JSON", "test"); Now its working fine . Thanx @Visruth CV – haripcce Sep 29 '14 at 05:09