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) 評(píng)論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 恩平市| 衡阳市| 宝鸡市| 和林格尔县| 汉寿县| 万源市| 望谟县| 仁布县| 云安县| 日照市| 怀安县| 玉树县| 城口县| 通化县| 芦山县| 福建省| 申扎县| 民权县| 长子县| 沐川县| 平定县| 仙游县| 河源市| 福泉市| 威宁| 车险| 黄骅市| 辛集市| 交城县| 敖汉旗| 渭源县| 阜康市| 比如县| 香格里拉县| 中卫市| 英德市| 安溪县| 陈巴尔虎旗| 若羌县| 广东省| 浙江省|