Soham
Soham

Reputation: 91

Inserting Multiple spaces in jsp

I am trying to insert multiple spaces between two words in jsp.

Is there a way to insert spaces without using    mulitple times?

Kindly help. Thank you.

Upvotes: 5

Views: 20232

Answers (4)

jsshah
jsshah

Reputation: 1741

In the JSP of your choice do the following at the bottom

<%!
public String getSpaces(int numSpaces)
{
  StringBuffer buffer = new StringBuffer(numSpaces);
  for(int i = 0; i < numSpaces; i++)
    buffer.append(" ");
  return buffer.toString();
}
%>

Then where you want space

This will have lot of spaces <%= getSpaces(10)%> here

Upvotes: 1

dsh
dsh

Reputation: 12213

In short, no.

HTML defines that sequences of multiple spaces have the same significance as a single space. There is also the non-breaking space, represented by the character entity &nbsp;. Do note that using spaces is not a good method for achieving layout and alignment (if that is your actual goal).

http://www.w3.org/TR/html401/struct/text.html

For all HTML elements except PRE, sequences of white space separate "words"

(empahsis mine)

Upvotes: 5

Benjamin Cox
Benjamin Cox

Reputation: 6120

It's not generally considered wise to stick a ton of nbsp's in your HTML, as the spacing is a formatting concern, rather than a semantic component of the HTML or the data, itself.

Instead, I usually try to use CSS to format things like that:

.spacey { word-spacing: 20px; }

<p class="spacey">This is some text with 20 pixels between each word.</p>

Upvotes: 1

Chander Shivdasani
Chander Shivdasani

Reputation: 10121

Enclose it in a <pre> tag. Example:

out.println("<pre>This text has   multiple      spaces </pre>");

You can use it whenever you need unusual formatting. Look at the link here for more details about the tag

Upvotes: 2

Related Questions