aspose file tools*
The moose likes JSP and the fly likes Database String in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Database String in jsp" Watch "Database String in jsp" New topic
Author

Database String in jsp

priya Anand
Greenhorn

Joined: Mar 30, 2009
Posts: 20
Hi,

I have saved the following String in Database and want to print it in a Text Area on my jsp.

The String is
I am here \n You are there \n I am not going any where.

If I hardcode the String, I get the three String each in new line.

Whereas the String from database prints the above string as is. I want to print the string in new line inside a text area.

The Stringtokenizer also does not work for this one.

Please Let me know, a way to acheive this.

Any help is greatly appreciated.

Thanks in advance
-Priya
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Priya,
Can you show the code you are using to create the textbox with the new lines? I think it is a
vs \n thing, but it's hard to tell without seeing the code.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi Priya, you can do it this way. Consider a sample code and the string you are retrieving from database is in first column:



Actually I executed this code. It is working. I am getting a new line every time. If this doesn't work try with the first one.



I created a table named abc which contains abcd\nefgh\nijkl and I tried to retrieve it. It came as I expected. The sentance was splitted into three different lines.



You can use any of the methods. I think there is a mistake in <textarea> tag. Once check it. I think the first one is a bad practice. You better check your tag and follow the third one.

Have a nice day.


Love all, trust a few, do wrong to none.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
priya!


html page does not know about "\n" character of java.

"\n" in java = <br> of html. try <br> tag. you will get what you need!

or


out.print("abcd <br> defg <br> hijk");

will return,
abcd
defg
hijk.


chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

yuvaraj yuvan wrote:priya!


html page does not know about "\n" character of java.

"\n" in java = <br> of html. try <br> tag. you will get what you need!

or


out.print("abcd <br> defg <br> hijk");

will return,
abcd
defg
hijk.



no it wont happen.



This will yield an output like this

abcd<br></br>efgh in the text area.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
don't close the <br> tag pa. its just <br>! thats it.
yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
<html>
<body>
<textarea> abcd <br> defg <br> </textarea>
</body>
</html>


abcd
defg
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

yuvaraj yuvan wrote:<html>
<body>
<textarea> abcd <br> defg <br> </textarea>
</body>
</html>


abcd
defg


Please once check the code. Execute it once. You will get abcd <br> defg <br> in the text area.

yuvaraj KumarAmudhan
Ranch Hand

Joined: Aug 22, 2009
Posts: 110
i am sorry! you are correct.


you may try this.
this should work.

<textarea> Test& #13; Newline gadsg</textarea>


thanks
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

yuvaraj yuvan wrote:i am sorry! you are correct.


you may try this.
this should work.

<textarea> Test& #13; Newline gadsg</textarea>


thanks


Its ok.
priya Anand
Greenhorn

Joined: Mar 30, 2009
Posts: 20
Hi Thanks for your replies.

But I am still not getting the thing right.

Anusha, how did you store your string in Database.

I will explain my Problem once again.

String strText = "Line one\nLine two\nLine three"

System.out.println("And the Original String is : "+strText);

In the logs I see this as :
Line one(box)Line two(box)Line three

I have the same string in the database saved it as varchar2.

I retrieve it and print it.

System.out.println("Index is at : "+rs.getString("VALUE"))

In the logs I see this as :
Line one\nLine two\nLine three

I tried to get the index of \n and I get back a -1.

The String retrieved from the Database has a different behaviour than the one I have hard coded.

Please let me know how can I retrieve this String as a normal String from the Database.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi Priya, it is not possible to give suggestions further until you paste a sample code. Please paste your code.
Sunder Ganapathy
Ranch Hand

Joined: Apr 01, 2003
Posts: 120
1. Using textarea does not give line-break , which you may try with a simple HTML and see.
2. Using \n also does not give line-break.
3. You need to use tag for this purpose.
4. Using table option does provide the line-break.
5. The column value used, for the database is, 'I am here \n You are there \n I am not going any where. '
6. The jsp code is as follows.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database String in jsp