Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to retain formatting when passing a String to a JSP

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the JSP forum.
 
Campbell Ritchie
Marshal
Posts: 74387
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 26914
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67601
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic