My java webapp fetches content from a textarea, and e-mails the same.
The problem I'm facing is that the newline character in the textarea message is not preserved when reading the same using
request.getParameter("message");
Any clues how it can be tackled?
TIA.
EDIT:
The content in the textarea is:
abcd
abcd
CODE:
String message = request.getParameter("message");
System.out.println("index loc for message "+message+" using \\r\\n : "+message.indexOf("\r\n"));
System.out.println("index loc for message "+message+" using \\n : "+message.indexOf("\n"));
System.out.println("index loc for message "+message+" using \\r : "+message.indexOf("\r"));
System.out.println("index loc for message "+message+" using \\n\\r : "+message.indexOf("\n\r"));
OUTPUT:
index loc for message asdfasdf using \r\n : -1
index loc for message asdfasdf using \n : -1
index loc for message asdfasdf using \r : -1
index loc for message asdfasdf using \n\r : -1
That completely depends on how you're redisplaying it.
It sounds like that you're redisplaying it in HTML. "Raw" newlines are not part of HTML markup. Do a rightclick, View Page Source in webbrowser. You'll see linebreaks over all place. Usually before and/or after HTML tags.
In order to visually present linebreaks in the HTML presentation, you should actually be using <br> tags. You can replace newlines by <br> strings as below:
message = message.replace("\n", "<br>");
This is only sensitive to XSS attack holes if the message is an user-controlled variable, because you have to present it unescaped in JSP (i.e. without <c:out>) in order to get <br> to work. You thus need to make sure that the message variable is sanitized beforehand.
Alternatively, you can also set CSS white-space property there where you're redisplaying the message to pre. If you'd like to wrap lines inside the context of a block element, then set pre-wrap. Or if you'd like to collapse spaces and tabs as well, then set pre-line.
<div id="message"><c:out value="${message}" /></div>
#message {
white-space: pre-line;
}
This will display the text preformatted (as a textarea by default does).
Two possible problems:
The text in the textarea is word wrapped and doesn't really have any newlines.
The String you get with getParameter() contains newlines (\n) but no carriage returns (\r) as expected by many email programs.
As a first step, I'd try dumping the retrieved String in a way you can check for this. You could write to a file and use od or a hex editor to look at the file, for example.
If it turns out you're simply missing CRs, you could do some simple regexp-based replacement on the string to fix that.
Searching the ASCII codes i found that the new line is not defined like the often \n, instead is defined like \r\n.
Regards.
You need to encodeURIComponent() before submitting the form and decodeURIComponent() on the server side.
Related
I have a requirement where I receive a user input in the form string like:
"Hi there,\r\n\r\nThe information is not visible in the page since you’ve adjusted the app.\r\n\r\ncan you please fix it?\r\n\r\nThanks,\r\n\r\nXYZ"
While it looks like this in UI:
Hi there,
The information is not visible in the page since you’ve adjusted the app.
can you please fix it?
Thanks,
XYZ
I need to parse the above string with /r /n send it over mail to someone else.
Now, while I print this in console, it prints just like it looks in UI, quite obviously.
But, when I try to put this string as a java mail content, i simply get everything in a single line when I check the received email.
The content type for mail is set to text/html; charset=utf-8. I also tried pre-wrap styling but didn't work.
private static String createEmailContent(String descp) {
StringBuilder sb = new StringBuilder("<style> div { white-space: pre-wrap;}</style><div>");
sb.append(descp);
sb.append("/<div>");
return sb.toString();
}
How can I beautify the content to look exactly as received?
This is plain text content, just send it as text/plain instead of text/html.
If you really need to send it as text/html, you'll need to process the data and instead html line breaks <br> or wrap it all in <pre>.
I have a html content as a string.
String attachment = "<div style=\"color:black;font-style:normal;font-size:10pt;font-family:verdana;\"><div><span style=\"background-color: rgb(255,255,255);\">This is special "'; </span></div></div>";
If I try to add this as a multipart form data I get an exception. The reason happens to be the special characters inside the html which is " and '. So I tried escaping the entire string using
org.apache.commons.lang.StringEscapeUtils.escapeJave(attachment);
After doing this the exception disappeared and it was working fine. But the double quotes used for the attributes, like style are also escaped using this method, which is not desired.
Instead of <div> style="color:black;
it was sent as <div> style=\"color:black;
So far I realized that I need to escape only the text inside the html content and not the entire text. i could extract the text content using jsoup or something else then form the html again.
But is there a generic easy solution to do this?
I am trying to print a table having one column heading as - Assigned <BR> On. When I am printing this table heading in console, due to this BR tag, entire table is not getting printed in desired structure. Till it reaches Assigned, printed in one line, then from On it goes down to another line. So structure of the table is not in correct format. I am using Selenium WebDriver and my goal is to print this table with the same structure it is displaying in the web page. Not finding a way how to ignore this <BR> tag.
Check String regex replacement may help:
String testString = "abc/d <BR> ff</BR>";
testString = testString.replaceAll("<BR>|</BR>", "");
I have a text area and enter some content in this text-area I want to display the content on jsp page in the same format in which I have entered the content in text-area. So its like I entered the text in text-area then save it in mysql database then retrieve it from database and display it on Jsp page. I google it also but can't find the solution.
I am using Spring MVC with mysql database.
Enclose the text into a pre tag:
<pre><c:out value="${theTextFromDatabase}"/></pre>
The c:out JSTL tag is necessary to HTML-escape all the HTML special chars: < become <, > becomes >, & becomes &, etc.
i got the answer to remove the Textarea Problem,Now there is no need to add Tag in Textarea.
just append the value which coming from textarea.like this.i did it & it is working well.
String ques=request.getParameter("ques");
StringBuffer sb=new StringBuffer();
sb.append("<pre>"+ques+"</pre>");
String ss=sb.toString();
When you say in the same format you mean bold, italic, eol, and so on? The text is saved in your database with the format that you have introduced ? . I think that you need some kind of plugin wysiwyg ( what you see is what you get) like TinyMce (http://www.tinymce.com/) or save the correct format in your database and then process the text to format it according the text you received from your database.
I am trying to clean HTML text and to extract plain text from it using Jsoup. The HTML might contain non-english character.
For example the HTML text is:
String html = "<p>Á <a href='http://example.com/'><b>example</b></a> link.</p>";
Now if I use Jsoup#parse(String html):
String text = Jsoup.parse(html).text();
It is printing:
Á example link.
And if I clean the text using Jsoup#clean(String bodyHtml, Whitelist whitelist):
String text = Jsoup.clean(html, Whitelist.none());
It is printing:
Á example link.
My question is, how can I get the text
Á example link.
using Whitelist and clean() method? I want to use Whitelist since I might be needed to use Whitelist#addTags(String... tags).
Any information will be very helpful to me.
Thanks.
Not possible in current version (1.6.1), jsoup print Á as Á because the entity escaping feature, there is no "don't escape" mode now (check Entities.EscapeMode).
You can 1. unescape these HTML entities, 2. extend jsoup's source code by adding a new escape mode with an empty map.