Recently, due to business requirements, I helped my colleagues to change the SELECT control on the JSP page. However, after the code was changed, an error message appeared on several pages with a large number of select components: _jspService exceeded the limit of 65535 bytes.

// Old <form:options items="${opts}" itemLabel="label" itemValue="value" htmlEscape="false"New code <c:forEach items="${opts}" var="d">
    <option value="d.value" label="d.label" title="d.label"></option>
</c:forEach>Copy the code

Although the problem was solved in other ways. But the mistake still haunts me. Just have nothing to do today, I decided to find out what the problem is. Since the rest of the problem page is unchanged, we can easily locate our problem code, which is the change from options to C :forEach.

Let’s look at the JSP compilation process: first, the client sends a request to the Web container; The Web container then compiles the JSP files into servlet source, and the servlet source into a class file. The Web container executes the class file and returns the response to the client. Unable to compile class for JSP. Compile failed because _jspService is too large.

According to the error message, we need to find the _jspService method first. Start debugging with the simplest empty page, compile, run, and access the page in your browser to generate Java files and class files. The following is the compiled class file. We successfully found the _jspService method, and we can see that this method is the output of our page.



Next, I created two JSP pages at the same time with the top code example, one with options tags and the other with C :forEach. Write () out.write(); out.write();



The first JSP uses the Options tag. As you can see from the diagram, the Options tag is compiled into a class file without any processing.



The second file uses c:forEach, and we see that the class file no longer contains the C :forEach tag. Instead, we see an if statement that evaluates the return value of a method that looks like forEach. At this point, the answer is already there. We continue to look in this file, and sure enough, the method appears below.



Inside this method is a do{}while() loop in which all options are printed. Since this method is too long to post the entire method, it will eventually return false and proceed with out.write(), which follows the _jspService method.

conclusion

Options elements are retained during JSP compilation to class and are handled later by Spring MVC, while each C :forEach tag is compiled at compile time into a function that outputs the target element. If the form requires a large number of select controls, it is best to use Options to do so. Of course, the best way is not to put too many elements into a JSP, can be flexible use of < JSP :include page=”” flush=”true” /> methods to decompose the file.