2

I have a JSP page which contains a table. On load of the page, the table will be populated. I also have an ajax call for every X seconds which has to refresh the table contents.

On load, the contents are populated as expected. But during ajax call, it fails with below error:

Jul 31, 2014 3:17:16 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcherServlet] in context with path        [/sample] threw exception [java.lang.IllegalStateException: getOutputStream() has    already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this    response
at org.apache.catalina.connector.Response.getWriter(Response.java:638)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.getWriter(SaveContextOnUpdateOrErrorResponseWrapper.java:109)
at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190)

I checked the existing questions on this issue, but with no good. I am not using scriplets in the code.

JSP COde:

$(document)
    .ready(
        function() {
            var performAjax = function() {
                $
                .ajax({
                    method : 'get',
                    contentType: 'application/json',
                    dataType: "json",
                    url : "${pageContext.request.contextPath}/refresh",
                    success : function(data) {
                        alert("got something");
                    },
                    error : function(e) {
                        alert('Error: ' + e);
                    }
                });
            }
        setInterval(performAjax, 15000);
    });

Controller code:

@RequestMapping(value = "/refresh")
public @ResponseBody
RefresingModel refresh(ModelMap modelMap,
    HttpSession session) {
        return refreshService.getUpdatedData();
    }
jww
  • 97,681
  • 90
  • 411
  • 885
Abhishek
  • 88
  • 1
  • 6

2 Answers2

0

Don't use out object in JQuery. It is a implicit object in JSP for OutputStream:

$(document)
        .ready(
                function() {
                    var performAjax = function() {
                        $
                                .ajax({
                                    method : 'get',
                                    contentType: 'application/json',
                                    dataType: "json",
                                    url : "${pageContext.request.contextPath}/refresh",
                                    success : function(data) {
                                        alert("got something");

                                    },
                                    error : function(e) {
                                        alert('Error: ' + e);

                                    }
                                });
                    }
                    setInterval(performAjax, 15000);
                }); 
Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
  • Oops. That was a type. I was looking at existing solutions and added that. I have removed that now and still face the same issue. – Abhishek Jul 31 '14 at 10:21
0

The same exception is thrown when the data returned has a recursive relationship: e.g Object A has collection Of Object B and and Object B has property of Type A.

    A: {
  id:23, 
  Collection: [
   B:{
       id:21,       
       parent: A
   }
 ]
}

Soln: add jsonIgnore on property inside Object B