Rey Rajesh
Rey Rajesh

Reputation: 500

How to display a string with new line character in a HTML page using scriplets?

<% 
String a="abc";
Srting b="xyz";
String c=a+"\n"+b;
%>

I want to display String c in a HTML table like this:

<table>
  <tr>
    <td><%= c %></td>
  </tr>
</table>

I want to get this:

--------
| abc  |
| xyz  |
--------

But I get this:

------------
| abc xyz  |
------------

Is there anything I could do with the scriplet to acheive this?

Upvotes: 3

Views: 17307

Answers (3)

Santhosh
Santhosh

Reputation: 8197

From your design ,

You can simply make it as,

<table>
  <tr>
    <td><%= a %></td>
  </tr>
  <tr>
    <td><%= b %></td>
  </tr>
</table>

to display it in the rows . Using <br/> in the table is meaningless.

If your intent is to display the multi-line td,

  1. How to show multiline text in a table cell
  2. Width of a <td> is narrower when I use a <br> in it on a fixed width <table>. Why?

Also scriptlets are discouraged over the decade . Please read How to avoid Java code in JSP files?

Upvotes: 1

atish shimpi
atish shimpi

Reputation: 5023

Try to use <br/> instead of "\n"

String c=a+"<br/>"+b;

Upvotes: 1

Nailgun
Nailgun

Reputation: 4169

Html has<br>tag for page breaks. So you can insert it in java code instead of \n:

String c=a+"<br>"+b;

Upvotes: 5

Related Questions