Solved

JSP & MySQL :make paragraph when enter into db?

Posted on 2003-11-07
14
899 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
yup gave me thw error like above
0
 
LVL 1

Author Comment

by:yapsing
Comment Utility
ok ok, got it got it
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:yapsing
Comment Utility
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
Comment Utility
thanx anyway...
0
 
LVL 14

Expert Comment

by:kennethxu
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ahh... all is clear now...

thanks
0
 
LVL 14

Expert Comment

by:kennethxu
Comment Utility
glad that I can help :-)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to write service layer in Spring ? 4 273
servlet init param and contect param 3 53
squirrelPlay java challenge 40 157
equalIsNot  challenge 43 115
This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

9 Experts available now in Live!

Get 1:1 Help Now