posts - 4,comments - 30,trackbacks - 0

          public class ToHtml {
          ?/** HTMLエンコードが必要な文字 **/
          ?static char[] htmlEncChar = {'&', '"', '<', '>'};
          ?/** HTMLエンコードした文字列 **/
          ?static String[] htmlEncStr = {"&amp;", "&quot;", "&lt;", "&gt;"};

          ?/**
          ? * HTMLエンコード処理。
          ? *?? &,",<,>の置換
          ?**/
          ?
          ?public static String encode (String strIn) {
          ??if (strIn == null) {
          ???return(null);
          ???? }

          ???? // HTMLエンコード処理
          ???? StringBuffer strOut = new StringBuffer(strIn);
          ???? // エンコードが必要な文字を順番に処理
          ???? for (int i = 0; i < htmlEncChar.length; i++) {
          ???? ?// エンコードが必要な文字の検索
          ???? ?int idx = strOut.toString().indexOf(htmlEncChar[i]);

          ???? ?while (idx != -1) {
          ???? ??// エンコードが必要な文字の置換
          ???? ??strOut.setCharAt(idx, htmlEncStr[i].charAt(0));
          ???? ??strOut.insert(idx + 1, htmlEncStr[i].substring(1));

          ???? ??// 次のエンコードが必要な文字の検索
          ???? ??idx = idx + htmlEncStr[i].length();
          ???? ??idx = strOut.toString().indexOf(htmlEncChar[i], idx);
          ???? ?}
          ???? }
          ???? return(strOut.toString());
          ?}
          }



          <%@ page contentType="text/html; charset=Shift_JIS" %>
          <%@ page import="renesas.common.util.*" %>

          <html>
          ?<head>
          ??<title>HTMLエンコードのテスト</title>
          ?</head>
          <body>

          <h1>HTMLエンコードのテスト</h1>

          <form>
          <table>
          ?<tr>
          ??<td> & の付いたデータ</td>
          ??<td><input type="text" value="<%= ToHtml.encode("joe&joe") %>" /></td>
          ?</tr>
          ?<tr>
          ??<td> <> の付いたデータ</td>
          ??<td><input type="text" value="<%= ToHtml.encode("<joe>") %>" /></td>
          ?</tr>
          ?<tr>
          ??<td> " の付いたデータ</td>
          ??<td><input type="text" value="<%= ToHtml.encode("\"joe\"") %>" /></td>
          ?</tr>
          </table>
          </form>

          </body>
          </html>


          posted on 2007-08-10 11:28 蠻哥♂楓 閱讀(269) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 威宁| 利川市| 买车| 大连市| 堆龙德庆县| 呈贡县| 晋江市| 罗甸县| 乌鲁木齐县| 庆云县| 平顶山市| 漳浦县| 澎湖县| 右玉县| 那曲县| 南康市| 遵义市| 长岭县| 神池县| 大冶市| 渝北区| 北票市| 青岛市| 福海县| 福州市| 盐源县| 浦江县| 武鸣县| 大荔县| 昔阳县| 洪洞县| 佛学| 砀山县| 汝州市| 景洪市| 嘉鱼县| 高安市| 花莲县| 绥阳县| 鱼台县| 灯塔市|