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" οnblur="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