Need to escape Special Characters in Java Web Application

listed in answer

Need to escape Special Characters in Java Web Application
0 votes, 0.00 avg. rating (0% score)

ANSWER:

you can pattern match the string and either build a black list of invalid characters or have a white list of valid characters….something like the following

 Pattern p = Pattern.compile(blackList); // or reverse with a white list
 Matcher m = p.matcher(unsafeInputString);
 if (m.matches())

    // Invalid input: reject it, or remove/change the offending characters.

else

   // Valid input.

by nate_weldon from http://stackoverflow.com/questions/10317029