JSP trimDirectiveWhitespaces Example

JSP trimDirectiveWhitespaces Example explains about how to remove unwanted white spaces from your JSP responses

trimDirectiveWhitespaces is new feature available on JSP verion 2.1, as before we can't able to eliminate unwanted white spaces

By using this feature you can have a complete control on your JSP pages and you can trim down the JSP response by removing the white spaces

Below example, I am showing how to trim down a JSP page using trimDirectiveWhitespaces page directive tag

For this, I am creating a simple JSP page and adding trimDirectiveWhitespaces = "true" as a page directive attribute (see the JSP page)

Required Libraries

You need to download

  1. Tomcat 7
Note

If you want to use this feature as property group then you need to configure like this:

<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>

JSP trimDirectiveWhitespaces Example

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" 
trimDirectiveWhitespaces="true"%>

<html>
<head>
<title>JSP trimDirectiveWhitespaces </title>
</head>
<body>


   This is a TEST String Using JSP


</body>
</html>
Output

When you are not using trimDirectiveWhitespaces or you are setting trimDirectiveWhitespaces="false"

<html>
<head>
<title>JSTL trimDirectiveWhitespaces</title>
</head>
<body>

THIS IS A TEST STRING USING JSP

</body>
</html>

When you are setting trimDirectiveWhitespaces="true", Now you can see the whitespaces are removed

<html>
<head>
<title>JSTL trimDirectiveWhitespaces</title>
</head>
<body>
THIS IS A TEST STRING USING JSP
</body>
</html>

 









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