A method to solve the missing format of JSP saved to database

Time:2020-5-31

What is the storage of articles on the website? There are many ways to use Oracle to store CLOB and Java to operate CLOB on the Internet. However, it is found that the format displayed on the web page after reading is totally different from that when entering, and some paragraphs no longer exist.

Because the page uses a textarea tag instead of a powerful web page text editor, so I have to deal with the format problem by myself. Here is a professional function I found. Use it to escape the article string passed from the foreground and then save it into the database. The article read out this time will have a paragraph display.

Solution code reference for JSP saving to database missing format:

/**
 *Format the article information and keep the format of the article. For saving.
 * 
 * @param str
 * @return
 */
public String HTMLEncode(String str) {
  str = str.replace(">", ">");
  str = str.replace("<", "<");
  char ch;
  ch = (char) 32; // space
  str = str.replace(String.valueOf(ch), " ");
  ch = (char) 34; // ''
  str = str.replace(String.valueOf(ch), """);
  ch = (char) 39; // '
  str = str.replace(String.valueOf(ch), "'");
  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 the JSP to the database lost format shared with you. I hope it will be helpful for your study. Welcome to communicate and discuss with you if you have good ideas.