📄 getdataservlet.java
字号:
// Fig. 5.03_01: GetDataServlet.java
// 处理客户端提交数据的 get 请求的 servlet
package com.fatcat.webchart;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class GetDataServlet 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>处理客户端提交数据的servlet</title>");
out.println("</head>");
// 生成文档的body部分
out.println("<body>");
// 获得正确编码的中文字符串
String name = convertTo8859(tempName);
if (name == null || name.equals(""))
{
name = "Java爱好者";
}
out.println("<h1>您好," + name + "<br>您提交的信息如下:<br>");
String level = convertTo8859(tempLevel);
out.println("您的Java编程水平为:<br>" + level + "</h1>");
out.println("</body>");
// 结束 XHTML 文档
out.println("</html>");
out.close(); // close stream to complete the page
}
// 字符内码转换
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 "";
}
}
}
/**************************************************************************
* (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 + -