How do I get \r and \n from Java string to be displayed in text area of JSP

I use a Prepared Statement insert to add data that is inputed by a user on my JSP to a mysql database. That works fine. I am able to add special characters including \n, \r, and single and double quotes.

My problem is when I try to retrieve the string values from the database (that have new line and carriage return characters within them) and try to load them into textareas in my JSP.

After first experiencing this problem I was told that I need to pretty much filter each string I want to display on my JSP where I would replace any instance of a \n or \r with a

tag. I have done this, AND STILL I DO NOT GET THE STRING TO DISPLAY IN MY TEXT AREA?

Please help if you can...quite URGENT!!!

[753 byte] By [XIKEE] at [2007-9-27 1:52:15]
# 1

Here is the script I have that assigns the string to the text area:

<script>

var strOne=<%= "(\"" + strField1 + "\")" %>;

document.Check.Field1.value=strOne;

</script>

If I do a System.out.println before I run this script, I get a string with the \r and \n replaced with

tags. The string looks fine if I look at my HTML source as well.

WHY IS IT NOT OUTPUTTING TO THE TEXTAREA WITH THE BREAKS? I am not getting anything displayed in the textarea?

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

If I use this script in my JSP that has a text area called Field1:

<script>

var strTEMP=("Hello

there");

document.Check.Field1.value=strTEMP;

</script>

I get output in my textarea of:

Hello

there

I want to get:

Hello

there

Why am I not getting this output?

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> var strTEMP=("Hello

there");

[snip]

> I get output in my textarea of:

>

> Hello

there

You are getting the

tags because you have sepecifcally placed them in the output stream. You are really dealing with two different types of strings.. You have an HTML string "Hello

there" and a plain old string "Hello\n\nthere". In your text box you will want to put the "Hello\n\nthere" and in your html you will want to put "Hello

there". The easiest solution is just to run a replace on the string to replace the

with \n or the \n to

depending on were you want the string to end up. Hope that helps...

ryan

ryanbarr at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

When I originally get the string from the database, it is in this form:

"Hello\n\nthere"

I created a replace method in my bean where I replace all occurances of \n with

. So I have tried both and there is still a problem. Could it be that maybe I have one too many steps here. Would it work if I just assigned the original string to the document.Check.Field1.value?

What do you think?

> > var strTEMP=("Hello

there");

> [snip]

> > I get output in my textarea of:

> >

> > Hello

there

>

> You are getting the

tags because you have

> sepecifcally placed them in the output stream. You

> are really dealing with two different types of

> strings.. You have an HTML string "Hello

there"

> and a plain old string "Hello\n\nthere". In your text

> box you will want to put the "Hello\n\nthere" and in

> your html you will want to put "Hello

there".

> The easiest solution is just to run a replace on the

> string to replace the

with \n or the \n to

> depending on were you want the string to end up.

>Hope that helps...

>

> ryan

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Try saving the following to ah HTML file and run it.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>Untitled</title>

</head>

<script language="JavaScript">

var someString="asdf\nasdf";

function replaceString()

{

document.test.txtA.value = someString;

}

</script>

<body>

<form name="test">

<textarea name="txtA" rows="5">

qwer

</textarea>

</form>

<a href="#" onClick="replaceString();">replace</a>

</body>

</html>

tus at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

If I use:

<script>

document.Check.Field1.value=<%= "(\"" + strField1 + "\")" %>;

</script>

Am I not assigning the "Hello\n\nthere" to the text area box? If I do a System.out.println(strField1); just before the script runs I get the "Hello there"?

PLEASE HELP?

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Another issue that I forgot about. You might have to go through the string you get from your database and replace the '\n' characters with '\\n' (or something like that) in your JSP file.

Take a look at the generated HTML to see if '\n' is in there. I have a feeling that when the JSP is run it will interpret the '\n' when it renders instead of putting in the literal characters and you'll get a JavaScript error.

i.e.

-

var somestring = "asdf \n asdf";

-

v.s.

-

var somestring = "asdf

asdf";

-

tus at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

I guess I'm kind of confused on what you are trying to do. If all you want is to have the JSP prepopulate a textarea and send it to the browser why don't you just use.

<textarea name="something">

<%=someString%>

</textarea>

Because you had the JavaScript I was assuming that you needed to change the contents of the filed on the fly. Then you would have to setup the strings as JavaScript variables and use JavaScript to set the value of the field on some event.

Can you please clarify exactly what you want to happen?

tus at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
The values that are being filled in when the page loads are coming from the DB.
XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

I have a JSP page that I want to populate with data that comes from my database when the page loads. I have both input boxes and text areas that I want to fill with the existing values.

I have the script runs that assigns the values onload.

This all works fine, except for when I have \n or \r in the text areas. The data in the db for the text areas represents the \n perfectly well.

My only problem here is getting those strings to be properly displayed in the text areas with the \n in the right places?

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

So why are you using JavaScript and trying to assign the value of a form field when you can do it through the structure of the JSP?

By the way how do you know when your assignment statement is evaluated? Consider the following

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>Untitled</title>

</head>

<script language="JavaScript">

var someString="asdf\nasdf";

document.test.txtA.value = someString;

</script>

<body>

<form name="test">

<textarea name="txtA" rows="5">

qwer

</textarea>

</form>

</body>

</html>

AND

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>Untitled</title>

</head>

<body>

<form name="test">

<textarea name="txtA" rows="5">

qwer

</textarea>

</form>

<script language="JavaScript">

var someString="asdf\nasdf";

document.test.txtA.value = someString;

</script>

</body>

</html>

tus at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

Below is some code that will do the replace:

public StringBuffer replaceString(String strFile,String strFindIn, String strReplaceIn)

{

StringBuffer sbFile = new StringBuffer();

int nAmpIndex = 0;

int nStartIndex = 0;

try

{

int index = strFile.indexOf(strFindIn, nStartIndex);

while (index!=-1)

{

nAmpIndex++;

sbFile.append(strFile.substring(nStartIndex, index));

sbFile.append(strReplaceIn);

nStartIndex = index+strFindIn.length();

index = strFile.indexOf(strFindIn, nStartIndex);

}

sbFile.append(strFile.substring(nStartIndex,

strFile.length()));

}

catch(Exception e)

{

System.err.println("WMLParser::replaceString() - " + e);

}

return sbFile;

}

Here is the line I use in my JSP to do the replace:

String strField1 = bean_name.replaceString(strField1,"\n","\\n").toString();

THIS STILL DOESN'T WORK!!!

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Can you post your JSP file?
tus at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

If I have a Java Bean method return a string to me in my JSP, like:

String strName=bean_name.method_name();

If I now want to take strName and assign it to the value of a textarea I have on the same JSP, how can I do this? As I have mentioned before, it works fine as long as I do not have special characters. As soon as strName has \n within it - there are problems.

e.g.

strName="

Hello

there"

XIKEE at 2007-7-4 20:25:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
Please help?
XIKEE at 2007-7-4 20:26:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
I think I have narrowed it down to a problem of making sure that all \n are replaced with \\nCould someone please show me some code to do this replace of new line characters with "\\n" Thank you!
XIKEE at 2007-7-4 20:26:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17

Sorry about the delay but I left work and went home.

The reason I want to see your source JSP is because I believe you are trying to have the browser do things that should be done by the server (even though it is probably possible to do on the browser side).

The impression I get is you are trying to set the value of the textarea on the client side by having the JSP generate the form objects and leaving them empty. The JSP is also generating JavaScript variables to pass to the browser. You then have some JavaScript logic to set values of the form fields with the generated JavaScript variables that the JSP page passes along to the browser. Finally all this stuff is sent to the browser so it can execute the JavaScript to set the form values.

Because everything you need generate the page and its layout is available on the server (it has to be since you are passing it all along to the browser) I see no reason to have the browser do any work. If I'm wrong here and you have some good reason please let me know.

Take a look at the code I have attached here. Save it as a JSP and run it. If you replace the string litteral I am using to set someString with the variable that you are using to store the results from your database query you should not have to do any character replacements.

There are also ways to prepopulate the check boxes with server side code instead of having JavaScript do it but I would really like to see you fix the textarea problem first.

-

<html>

<head>

<title>Untitled</title>

</head>

<%

String someString = "something \n something else";

%>

<body>

<textarea name="something">

<%=someString%>

tus at 2007-7-4 20:26:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 18

don't know why it happened but some lines got cut off of the file

-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title>Untitled</title>

</head>

<%

String someString = "something \n something else";

%>

<body>

<textarea name="something">

<%=someString%>

</textarea>

</body>

</html>

tus at 2007-7-4 20:26:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 19

> After first experiencing this problem I was told that

> I need to pretty much filter each string I want to

> display on my JSP where I would replace any instance

> of a \n or \r with a

tag.

Scan the String before display and replace each \ with \\.

MartinS. at 2007-7-4 20:26:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 20

I am now close to solving this problem. I have a new method to filter each string I want to display in the textarea:

public String convertFormContent (String field)

{

StringstrNewString= "";

StringTokenizer st = new StringTokenizer(field);

while (st.hasMoreTokens())

{

strNewString= strNewString + (st.nextToken()) + "\\n";

}

return (strNewString);

}

The only problem with this is that it replaces just normal spaces as well with '\\n' when I need it to only replace '\n'. I tried changing the fourth line to:

StringTokenizer st = new StringTokenizer(field, "\n");

This causes nothing to be displayed in the text area at all

Please help?

I really appreciate all your help so far with this!!!!!

XIKEE at 2007-7-4 20:26:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 21
I fixed it by doing a "\t\r\n" instead of just "\n"...the only difference from the default is the space....Thank you all again so much!!!!
XIKEE at 2007-7-4 20:26:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 22
Could you explain how to solve this? I saw so many replys..however I still don't know how to solve this....
ucftcys at 2007-7-4 20:26:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...