Solved

JSP & MySQL :make paragraph when enter into db?

Posted on 2003-11-07
14
921 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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