当前位置: 首页 > 知识库问答 >
问题:

IllegalStateException: getOutputStream()已经为此响应调用

孙梓
2023-03-14

我有一个包含表的JSP页面。加载页面时,将填充表。我还有一个每隔X秒的ajax调用,它必须刷新表内容。

加载时,按预期填充内容。但在ajax调用期间,它会失败,并出现以下错误:

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)

我检查了这个问题上存在的问题,但没有什么好结果。我没有在代码中使用scriplets。

JSP代码:

$(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);
    });

控制器代码:

@RequestMapping(value = "/refresh")
public @ResponseBody
RefresingModel refresh(ModelMap modelMap,
    HttpSession session) {
        return refreshService.getUpdatedData();
    }

共有2个答案

汪典
2023-03-14

当返回的数据具有递归关系时,会引发相同的异常:例如,对象a具有对象B的集合,而对象B具有类型a的属性。

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

解决方案:在对象B内的属性上添加jsonIgnore

柴晔
2023-03-14

不要在JQuery中使用out对象。它是用于OutputStream的JSP中的隐式对象:

$(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);
                }); 
 类似资料: