JSP에서 동등한 htmlentities?
-
11-07-2019 - |
문제
저는 PHP 사람이지만 JSP에서 작은 프로젝트를해야합니다. JSP에서 Htmlentities 함수 (PHP)와 동등한 지 궁금합니다.
해결책
public static String stringToHTMLString(String string) {
StringBuffer sb = new StringBuffer(string.length());
// true if last char was blank
boolean lastWasBlankChar = false;
int len = string.length();
char c;
for (int i = 0; i < len; i++)
{
c = string.charAt(i);
if (c == ' ') {
// blank gets extra work,
// this solves the problem you get if you replace all
// blanks with , if you do that you loss
// word breaking
if (lastWasBlankChar) {
lastWasBlankChar = false;
sb.append(" ");
}
else {
lastWasBlankChar = true;
sb.append(' ');
}
}
else {
lastWasBlankChar = false;
//
// HTML Special Chars
if (c == '"')
sb.append(""");
else if (c == '&')
sb.append("&");
else if (c == '<')
sb.append("<");
else if (c == '>')
sb.append(">");
else if (c == '\n')
// Handle Newline
sb.append("<br/>");
else {
int ci = 0xffff & c;
if (ci < 160 )
// nothing special only 7 Bit
sb.append(c);
else {
// Not 7 Bit use the unicode system
sb.append("&#");
sb.append(new Integer(ci).toString());
sb.append(';');
}
}
}
}
return sb.toString();
}
다른 팁
public static String StringToHtMlString (String String) {...
똑같은 일이 유용합니다 커먼즈 라인 도서관:
org.apache.commons.lang.StringEscapeUtils.escapeHtml
사용자 정의 TLD로 내보내면 JSP에 대한 편리한 방법이 얻을 수 있습니다.
JSP에서 직접 JSTL의 True 속성으로 EscapeXML을 사용하는 것이 좋습니다.
<c:out value="${string}" escapeXml="true" />
제휴하지 않습니다 StackOverflow