Http://blog.csdn.net/st780206/archive/2010/03/12/5372736.aspx
When you use View Source on the client to view the code generated by JSP, you will find many blank lines, which are generated by <%... %> is generated after the carriage return, that is, each line is generated by <%... %> the JSP code contained in the client becomes a blank line. Although it does not affect browsing, it still hopes to delete them. Here we will introduce how to delete the empty lines after JSP compilation.
The following describes how to delete the empty lines after JSP compilation by Tomcat:
1. jsp 2.1 + is supported. The following code is contained in each page to be empty rows:
- <%@ page trimDirectiveWhitespaces="true" %>
The JSP compilation is successfully tested under Tomcat 6.0.14.
2. Support servlet 2.5 +, that is, the XSD version of Web. XML is 2.5. Add the following code to Web. xml:
- <jsp-config>
- <jsp-property-group>
- <url-pattern>*.jsp</url-pattern>
- <trim-directive-whitespaces>true</trim-directive-whitespaces>
- </jsp-property-group>
- </jsp-config>
The JSP compilation is successfully tested under Tomcat 6.0.14.
3. Tomcat 5.5.x +. In the tomcat installation directory/CONF/Web. XML, find the servlet named "jsp" and add the following code:
- <init-param>
- <param-name>trimSpaces</param-name>
- <param-value>true</param-value>
- </init-param>
I have never tested it, but the help of the web. xml file in Tomcat said so.
Trimspaces shocould white spaces in template text between actions or directives be trimmed? [False]
In actual operations, I added the 5.5 configuration to the page and started Tomcat several times repeatedly, but it was still unsuccessful. Then I thought that JSP has been compiled into servlet, so it could not be changed, go to the work directory in Tomcat and delete all the JSP-compiled classes. Wow, the whole world is clean and empty lines are successfully deleted.
Resin can delete the empty lines after JSP compilation as follows:
I'm getting a lot of whitespace in my JSP that I don't intend to be there. Why is it appearing and how can I get rid of it?
The extra whitespace is coming from newlines, often at the end of declaration lines at the beginning of the JSP. For example, the following JSP:
<%@ page import='java.util.*' %><%@ page import='java.io.*' %>Hello world |
Has newlines in these locations:
<%@ page import='java.util.*' %>NL<%@ page import='java.io.*' %>NLHello worldNL |
The result contains the newlines, which may be surprising:
One solution is to let the JSP tag extend into ss the newline:
<%@ page import='java.util.*'%><%@ page import='java.io.*'%>Hello world |
Another solution is to use JSP comments to remove the newlines:
<%@ page import='java.util.*' %><%----%><%@ page import='java.io.*' %><%----%>Hello world |
Another solution is to use the XML syntax of JSP. parsing of XML causes removal of extra whitespace.
<jsp:root><jsp:directive.page import="java.util.*"/><jsp:directive.page import="java.io.*"/><jsp:text>Hello world</jsp:text></jsp:root> |
Resin also supports the use of the '/' character to eat whitespace (this is a resin specific feature ):
<%@ page import='java.util.*' %>/<%@ page import='java.io.*' %>/Hello world |