java实现输入汉字自动显示汉字的首拼音字母
2011-06-11 21:26
429 查看
1.首先去官网下载pinyin4j-2.5.0.jar
,放到eclipse的classpath下
2.新建一个hanZiToPinYin.jsp页面,代码如下
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hanZiToPinYin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var xmlHttp;
function toPinYin(){
var hanzi=document.getElementById("hanzi").value;
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
if(null!=xmlHttp){
xmlHttp.open("GET","ToPinYinServlet?hanzi="+hanzi,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
}
function callback(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var responseText = xmlHttp.responseText;
document.getElementById("pinyin").value = responseText;
}
}
}
</script>
</head>
<body>
<form>
输入汉字:<input type="text" name="hanzi" id="hanzi" onblur="toPinYin();"></br>
拼音为:<input type="text" name="pinyin" id="pinyin"/>
</form>
</body>
</html>
3.新建一个ToPinYinServlet.java文件,代码如下
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class ToPinYinServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String hanzi = request.getParameter("hanzi");
String hanzhis = new String(hanzi.getBytes("iso-8859-1"),"gbk");
CharSequence s= hanzhis;
char [] hanzhi=new char[s.length()];
for(int i=0;i<s.length();i++){
hanzhi[i]=s.charAt(i);
}
char [] t1 =hanzhi;
String[] t2 = new String[s.length()];
/**
* 设置输出格式
*/
net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
int t0=t1.length;
String py = "";
try {
for (int i=0;i<t0;i++){
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
py=py+t2[0].charAt(0);//获取首字母
}
}
catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
out.write(py.trim().toString());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ToPinYinServlet</servlet-name>
<servlet-class>ToPinYinServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ToPinYinServlet</servlet-name>
<url-pattern>/ToPinYinServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.部署web项目,在浏览器中输入url
,放到eclipse的classpath下
2.新建一个hanZiToPinYin.jsp页面,代码如下
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'hanZiToPinYin.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var xmlHttp;
function toPinYin(){
var hanzi=document.getElementById("hanzi").value;
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
if(null!=xmlHttp){
xmlHttp.open("GET","ToPinYinServlet?hanzi="+hanzi,true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
}
function callback(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
var responseText = xmlHttp.responseText;
document.getElementById("pinyin").value = responseText;
}
}
}
</script>
</head>
<body>
<form>
输入汉字:<input type="text" name="hanzi" id="hanzi" onblur="toPinYin();"></br>
拼音为:<input type="text" name="pinyin" id="pinyin"/>
</form>
</body>
</html>
3.新建一个ToPinYinServlet.java文件,代码如下
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class ToPinYinServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
String hanzi = request.getParameter("hanzi");
String hanzhis = new String(hanzi.getBytes("iso-8859-1"),"gbk");
CharSequence s= hanzhis;
char [] hanzhi=new char[s.length()];
for(int i=0;i<s.length();i++){
hanzhi[i]=s.charAt(i);
}
char [] t1 =hanzhi;
String[] t2 = new String[s.length()];
/**
* 设置输出格式
*/
net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
int t0=t1.length;
String py = "";
try {
for (int i=0;i<t0;i++){
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
py=py+t2[0].charAt(0);//获取首字母
}
}
catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
out.write(py.trim().toString());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>ToPinYinServlet</servlet-name>
<servlet-class>ToPinYinServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ToPinYinServlet</servlet-name>
<url-pattern>/ToPinYinServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5.部署web项目,在浏览器中输入url
相关文章推荐
- JAVA实现 输入一串数字,转换成汉字显示
- JAVA实现 输入一串数字,转换成汉字显示
- 使用java简单实现根据输入的汉字,得到汉字的首字母
- Silverlight combobox,实现输入拼音显示对应的所有的汉字
- 密码框失去焦点且为空时显示汉字“密码”、获取焦点时输入内容显示为密码“**********”的实现方法
- Java实现QQ窗口自动输入
- 实现输入【汉字】自动识别出对应的【拼音】
- java实现汉字验证码,包含验证输入。
- JAVA输入一串数字,转换成汉字显示
- Java:Poperties Editor 插件------自动解决ApplicationResources.properties中汉字显示乱码问题
- 输入汉字自动转为拼音(jsp实现方式)
- java技术:输入拼音自动提示汉字的方法
- Java实现QQ窗口自动输入
- java中swing实现TextArea显示内容满时自动滚动到末尾
- Java实现QQ窗口自动输入
- 怎么样在PB的数据窗口中一输入一个汉字就自动的取出它的拼音字母的第一个
- jQuery实现文本域内提示文字,当鼠标点击进文本域后自动消失,文本域失去鼠标焦点后显示提示文字或显示用户输入的文字。
- 重写java中JButton,实现图片下显示文字(根据图片宽度文字自动换行)
- java输入联想,支持汉字自动匹配
- Android在AutoCompleteTextView中输入字母或者汉字通过Filterable实现自动筛选提醒