0
votes

could someone please help me out? I am using JQuery $.getJSON() to load the contents of one dropdown (subcategories) based on the the selected dropdown (category).

I am getting an IllegalStateException for some reason. Thank you ahead of time.

<script>
    $(document)
            .ready(
                    function() {

                        // reload subcategories when changing category
                        $('#categories').change(loadSubcategories);

                    });

    // load the subcategories based on selected category
    function loadSubcategories() {
        $.getJSON('findSubcategoriesForCategory.html', {
            categoryId : $('#categories').val(),
            ajax : 'true'
        }, function(data) {
            var html = '<option value="">Select Subcategory</option>';
            var len = data.length;
            for ( var i = 0; i < len; i++) {
                html += '<option value="' + data[i].id + '">' + data[i].name
                        + '</option>';
            }
            html += '</option>';

            $('#subcategories').html(html);

        });
    }
</script>

Here is the code for the Controller method:

@RequestMapping(value = "findSubcategoriesForCategory", method = RequestMethod.GET)
public @ResponseBody
List<Subcategory> findSubcategoriesForCategory(
        @RequestParam(value = "categoryId", required = true) String categoryId) {

    List<Subcategory> returnList = new ArrayList<Subcategory>();

    try {
        returnList = categoryService
                .findSubcategoriesForCategory(categoryId);
    } catch (Exception e) {
        LOGGER.error("An Error occured when Finding sub-categories for category id: " + categoryId);
    }

    return returnList;

}

java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(Response.java:611) at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198) at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:112) 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.JspWriterImpl.write(JspWriterImpl.java:273) at java.io.PrintWriter.write(Unknown Source) at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:119) at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:326) at org.apache.jasper.runtime.JspWriterImpl.write(JspWriterImpl.java:342) at org.apache.jsp.WEB_002dINF.jsp.include.header_jsp._jspx_meth_c_005fforEach_005f1(header_jsp.java:341) at org.apache.jsp.WEB_002dINF.jsp.include.header_jsp._jspx_meth_c_005fforEach_005f0(header_jsp.java:293) at org.apache.jsp.WEB_002dINF.jsp.include.header_jsp._jspService(header_jsp.java:133) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646) at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551) at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488) at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:968) at org.apache.jsp.WEB_002dINF.jsp.errorMsg_jsp._jspService(errorMsg_jsp.java:69) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646) at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:551) at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:488) at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:229) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574) at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)

2
Is there a controller bound to findSubcategoriesForCategory.html, if so, what is the code related to it?dardo
I updated the original post and added the Controller method. Thanks.user879220
If you debug this method, where is the error happening. It's hard to tell from this servlet stack tracedardo
I just debuged the method. It seems that the "categoryId" parameter in the Controller is coming in as empty (""). If I can figure out why, then I think it will work. Thanks.user879220
so my guess is the service you're calling to find the categories is empty which is causing an exception, which is calling getOutputStream from within the Logger, but then hits the return statement and also calls getOutputStream, which is causing your servlet exception.dardo

2 Answers

1
votes

This is the response from your webserver. There is no Java involved on the front end. :)

0
votes

I had to add this @JsonIgnore in my Hibernate mappings getter methods for the code to work.