java实现输入汉字自动显示汉字的首拼音字母

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值