• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to retain formatting when passing a String to a JSP

 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I'm in a pickle, and I don't know to fix it.

I have a program that makes a request to a website, then pulls one little string from the response from the website, and passes it as part of an ArrayList of Strings to a JSP that displays it as part of a table.

The response comes back from the website as an xhtml page, and the part that I parse out looks like this:



When I print this string to the console, it is formatted similar to above. However, when I pass the string to the JSP and display it on a webpage, all the formatting is lost and it prints as a single line:



I have 2 questions, 1: I'm curious how this is managed to be formatted like this without any html tags in the response I get from the website.

2: How can I preserve this formatting and display it on my JSP arranged in separate lines, with the three similar sequences aligned with each other.

Here's the code for my JSP, if that helps:

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could just wrap an HTML "pre" element around the text, and then it will be rendered in monospace font, with newlines and spacing preserved. Perhaps that's how it was done on the original page as well.
 
Campbell Ritchie
Marshal
Posts: 52589
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the JSP forum.
 
Campbell Ritchie
Marshal
Posts: 52589
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The String itself will be passed unchanged, but the way it is displayed can be varied.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Well, you could just wrap an HTML "pre" element around the text, and then it will be rendered in monospace font, with newlines and spacing preserved. Perhaps that's how it was done on the original page as well.


Thanks! That worked!

I had to insert the pre tags into the String object in the java program. It didn't work if I just added the pre tags to the JSP. Do you know why that is, just so I can improve my understanding of JSPs?

Thanks for your help!!

Matt
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Busse wrote:It didn't work if I just added the pre tags to the JSP.

Then you did something wrong. Adding them to the string in the servlet is atrocious.

Show us the code.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Show us the code.


I first tried to add the pre tags in the JSP (line 25):



That's going to add the pre tags to every column of the table that is generated based on the array. But when I tried that, the formatting was still wrong. Do the pre tags need to go inside the td tags? So then I tried adding the pre tags to the string in the Java model class (not the servlet, if that makes a difference):



I'm not sure I want all the columns to have the pre tags. Why is it atrocious to add the pre tags to the String?

Thanks!
Matt
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Busse wrote:
I'm not sure I want all the columns to have the pre tags. Why is it atrocious to add the pre tags to the String?

Then just put the <pre> tags around the columns which need them. A few more <c:if> statements?

And mixing data with markup shouldn't be necessary. If you do it, then JSTL is going to try to convert the "markup" in your data to real data, probably by escaping the < characters in it. Then you're going to have to fight against the JSTL to stop that from happening. So don't do that. Let your controller deal with the data and let your JSP deal with the markup.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre><td>${epParam}</td></pre>

Invalid HTML. This is not what is generated when you put the markup into the string.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
<pre><td>${epParam}</td></pre>

Invalid HTML. This is not what is generated when you put the markup into the string.


I don't think that is what is generated when I put the pre tags in the String. That is how I first tried to add pre tags to the JSP, which didn't work. Why is that invalid HTML? Where should the pre tags go in the html?

Thanks,

Matt
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Invalid HTML.


To elaborate: yes, <pre> outside of <td>, inside of <tr>, is the invalid part. You do want to put the <pre> inside the <td>.

It's atrocious because keeping markup and content separate is a cornerstone of good web design. If you mix them, you quickly end up with an unmaintainable mess. You're very close to doing things exactly right here, so it makes good sense to go all the way.
 
Matthew Busse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
To elaborate: yes, <pre> outside of <td>, inside of <tr>, is the invalid part. You do want to put the <pre> inside the <td>.

It's atrocious because keeping markup and content separate is a cornerstone of good web design. If you mix them, you quickly end up with an unmaintainable mess. You're very close to doing things exactly right here, so it makes good sense to go all the way.


I see, thank you for the explanation.

Ok, this worked(line 12):


This JSP generates a table from an ArrayList of ArrayLists, if I only wanted the pre tags on the 5th column of the table, I could do that with <c:if> tags? (I think it might be fine the way it is, but I'm just curious) Thanks for all your help, this is getting way deeper than the books I've been reading.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!