JSP page compilation fails, getting 65k byte limit error

So whenever you talk about JSP, the first and the foremost thing to have it in mind is, its HTML inside JAVA. So whenever the jsp pages are complied, Java converts these jsp pages into servlets. These servlets will have methods, variables like a normal Java class has.

Now the issue here says that when the jsp pages were complied to servlets, your one method grows so big in size that it crossed the barrier of 65k limit. Hence, the error.

First thing to look when you are getting 65k byte limit error is whether in your jsp page, you have defined a form(I am using forms here, because that's what I had faced) which is containing everything. 

Basically, JSP pages are  meant to display dynamic data, but it is not advisable to have lots of logic in it. The purpose of your handler/controller is to have logic embedded in it. So, if you have anything like this, try using multiple forms.

Now, there is one resolution given on other sites is, to update web.xml of your tomcat server to add mappedFile as false. This does solve the issue, but to a certain limit. 

Q. What it does, if you have mappedFile as TRUE ? 
A. While converting the jsp code to servlets, it creates a lot of out.println() statetment.

But if the mappedFile value is changed to FALSE, the servlet created will have only one out.println() statement.

Second resolution is to have the part of jsp code moved to another jsp page. 

There are two ways to include a different jsp page in your main jsp page.
Static include : With this include tag, 
<%@include file="different.jsp" %>
you are actually using the same code base. Think this as dividing the jsp codebase into multiple jsp pages. But remember this will not solve the issue.

Dynamic include : is the one which will save the day.
<jsp:include page="different.jsp" />
This complies the different jsp page independently and put the output on the main jsp page.

Third resolution is to have different forms. Since these forms are created as methods in servlets when jsp is complied, it will never cross the 65k limit and we can have multiple forms on the same jsp page.

This is completely based on your code base and how well it is written.

Now we have come this far, then lets get into the main issue. 

There are higher chances of your one section of jsp page calling another section or using it. 

e.g. You have Person' details in one form, but the address details are big so you decided to put it in another form. Now the problem is how to call an form inside a form.

NOTE : I am using Spring Webflow, so I will be talking in context to form:form tag.

Further Issue Details:

If we are using form:form tag, it has the commandName which binds the tags with the formObject you specify in your applicationContext.xml. Since, we are using different forms and the commandName is binded with (let's say) only Person object, then you cannot have different commandName for your other form if it is not mentioned in your applicationContext.xml file. 

Else you will get this error.

Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Address' available as request attribute

So, you thought of using the same commandName. This works fine in displaying the jsp page.

What's the catch here is, you have not defined your form object to contains another form. So, it will not be able to bind the object.

The things we are going to do here are :

  • Lets say you have your Person and Address objects created.
  • We will create another object Dummy, which will contain these two (Person and Address object).
  • In your applicationContext.xml, define the formObjectClass as Dummy.java
  • On your handler side, get both formObjects using getFormObject() method.
  • And this will bind the object together. (Assumption)
In the jsp code, also look for any thrid party tags used for any specific reason. This might also be creating a lot of unwanted lines of code to the servlet.

Comments