Solved

JSP & MySQL :make paragraph when enter into db?

Posted on 2003-11-07
14
915 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hashcode is calculated on the basis of value object 2 110
java cyclic graphs 15 209
IE doesn't Invoke servlet in iframe 1 111
jsp insert to database example 2 24
An article on effective troubleshooting
Adding Computers to AD groups through an SCCM Task Sequence
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

778 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