JSP output HTML generated a large number of Spaces and line feed removal method

  • 2020-05-24 05:58:17
  • OfStack

This problem also bothered me for a long time, because the EL tag and other tags used in jsp would generate a lot of Spaces and newlines, such as:
 
------- 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 a few more line breaks.
 
------- start ---------- 
Something 
------- end ----------- 

Of course, if you don't pay special attention to 1, you won't find any problems. After all, the output of more Spaces and newlines will have a similar final effect on the browser. That's why most developers ignore this problem; But actually these Spaces and line feeds take up quite a bit of space, and my experience is that around 30% are space/line feeds. Others say that when the web server outputs html in zip mode, the bandwidth problem caused by whitespace can be solved. Yes, space/line feed is saved when zip is used to output html, but this increases the amount of work zip has to do, and the biggest problem is that when the browser generates a page, it still restores all the blank line feeds. This is bad news for front-end developers, who are faced with a lot of whitespace and lengthy html source code, and it is not easy to find the location of the style in question.
Here is the solution, taking Tomcat as an example:
Scheme 1 utilizes the trimSpaces function of the web server.
This is the easiest way to use Tomcat5 or more.
 
<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> 

One disadvantage of this scheme is that it removes all white space newlines between the jsp EL tags, which in some cases is also inconvenient.
For example, Your name is ${firstName} ${lastName}. == output is == > Your name is firstNamelastName.
The space between the two ${} variables also disappears. To solve this problem, you have to introduce a variable with only one space.
< c:set var="one_space" > < /c:set >
Your name is ${firstName}${one_space}${lastName}.
That's normal, please. Although it is possible to add an one_space variable to one of the global variables, the code still looks uncomfortable.
Plan 2. I prefer it.
This scheme is only available on web servers that support jsp 2.1, such as Tomcat6.
Jsp 2.1 has one more useful command;
< %@ page trimDirectiveWhitespaces="true" % >
This command can make jsp output html to get rid of the extra blank lines (EL and tag will produce a lot of blank space and blank lines on jsp), without the problem of trimSpaces. Now jsp output html can also be well formatted and looks professional. Before, I always envied velocity's template, and the output html10 was clean and good-looking. Now jsp is also ok.
As a side note, Tomcat6 has some compatibility issues. For example, you cannot use #{} in jsp because it will be executed as an JSF script.
Although this is a small question, we still need to pay attention to the details.

Related articles: