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 蠻哥♂楓 閱讀(272) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 桐乡市| 兴和县| 克什克腾旗| 富平县| 双柏县| 绥棱县| 保定市| 三亚市| 突泉县| 茌平县| 马龙县| 杭锦后旗| 丰都县| 丹凤县| 北辰区| 杨浦区| 夹江县| 永宁县| 秦皇岛市| 永年县| 胶南市| 北安市| 固安县| 且末县| 黄梅县| 和顺县| 沙坪坝区| 沂南县| 大港区| 高安市| 乌兰浩特市| 阿坝县| 绥宁县| 天津市| 郓城县| 开阳县| 屯留县| 梨树县| 淅川县| 宜良县| 萨嘎县|