Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP trimDirectiveWhitespaces

Tags:

java

jsp

servlets

I am testing several ways to improve and reduce my response size, network traffic, and load time. One of the things I thought of using is the trimDirectiveWhitespaces element. I placed it in the page directive:

<%@ page contentType="text/html;charset=UTF-8" language="java" trimDirectiveWhitespaces="true"%> 

I added some empty new lines and didn't see any change in my response or when i view source of the document, new lines were there.

What is the correct usage of this and what am I doing wrong?

like image 765
NinaNa Avatar asked Sep 16 '14 14:09

NinaNa


2 Answers

trimDirectiveWhitespaces only removes empty lines around JSP tags and leaves other empty lines as they are.

Example:

<% page pageEncoding="UTF-8"%>X
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>X
<html>


<head>

This will become:

X
X
<html>


<head>

(note I added X to mark end-of-line for this example, so it is more obvious and stackoverflow wont trim them out)

If trimDirectiveWhitespaces="true" is added to the page directive, the output becomes:

<html>


<head>

The first two empty lines are removed, but the next two (between the html and head tags) are preserved.

like image 68
David Balažic Avatar answered Sep 19 '22 17:09

David Balažic


You can place the following configuration for trim-directive-whitespaces in web.xml as well:

<web-app xmlns=...>
(...)
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
  </jsp-config>
(...)
</web-app>
like image 29
SamDJava Avatar answered Sep 19 '22 17:09

SamDJava