JSP trimDirectiveWhitespaces

JSP trimDirectiveWhitespaces


Posted in : Java Posted on : May 2, 2012 at 6:38 PM Comments : [ 0 ]

In this tutorial you will learn about the trimDirectiveWhitespaces.

JSP trimDirectiveWhitespaces

In this tutorial you will learn about the trimDirectiveWhitespaces.

Before the JSP 2.1 version you were saw the unwanted white spaces in the response page these white spaces makes the html source code less legible and add the extra payload to the response. When the JSP 2.1 is introduced a new feature is added to it to remove the unwanted spaces from the response page. The trimDirectiveWhitespaces is used as a page directive or as a property-group configuration parameter that removes the blank lines or white spaces from the response output. However, it doesn't affect the response output it only removes the blank lines or white space from the html source code.

Example :

An example that I am giving here will demonstrate you about the trimDirectiveWhitespaces. In this example I will use it as the page directive attribute. So at first I have created a JSP page and at the beginning of the page I have set the trimDirectiveWhitespaces = "true" as a page directive attribute. Then perform some basic task for displaying the two concatenated string.

trimDirectiveWhitespaces.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" trimDirectiveWhitespaces="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>JSP trimDirectiveWhitespaces</title>
</head>
<body>
<h3>JSTL Core Tag choose, when, otherwise Example.</h3>
<c:set var="str" value="Dev"/>
<c:set var="str1" value="manuals"/>
<c:choose>
<c:when test="${str eq 'Dev'}">
<c:set var="concat" value="${str}${str1}" />
<c:out value="${concat}"></c:out>
</c:when>
<c:otherwise>
<b>"${str}"</b>
</c:otherwise>
</c:choose>

</body>
</html>

NOTE : In case you want to use this feature as a jsp property-group configuration parameter you may use it as follows :

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

Output :

1. When you will not use the trimDirectiveWhitespaces page directive or is set to false then the html source code of response will be as follows :

2. When you will use the trimDirectiveWhitespaces page directive or is set to true then the html source code of response will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics