I have a JSP page where I write the text of an existing HTML page from an ArrayList where I have stored the lines of the HTML page. The reason I am breaking the HTML page into lines is that I want to highlight some words. My code contains: <% DisplayForm displayForm = (DisplayForm)session.getAttribute("displayForm"); ArrayList<String> htmlStrings = displayForm.getHtmlStrings(); for (int i = 0; i < htmlStrings.size(); i++) { String nextString = htmlStrings.get(i); %> <% } %> <%= nextString %> <% } } %> The problem is that the page displays '?' where the HTML contains & (") and does the same thing for '. I suspect that other special characters are treated the same way. Please tell me how to solve this problem.

I don't see my problem is in the JSP FAQ. The link provided, has already been solved, doesn't answer the question because the problem is not with character in the file. The file displays perfectly in the browser, but the text created by the JSP changes characters from the string being written with <%= %>; it changes & to a ? and changes other characters like ' and " to ?.

Post by:Bear Bibeault
,
Marshal
staff

Mar 16, 2008 19:29:00

The file displays perfectly in the browser

What file?

Post by:Ben Souther
,
Sheriff
staff

Mar 17, 2008 05:59:00

Have you used your browser's 'view source' feature to see if the characters in question are being altered by JSP or if they are just being interpreted by the browser?

Post by:Alejandro Barrero
,
Ranch Hand

Mar 17, 2008 06:57:00

Thank you for your prompt replies. First the file I am talking about is o.htm; a file that displays correctly in the browser (firefox or Internet explorer). Second, when I construct the the HTML text with the JSP all the text is the same as the original file except some characters like ", ' and -. In the case of ". For some reason the original file some times contains the HTML equivalent which has & followed by # and 38 (it doesn't display in these posts) and some times it has a " that displays correctly (the original file was created with Microsot Word). Even the HTML equivalent of " is displayed as ?. I just created the JSP: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String s = "\"&Test'"; %> <%= s %> </body> </html> The string appears correctly in the body and it displays "&Test'. So it appears that creating the HTML with Word for windows is giving problems; it has lots of css. After the test with the previous page, I know the problem is not JSP. It looks like I will never find out what's the problem.

Post by:Ben Souther
,
Sheriff
staff

Mar 17, 2008 07:06:00

Classic JSP expressions ( <%= s %> ) don't attempt to massage the output text. Are you sure that one of the classes on your side isn't doing this?

Try printing the text (as it's coming out from your classes) to stdout and see if it's being escaped there.

Post by:Alejandro Barrero
,
Ranch Hand

Mar 17, 2008 08:07:00

Thanks for your replay Ben. My JSP is simply using <%= %>; the example in my previous post it works fine. I found out that the HTML file created with Word uses the value 8216 for '. The code: <% String s = "'test'" + (char)8216 + "&"; %> <%= s %> Prints 'test'?& Now I know what is the problem with the ' in the file. I can handle it and " and others. I am puzzled that <%= s %> is mangling &.

Post by:Ben Souther
,
Sheriff
staff

Mar 17, 2008 08:14:00

Originally posted by Alejandro Barrero: I am puzzled that <%= s %> is mangling &.

Well, there is something strange going on. In my test JSP I only have: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body> <% String s = "'test'" + (char)8216 + "&"; String z = "Z";%> <%= s %> <%= z %> </body> </html> And it is displaying: 'test'?& Z Not only it's mangling the HTML representation of " but it is adding a blank after the string. I am running this with MyEclipse in Apache. I haven't tried in other systems. It is a deep mystery. No other code created by me is intervening. It is so weird that we may never know. At any rate thank you very much for your replies.

Post by:Ben Souther
,
Sheriff
staff

Mar 17, 2008 16:08:00

This sounds more like a character encoding issue. Try printing that character as a string literal.

This thread has been viewed 2891 times.

All times above are in ranch (not your local) time.
The current ranch time is Dec 19, 2018 03:17:27.