I'm coding a Java Servlet that has to handle my request and return a string as response.
The response is composed by 2 lines, so I write my servlet code:
response.getWriter().println(line1 + "\n");
response.getWriter().println(line2);
But in my html page, the response is displayed as an unique line. How can I display my response in 2 different line ?
My project is an Angular2 project and I retrieve the response in this way:
constructor(private http: Http) { }
this.http.get(url)
.toPromise()
.then(response => response.text())
EDIT
I solved changing my HTML result, by adding the attribute innerHTML:
<div innerHTML="{{response text}}">
答案 0 :(得分:1)
Linebreaks和其他空格都在HTML中作为单个空格处理。这与Java,Angular或servlet无关。
如果要在HTML中分隔一行,可以使用单独的元素,<br>
或pre
格式化元素或其他各种选项。
.pre {
white-space: pre;
}
<div>
Line 1
Line 2
Note there is no displayed line break
</div>
<hr>
<div>
Line 1
<br>Line 2
<br>Note there is a displayed line break
</div>
<hr>
<div>
<div>Line 1</div>
<div>Line 2</div>
<div>Note there is a displayed line break</div>
</div>
<hr>
<pre>
Line 1
Line 2
Note there is a displayed line break
</pre>
<hr>
<div class="pre">
Line 1
Line 2
Note there is a displayed line break
</div>
答案 1 :(得分:0)
Because \n
is whitespace in HTML, and HTML 'user agents should collapse input white space sequences when producing output inter-word space'. Perhaps you are looking for <br/>
?
NB You should be using print()
, or write()
, not println()
.