Method to solve the missing format of JSP saved to database

  • 2021-07-22 10:56:45
  • OfStack

What is the storage of articles on the website? Using Oralce to store with CLOB, Java to operate CLOB, there are many methods on the Internet, but it is found that the format displayed on the web page after reading is completely different from that when entering, and some paragraphs have ceased to exist.

Because the page is using textarea tag rather than a powerful web page text editor, so to deal with the format of the problem, the following is a professional function I found, using it to pass the article string escaped 1 and then stored in the database, this time read out the article has a paragraph of the display.

JSP Save to Database Missing Format Solution Code Reference:


/**
 *  Format the article information and keep the article formatted. Used when saving. 
 * 
 * @param str
 * @return
 */
public String HTMLEncode(String str) {
  str = str.replace(">", ">");
  str = str.replace("<", "&lt;");
  char ch;
  ch = (char) 32; // space
  str = str.replace(String.valueOf(ch), "&nbsp;");
  ch = (char) 34; // ''
  str = str.replace(String.valueOf(ch), "&quot;");
  ch = (char) 39; // '
  str = str.replace(String.valueOf(ch), "&#39;");
  ch = (char) 13; // carriage return
  str = str.replace(String.valueOf(ch), "");
  ch = (char) 10; // new line
  str = str.replace(String.valueOf(ch), "<BR>");
  return str;
}

The above is the solution to save JSP to the database lost format shared with you. I hope it will be helpful for you to learn. You are welcome to communicate and discuss with each other if you have good ideas.


Related articles: