Solved

JSP & MySQL :make paragraph when enter into db?

Posted on 2003-11-07
14
906 Views
Last Modified: 2008-02-01
hi guys,

I have a news column for company's sports club. i need to know how to make new paragraph everytime a user press enter like what we see here. the body text is save as a BLOB field.
0
Comment
Question by:yapsing
  • 8
  • 5
14 Comments
 
LVL 3

Expert Comment

by:applekanna
ID: 9706108
para1 -> If this is

para2 -> is what you are talking about

I think the \n sequence will automatically be stored in the databse itself and we need do anything specific when displaying it.

You can also parse the text if you want and then insert <p> before dispalying it ....


0
 
LVL 14

Accepted Solution

by:
kennethxu earned 50 total points
ID: 9708769
try out this jsp page and you'll have a better idea of how to handle those issues:
====================
<%!
   public static String text2html(String s, boolean preformat)
   {
       StringBuffer buf = new StringBuffer();
       for (int i=0, limit=s.length(); i<limit; i++) {
           char c = s.charAt(i);
           switch( c ) {
               case '<': buf.append( "&lt;" ); break;
               case '>': buf.append( "&gt;" ); break;
               case '&': buf.append( "&amp;" ); break;
               case '\'': buf.append( "&#39;" ); break;
               case '"': buf.append( "&quot;" ); break;
               case ' ': if(preformat && s.charAt(i+1) == ' ') buf.append( "&nbsp;" ); else buf.append( c ); break;
               case '\n': if(preformat) buf.append( "<br>" ); else buf.append( c ); break;
               default: buf.append( c );
           }
      }
      return buf.toString();
   }
%>
<% String xxx="my+String That<Contains>\"Quotes'"; %>
<% String yyy="my+String That<Contains>\"Quotes'\nand      CRLF, with a\n         long line like this aaaaaaaaa bbbbbbbbbbbb ccccccccccccc ddddddddddddddd eeeeeeeeeeeeeee ffffffffffff gggggggggggggggg hhhhhhhhhhhhhhh wwwwwwwwwwwwwwwww"; %>
<FORM NAME=abc>
<h2>Use java.net.URLEncode and javascrpt:unescape():</h2>
<INPUT TYPE="TEXT" SIZE=50 NAME="text1" VALUE=""><br>
<TEXTAREA COLS=50 ROWS=4 NAME="textarea1"></TEXTAREA><br>

<h2>Use text2html()</h2>
<INPUT TYPE="TEXT" SIZE=50 NAME="text2" VALUE="<%=text2html(xxx, false)%>"><br>
<TEXTAREA COLS=50 ROWS=4 NAME="textarea2"><%=text2html(yyy, false)%></TEXTAREA><p>

<h3>preformat=false:</h3>
<%=text2html(yyy, false)%><p>

<h3>preformat=true:</h3>
<code><%=text2html(yyy, true)%></code><br>

<h3>preformat=false in &lt;pre&gt; tag:</h3>
<pre><%=text2html(yyy, false)%></pre>
</FORM>

<script>
      abc.text1.value=unescape('<%=java.net.URLEncoder.encode(xxx).replace( '+', ' ')%>' );
      abc.textarea1.value=unescape('<%=java.net.URLEncoder.encode(yyy).replace( '+', ' ')%>' );
</script>
0
 
LVL 1

Author Comment

by:yapsing
ID: 9719330
forgive me of my slowness kennethxu, can u specifically tell me how to place the coding in the proper place?
0
 
LVL 1

Author Comment

by:yapsing
ID: 9719390
An error occurred at line: -1 in the jsp file: null

Generated servlet error:
    [javac] Since fork is true, ignoring compiler setting.
    [javac] Compiling 1 source file
    [javac] Since fork is true, ignoring compiler setting.
    [javac] C:\Tomcat\work\Standalone\localhost\_\try_jsp.java:46: illegal start of expression
    [javac]    public static String text2html(String s, boolean preformat)
    [javac]    ^
    [javac] C:\Tomcat\work\Standalone\localhost\_\try_jsp.java:89: cannot resolve symbol
    [javac] symbol  : method text2html (java.lang.String,boolean)
    [javac] location: class org.apache.jsp.try_jsp
    [javac]       out.print(text2html(xxx, false));
    [javac]                 ^
    [javac] C:\Tomcat\work\Standalone\localhost\_\try_jsp.java:93: cannot resolve symbol
    [javac] symbol  : method text2html (java.lang.String,boolean)
    [javac] location: class org.apache.jsp.try_jsp
    [javac]       out.print(text2html(yyy, false));
    [javac]                 ^
    [javac] C:\Tomcat\work\Standalone\localhost\_\try_jsp.java:98: cannot resolve symbol
    [javac] symbol  : method text2html (java.lang.String,boolean)
    [javac] location: class org.apache.jsp.try_jsp
    [javac]       out.print(text2html(yyy, false));
    [javac]                 ^
    [javac] C:\Tomcat\work\Standalone\localhost\_\try_jsp.java:103: cannot resolve symbol
    [javac] symbol  : method text2html (java.lang.String,boolean)
    [javac] location: class org.apache.jsp.try_jsp
    [javac]       out.print(text2html(yyy, true));
    [javac]                 ^
    [javac] C:\Tomcat\work\Standalone\localhost\_\try_jsp.java:109: cannot resolve symbol
    [javac] symbol  : method text2html (java.lang.String,boolean)
    [javac] location: class org.apache.jsp.try_jsp
    [javac]       out.print(text2html(yyy, false));
    [javac]                 ^
    [javac] 6 errors
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9719392
did you try out the code?
this is jsp code to give you an idea of how to display new line and other special characters.

if you couldn't understand, then post whatever you have.
0
 
LVL 1

Author Comment

by:yapsing
ID: 9719420
yup gave me thw error like above
0
 
LVL 1

Author Comment

by:yapsing
ID: 9719438
ok ok, got it got it
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:yapsing
ID: 9719513
ok, my explanation is, everytime i retrieve or get a text field, the cases will check every character in the text i sent, correct?

i can replace:
this
<% String xxx="my+String That<Contains>\"Quotes'"; %>
<% String yyy="my+String That<Contains>\"Quotes'\nand      CRLF, with a\n         long line like this aaaaaaaaa bbbbbbbbbbbb ccccccccccccc ddddddddddddddd eeeeeeeeeeeeeee ffffffffffff gggggggggggggggg hhhhhhhhhhhhhhh wwwwwwwwwwwwwwwww"; %>

with this:
<% String blb = request.getParameter("strng")%>
0
 
LVL 1

Author Comment

by:yapsing
ID: 9719610
thanx anyway...
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9719719
yes, you can use it before you save to database, or after you retrieve from database right before display to html page. I would suggest later approach is better.
0
 
LVL 1

Author Comment

by:yapsing
ID: 9720202
how would the db know its a next line break when we save it?
can u explain or show me a page where i can do reference?
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9723603
db doesn't have to know what you are saving to it's clob/blob column, it will just save whatever you give to it and return the same thing when you retrieve from it.

For example, The text I'm typing here will be submitted to web server, the web server will store this text in database as is.  when it needs to be displayed, we read it from database and call the text2html method, then display it on html page.

unless you have special requirement that you must cut the string and store them in different rows.

let me know.
0
 
LVL 1

Author Comment

by:yapsing
ID: 9727616
ahh... all is clear now...

thanks
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 9728373
glad that I can help :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jason format for rest webservices and sub resourcing 1 88
Fisheye tool 2 104
Getting NoSuchMethodError on accessing a spring mvc project 3 158
java operators 3 110
What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now