0

How to add slashes to a particular string in JSP? I want to convert this PHP code $subj = addslashes($_POST['txtsubjct']); to JSP.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
david
  • 1
  • 1
  • 4
  • Note that writing raw Java code in JSP files is not the best practice. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files. A well designed JSP contains template/presentation logic only. – BalusC Feb 21 '11 at 11:40

2 Answers2

6

addslashes() is not particularly needed:

If you want to protect from sql-injections, use PreparedStatement

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0
public static String addSlashes(String s) {
    s = s.replaceAll("\\\\", "\\\\\\\\");
    s = s.replaceAll("\\n", "\\\\n");
    s = s.replaceAll("\\r", "\\\\r");
    s = s.replaceAll("\\00", "\\\\0");
    s = s.replaceAll("'", "\\\\'");
    return s;
}
Mohammed Saqib Rajput
  • 1,331
  • 1
  • 14
  • 22