Replace multiple backslashes with HTML entities in Java string for JSP generated Json
after hours of googeling and searching within SO, I finaly come to the place where I need to ask you! :)

Situation is the following:
A webservice delivers data in a CDATA. This data is parsed and put into our model. Using Spring MVC we access the model inside the JSP files to create....here come the point... JSON! Don't ask, historically! ;-)

Now, somehow someone came to the glorious idea to put multiple (back)slashes into a title property. The getTitle() method returns the string "/// Glasvegas \\\". This of course doesn't work, if we do a JavaSCript eval() on the JSON (created within the JSP) to get the JavaScript Json object. It simply interprets the backslashes as comment, making the Json invalid.
I tried to use the escapeHtml() methods from apache.common and springframework, but they both just ignore the backslashes while encoding all other special chars correctly.

Then I tried to write my own method:

    public static String escapeHTML(String string) {    	 
        String foreslash="&#92;";
        String regex="\\\\";
        System.out.println(string.replaceAll(regex,foreslash));
        string.replaceAll(regex,foreslash);
        return string;
    }

In console output the string is correctly replaced, but if break at the return and inspect the variable 'string' in the debugger it's still "/// Glasvegas \\\". The same also, in the generated JSP. 

So, I'm kind of lost here.

Regards,
ASP
Replace multiple backslashes with HTML entities in Java string for JSP generated Json
===

after hours of googeling and searching within SO, I finaly come to the place where I need to ask you! :)

Situation is the following:
A webservice delivers data in a CDATA. This data is parsed and put into our model. Using Spring MVC we access the model inside the JSP files to create....here come the point... JSON! Don't ask, historically! ;-)

Now, somehow someone came to the glorious idea to put multiple (back)slashes into a title property. The getTitle() method returns the string "/// Glasvegas \\\". This of course doesn't work, if we do a JavaSCript eval() on the JSON (created within the JSP) to get the JavaScript Json object. It simply interprets the backslashes as comment, making the Json invalid.
I tried to use the escapeHtml() methods from apache.common and springframework, but they both just ignore the backslashes while encoding all other special chars correctly.

Then I tried to write my own method:

    public static String escapeHTML(String string) {    	 
        String foreslash="&#92;";
        String regex="\\\\";
        System.out.println(string.replaceAll(regex,foreslash));
        string.replaceAll(regex,foreslash);
        return string;
    }

In console output the string is correctly replaced, but if break at the return and inspect the variable 'string' in the debugger it's still "/// Glasvegas \\\". The same also, in the generated JSP. 

So, I'm kind of lost here.

Regards,
ASP