A common complaint from developers concerns the large number of blank lines, or white spaces, that often clutter the output of a JSP page. This typically occurs because white space will appear anywhere a JSP or JSTL tag is used.
For example, the following JSP or comment would produce three lines of white space in a rendered JSP page:
These extra blank lines have no effect on the rendered page, but they add extra payload to the response and they make the HTML source code more difficult to inspect.
Since JSP version 2.1 or later, you can use the following directive in a JSP page to remove this unnecessary white space:
Following is a before and after table illustrating the difference I found when applying the directive to some of my own pages.
| Page | Before | After |
|---|---|---|
| Page 1 | 38kb | 30kb |
| Page 2 | 24kb | 23kb |
| Page 3 | 38kb | 31kb |
| Page 4 | 20kb | 19kb |
| Page 5 | 26kb | 24kb |
| Page 6 | 22kb | 20kb |
| Page 7 | 22kb | 21kb |
The average KB saved is minimal, but every byte counts, right? In fact, what I have represented in the table is just the main container pages; I have not also included the weight of the pages returned within these as a result of asynchronous requests. Each of the pages I tested actually make ajax calls to retrieve inner contents that are sometimes heavier than the containing pages themselves.
From a page weight or performance perspective, the difference is certainly not as significant as what you'd get from something like minifying a large JavaScript or CSS file. But again, it is also about making your page source more legible and that can be helpful for maintaining and troubleshooting a site. Using the trim directive is easy to do and it makes a difference, so give it a try.