2

I wrote a custom JSP tag that basically constructs a URL to the full domain name of my site, e.g. http://example.com. The problem is that within my tag file, I have spaces to properly format my code, and these spaces are included when I use the tag in JSP files.

For example, if I use my tag like this:

<link rel="stylesheet" type="text/css" href="<custom:domainNameUri />${stylesheet}" />

It will show up like this in the page's source code:

<link rel="stylesheet" type="text/css" href="



        http://example.com

/css/bootstrap.css" />

How do I prevent spaces from being output from my tag so it doesn't break things when I use the tag?

Edit: Here is part of the tag's source code:

<%@ attribute name="includePort" required="false" type="java.lang.Boolean" description="Whether or not to include the server port in the URI" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="domain" value="${pageContext.request.serverName}" />

<%-- Build domain name URI --%>
<c:set var="uri" value="${pageContext.request.scheme}://${domain}" />

<c:choose>
    <c:when test="${includePort == true}">
        <c:out value="${uri}:${pageContext.request.serverPort}" />
    </c:when>

    <c:otherwise>
        <c:out value="${uri}" />
    </c:otherwise>
</c:choose>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ba0708
  • 10,180
  • 13
  • 67
  • 99
  • Well, fix the code of the tag. Where is it? What's the use of this tag? What's wrong with `href="/css/bootstrap.css"`? – JB Nizet Jan 29 '15 at 18:23
  • @JBNizet I am new to creating tags, so I don't know how to avoid this. The tag is just printing out `http://example.com` as mentioned in the question. I need absolute URLs because my site uses many subdomains, and the spaces included in the `href` attribute by my tag breaks the stylesheet link. – ba0708 Jan 29 '15 at 18:39
  • 1
    http://stackoverflow.com/questions/208736/strip-whitespace-from-jsp-output – JB Nizet Jan 29 '15 at 20:24
  • @JBNizet Yes, I am using that already, but it only strips whitespaces at the top of my document (i.e. before the doctype declaration), not within tag attributes. So it solves another problem, but unfortunately not the one I am facing right now. – ba0708 Jan 30 '15 at 09:24

1 Answers1

0

use this to trim space.

<%@ page trimDirectiveWhitespaces="true" %>
Sanjay
  • 2,481
  • 1
  • 13
  • 28