当前位置: 首页 > 图文教程 > 网络编程 > JSP > jsp中任意文字转Unicode的通用模块

JSP
JSP的login程序代码
编写线程安全的JSP程序
通过JSP的预编译消除性能瓶颈
JSP学习心得
使用JSP开发WebMail系统
在jsp中用bean和servlet联合实现用户注册、登录
在Linux环境下安装JSP
jsp页面调用applet实现人民币的大小写转换
JSP+XML构架网站的实例
用缓冲技术提高JSP应用的性能和稳定性
配置Web应用环境实现JSP留言簿
用JSP创建可重用的图形背景
轻松使用JSP生成饼图
Jdbc连Sybase数据库的几种方法
用定制标签库和配置文件实现对JSP页面元素的访问控制
Jsp结合XML+XSLT将输出转换为Html格式
用JSP操作Cookie
安装resin+mysql+IIS+JDK的总结
JSP的运行内幕
指南:想成为一个JSP网站程序员吗?

JSP 中的 jsp中任意文字转Unicode的通用模块


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-02   浏览: 76 ::
收藏到网摘: n/a

  /** ToUnicode.java */
package com.edgewww.util;

import java.io.*;

/**
* 字符串转换成Unicode码的类
* @author 栾金奎 [email protected]
* @date 2001-03-05
*/
public class ToUnicode {

/**
* 把字符串转换成Unicode码
* @param strText 待转换的字符串
* @param code 转换前字符串的编码,如"GBK"
* @return 转换后的Unicode码字符串
*/
public String toUnicode(String strText,String code) throws UnsupportedEncodingException{
  char c;
  String strRet = "" ;
  int intAsc;
  String strHex;
  strText = new String(strText.getBytes("8859_1"),code);
  for ( int i = 0; i < strText.length(); i++ ){
    c = strText.charAt(i);
    intAsc = (int)c;
    if(intAsc>128){
      strHex = Integer.toHexString(intAsc);
      strRet = strRet + "&#x" + strHex+";";
    }
    else{
      strRet = strRet + c;
    }
  }
  return strRet ;
}

}

/** 应用举例 */
/** gbk2Unicode.jsp */
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<jsp:useBean id="g2u" scope="session" class="com.edgewww.util.ToUnicode"/>
<% String lang = "这是简体中文"; %>
<br>
<%=lang %>
<br>
<%=g2u.toUnicode(lang,"GBK") %>