How to delete empty lines after JSP Compilation

Source: Internet
Author: User
    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:

 
 
  1. <%@ 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:

 
 
  1. <jsp-config> 
  2. <jsp-property-group> 
  3. <url-pattern>*.jsp</url-pattern> 
  4. <trim-directive-whitespaces>true</trim-directive-whitespaces> 
  5. </jsp-property-group> 
  6. </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:

 
 
  1. <init-param> 
  2. <param-name>trimSpaces</param-name> 
  3. <param-value>true</param-value> 
  4. </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:

 

 Hello world

 

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

 

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.