How to remove a large number of spaces and line breaks generated when JSP outputs HTML

Source: Internet
Author: User

This problem also plagued me for a long time, because a large number of spaces and line breaks are generated when EL labels and other labels used in jsp. For example:
Copy codeThe Code is as follows:
------- Start ----------
<C: choose>
<C: when test = "$ {fn: length (mainPageList)> 1 &}">
Something
</C: when>
<C: otherwise>
Others
</C: otherwise>
</C: choose>
------- End -----------

The output of this Code on Tomcat is as follows, with several lines of line breaks added.
Copy codeThe Code is as follows:
------- Start ----------
Something
------- End -----------

Of course, you will not find any problems unless you pay special attention to them. After all, the output of more spaces and line breaks will produce the same final effect for the browser. That is why most developers ignore this problem. However, in fact, these spaces and line breaks occupy a lot of space. In my experience, about 30% is space and line breaks. Some people also say that when the web server outputs html in zip mode, the bandwidth problem caused by space can be solved; yes, when zip is used to output html, space for space and line feed will be saved back, but this increases the zip workload, and the biggest problem is that when the browser generates a page, it will still restore the line breaks of all spaces. This is a bad news for front-end developers. In the face of a large number of spaces and lengthy html source code, it is not easy to find the corresponding problematic style location.
The following describes the solution. Tomcat is used as an example:
Solution 1: Use the trimSpaces function of the web server.
Tomcat 5 and later versions can be used, which is the simplest method.
Copy codeThe Code is 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>

This solution has a disadvantage. It will remove the line breaks between all jsp EL labels, which may cause inconvenience in some cases.
For example, Your name is $ {firstName }$ {lastName}. = The output is ==> Your name is firstNamelastName.
Spaces between two $ {} variables also disappear. To solve this problem, it is quite 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 and troublesome. Although you can add a one_space variable in some global variables, the Code still looks uncomfortable.
Solution 2: I like it.
This solution can be used only on web servers that support jsp 2.1, such as Tomcat 6.
Jsp2.1 has an additional useful command;
<% @ Page trimDirectiveWhitespaces = "true" %>
This command can remove unnecessary blank lines when jsp outputs html (using EL and tag on jsp will generate a lot of spaces and empty lines) without trimSpaces, now jsp output html can also be well formatted and looks professional. I have always envied the velocity template before, and the output html is very clean and nice, so can jsp output.
In addition, Tomcat6 has some compatibility issues. For example, the Code # {} cannot be used in jsp because it will be executed as a JSF script.
Although this is a small problem, we still need to pay attention to the details.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.