⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getcharcodeservlet.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 5.03_02: GetCharCodeServlet.java
// 处理客户端提交数据的 get	请求的 servlet, 获取提交字符的Unicode码值

package com.fatcat.webchart;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class GetCharCodeServlet extends HttpServlet
{

  //  处理客户端提交数据的 "get" 请求
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
                       throws ServletException, IOException
  {

    // 发送 XHTML 格式的页面给客户端
    response.setContentType("text/html;charset=gb2312");

    String tempName = request.getParameter("name");
    String tempLevel = request.getParameter("level");

    PrintWriter out = response.getWriter();

    // 开始生成 XHTML 文档
    out.println("<?xml version = \"1.0\"?>");

	out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 
		        "XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 
		        "/TR/xhtml1/DTD/xhtml1-strict.dtd\">");

    out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">");

    // 生成文档的head部分
    out.println("<head>");
    out.println("<title>处理客户端提交数据字符的Unicode码值servlet</title>");
    out.println("</head>");

    // 生成文档的body部分
    out.println("<body>");

    // 直接获取并输出参数值
    out.println("<h1>编码前:<br>");
    out.println("字符串\"" + tempName + "\"的Unicode编码为:&nbsp;");
    out.println(getCharCode(tempName) + "<br>");
    out.println("字符串\"" + tempLevel + "\"的Unicode编码为:&nbsp;<br>");
    out.println(getCharCode(tempLevel));
    out.println("</h1><br>");

    // 重新编码后	
    // 获得正确编码的中文字符串
    String name = convertTo8859(tempName);
    String level = convertTo8859(tempLevel);

    if (name == null || name.equals(""))
    {
      name = "Java爱好者";
    }
    out.println("<h1>编码后:<br>");
    out.println("字符串\"" + name + "\"的Unicode编码为:&nbsp;");
    out.println(getCharCode(name) + "<br>");
    out.println("字符串\"" + level + "\"的Unicode编码为:&nbsp;<br>");
    out.println(getCharCode(level));
    out.println("</h1><br>");


    out.println("</body>");

    // 结束 XHTML 文档
    out.println("</html>");
    out.close(); // 关闭输出流
  }

  // 字符内码转换
  public String convertTo8859(String strInput)
  {
    try
    {
      String tempStr = strInput;
      byte[] tempStrBytes = tempStr.getBytes("ISO8859-1");
      String strOutput = new String(tempStrBytes);
      return strOutput;
    }
    catch (Exception e)
    {
      return "";
    }
  }

  // 输出字符的Unicode编码
  public String getCharCode(String str)
  {
    String temp = "";
    for (int i = 0; i < str.length(); i++)
    {
      temp += Integer.toHexString((int)str.charAt(i)) + "&nbsp;";
    }
    return temp;
  }
}

/**************************************************************************
 * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓).  *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors of this code have used their                   *
 * best efforts in preparing the code. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these codes. The authors *
 * shall not be liable in any event for incidental or consequential       *
 * damages in connection with, or arising out of, the furnishing,         *
 * performance, or use of these programs.                                 *
 **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -