Remove Whitespace From HTML / JSP / JSF

Remove Whitespace From HTML / JSP / JSF explains about implementing a servlet filter for removing whitespace & blank lines from your html/jsp/jsf output, which will reduce the payload and improve the overall web application performance

If you need a solution for below questions

How To Remove Whitespace From Response In Your Java Web Application?
How To Remove Whitespace From JSP/JSF And HTML?

then servlet filter will be the right choice, because by using servlet filter, we can implement this in a single place rather than on implementing every page

if you dont know about Servlet Filter, you can follow servlet filter Tutorial Here I am using, whitespace removing filter available from http://balusc.blogspot.in/2007/12/whitespacefilter.html

By using this filter you can strip/trim whitespace & newline characters from your html/jsp/jsf pages without losing actual contents

Required Libraries

You need to download

  1. Tomcat 7

Following jar must be in classpath

  1. whitespacefilter.jar (I am created a jar file using WhitespaceFilter.java)

Package Structure

Remove Whitespace From HTML / JSP / JSF

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	<display-name>whitespace Filter</display-name>
	<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>
</web-app>
Note

Javatips.net is using this filter for optimizing the source code (please see the below screenshot)

Output

Remove Whitespace From HTML / JSP / JSF











Your email address will not be published. Required fields are marked *