Whitespace
Whitespace is used everywhere. It covers spaces, tabs and newlines. It is used to distinguish lexical tokens from each other and also to keep the source code readable for the developer. But in case of HTML over network, whitespace costs bandwidth and therefore in some circumstances also money and/or performance. If you care about the bandwidth usage and/or the money and/or performance, then you can consider to trim off all whitespace of the HTML response. The only con is that it makes the HTML source code at the client side almost unreadable.
You can trim whitespace right in the HTML files (or JSP or JSF or whatever view you're using, as long as it writes plain HTML response), but that would make the source code unreadable for yourself. Better way is to use a Filter which trims the whitespace from the response.
Back to top
Replace response writer
Here is how such a WhitespaceFilter can look like. It is relatively easy, it actually replaces the writer of the HttpServletResponse with a customized implementation of PrintWriter. This implemetation will trim whitespace off from any strings and character arrays before writing it to the response stream. It also take care of any and tags and keep the whitespace of its contents unchanged. However it doesn't care about the CSS white-space: pre; property, because it would involve too much work to check on that (parse HTML, lookup CSS classes, sniff the appropriate style and parse it again, etc). It isn't worth that effort. Just use tags if you want to use preformatted text ;)
Note that this filter only works on requests which are passed through a servlet which writes the response to the PrintWriter, e.g. JSP and JSF files (parsed by JspServlet and FacesServlet respectively) or custom servlets which uses HttpServletResponse#getWriter() to write output. This filter does not work on requests for plain vanilla CSS, Javascript, HTML files and images and another binary files which aren't written through the PrintWriter, but through the OutputStream. If you want to implement the same thing for the OutputStream, then you'll have to check the content type first if it starts with "text" or not, otherwise binary files would be screwed up. Unfortunately in real (at least, in Tomcat 6.0) the content type is set after the output stream is acquired, thus we cannot determine the content type during acquiring the output stream.
The stuff is tested in a Java EE 5.0 environment with Tomcat 6.0 with Servlet 2.5, JSP 2.1, JSTL 1.2 and JSF 1.2_06.
/*};
* net/balusc/webapp/WhitespaceFilter.java
*
* Copyright (C) 2007 BalusC
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if
* not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package net.balusc.webapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* This filter class removes any whitespace from the response. It actually trims all leading and
* trailing spaces or tabs and newlines before writing to the response stream. This will greatly
* save the network bandwith, but this will make the source of the response more hard to read.
*
* This filter should be configured in the web.xml as follows:
*
* <filter>
* <description>
* This filter class removes any whitespace from the response. It actually trims all
* leading and trailing spaces or tabs and newlines before writing to the response stream.
* This will greatly save the network bandwith, but this will make the source of the
* response more hard to read.
* </description>
* <filter-name>whitespaceFilter</filter-name>
* <filter-class>net.balusc.webapp.WhitespaceFilter</filter-class>
* </filter>
* <filter-mapping>
* <filter-name>whitespaceFilter</filter-name>
* <url-pattern>/*</url-pattern>
* </filter-mapping>
*
*
* @author BalusC
* @link http://balusc.blogspot.com/2007/12/whitespacefilter.html
*/
public class WhitespaceFilter implements Filter {
// Constants ----------------------------------------------------------------------------------
// Specify here where you'd like to start/stop the trimming.
// You may want to replace this by init-param and initialize in init() instead.
static final String[] START_TRIM_AFTER = {", ", "
static final String[] STOP_TRIM_AFTER = {", "
Tidak ada komentar:
Posting Komentar