This problem also bothered me for a long time. Because of the El tags and other labels used within the JSP, a large number of spaces and line breaks are generated. For example:
Copy Code code as follows:
-------Start----------
<c:choose>
<c:when test= "${fn:length (mainpagelist) >1&}" >
Something
</c:when>
<c:otherwise>
Others
</c:otherwise>
</c:choose>
-------End-----------
This code is printed on tomcat as follows, with a few more lines to wrap.
Copy Code code as follows:
-------Start----------
Something
-------End-----------
Of course, generally do not pay special attention is not to find any problems, after all, output more space and line breaks, the final effect of browser generation is the same. That's why most developers ignore the problem, but in fact these spaces and change lines occupy a lot of space, my experience is 30% or so is the space/line. It is also said that when the Web server outputs HTML in a zip way, the space caused by the bandwidth problem can be solved; Yes, when you use zip to output HTML, the space/line break will be saved, but this adds zip to the workload, and the biggest problem is that when the browser generates the page, Or, a newline character that restores all the spaces. This is bad news for front-end developers, with lots of space and lengthy HTML source code, and it's not easy to find the right location for the offending style.
Here's the solution, take Tomcat for example:
Scheme one, take advantage of the trimspaces function of the Web server.
TOMCAT5 the above version can be used, this is the easiest way.
Copy Code code as follows:
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>trimspaces </param-name>
<param-value>true </param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
The disadvantage of this scheme is that it removes line breaks from all the spaces between the JSP El tags and is inconvenient in some cases.
such as: Your name is ${firstname} ${lastname}. = = output to ==> Your name is Firstnamelastname.
The spaces between the two ${} variables also disappear. To solve this problem it is rather troublesome to introduce a variable with only one space.
<c:set var= "One_space" > </c:set>
Your name is ${firstname}${one_space}${lastname}.
This is normal, trouble. Although it is possible to add a one_space variable to some global variable, the code still looks uncomfortable.
Option Two, I like it better.
This scenario is only available on Web servers that support JSP 2.1, such as TOMCAT6.
Jsp2.1 a more useful command;
<%@ page trimdirectivewhitespaces= "true"%>
This command can make the JSP output HTML when the extra empty line (using El and tag on the JSP will produce a lot of space and blank lines), no use of trimspaces problem, now JSP output HTML can also be very good typesetting, looks also professional I used to envy the velocity of the template, output HTML is very clean and good-looking, now JSP can also be.
Also, mention that TOMCAT6 has some compatibility issues, such as the inability to use #{} code in the JSP because it will be executed as a JSF script.
Although this is a very small problem, but we still have to pay attention to the details.