1

In the response my server gives to the client, I am noticing quite a bit of whitespace and newlines. The response body seems to be sending a newline for every line of parsed JSP. I am just curious if this is a problem someone has already written a bean for? Is this something I even need to worry about?

Example JSP:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@page import="java.util.Date"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Properties"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.IOException"%>
<head>
  <title>My Page!</title>
  <% String message = "Hello World!"; %>
</head>
<body>
  <div><%=message%></div>
</body>
</html>

Example HTML Response:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


<!-- THESE ARE BLANK NEWLINES ... -->



<head>
  <title>My Page!</title>

</head>
<body>
  <div>Hello World!</div>
</body>
</html>
wfoster
  • 781
  • 10
  • 23

3 Answers3

6

Yes, you can do that. Try:

<%@ page trimDirectiveWhitespaces="true" %>

More details are available here: Strip whitespace from jsp output

Community
  • 1
  • 1
aroth
  • 54,026
  • 20
  • 135
  • 176
1

@aroth has to most correct answer. But it also have consequences, mostly minor.

Alternative is to not have line endings between the tags. e.g:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@page import="java.util.Date"%><%@page 
import="java.util.GregorianCalendar"%><%@page 
import="java.util.Properties"%><%@page 
import="java.util.Map"%><%@page 
import="java.util.HashMap"%><%@page 
import="java.io.IOException"%><head>
  <title>My Page!</title>
  <% String message = "Hello World!"; %>
</head>
<body>
  <div><%=message%></div>
</body>
</html>

BTW you can have several imports in a jsp tag:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@page import="java.util.Date,java.util.GregorianCalendar,java.util.Properties,java.util.Map,java.util.HashMap,java.io.IOException"%><head>
  <title>My Page!</title>
  <% String message = "Hello World!"; %>
</head>
<body>
  <div><%=message%></div>
</body>
</html>
flurdy
  • 3,782
  • 29
  • 31
0

You can do this in code by calling the reset method on HttpServletResponse

Jef
  • 11
  • 1