当前页面: 开发资料首页 → Java 专题 → servlet绘图入门:模拟投票统计的Servlet
摘要: servlet绘图入门:模拟投票统计的Servlet
一、web.xml文件,在这里配置servlet。
<?xml version="1.0" encoding="GB2312"?>
web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
二、投票页面html文件
<?xml version = "1.0"?>
html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<body>
<form action = "/Test/initVote" method = "get">
选择您最熟悉的编程语言
<input type="radio" name="program" value="0" />Python
<input type="radio" name="program" value="1" />Java
<input type="radio" name="program" value="2" />C#
<input type="radio" name="program" value="3" />Perl
<input type="radio" name="program" value="4" />PHP
<input name="submit" type = "submit" value = " 投 票 " />
如图:
三、servlet源程序
// Fig. 5.7: InitVoteServlet.java
// 获取Servlet 初始化参数并模拟投票统计的Servlet
package com.fatcat.webchart;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;
public class InitVoteServlet extends HttpServlet
{
// 初始化点击数
static int[]voteCounter =
{
0, 0, 0, 0, 0
};
// 获取初始化参数
public void init()throws ServletException
{
voteCounter[0] = Integer.parseInt(getInitParameter("Python"));
voteCounter[1] = Integer.parseInt(getInitParameter("Java"));
voteCounter[2] = Integer.parseInt(getInitParameter("CSharp"));
voteCounter[3] = Integer.parseInt(getInitParameter("Perl"));
voteCounter[4] = Integer.parseInt(getInitParameter("PHP"));
}
// 处理 HTTP get 请求
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// 清空缓冲区
response.reset();
// 注意这里的MIME类型
response.setContentType("image/png");
// 初始化总点击数
float totalCounter = 0;
// 获得客户提交的参数
String program = request.getParameter("program");
// 判断客户是否刷新页面并以此决定是否更新点击数
String accept = request.getHeader("Accept");
if (program != null && !accept.equals("*/*"))
{
int programID = Integer.parseInt(program);
voteCounter[programID]++;
}
// 计算点击数
for (int i = 0; i < voteCounter.length; i++)
{
totalCounter += voteCounter[i];
}
// 创建一个 600X330 的图像
int width = 600, height = 330;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到图形环境对象 g
Graphics g = image.getGraphics();
// 填充背景
g.setColor(Color.YELLOW);
g.fillRect(0, 0, width, height);
g.setColor(Color.white);
g.fillRect(11, 35, width - 21, height - 47);
g.setColor(Color.black);
g.drawRect(10, 35, width - 20, height - 46);
g.setColor(Color.black);
g.setFont(new Font("方正粗宋简体", Font.PLAIN, 25));
g.drawString("模拟投票统计的Servlet--水平直方图", 45, 25);
String programName[] =
{
"Python", "JAVA", "C#", "Perl", "PHP"
};
Color color[] = new Color[5];
color[0] = new Color(99, 99, 0);
color[1] = new Color(255, 169, 66);
color[2] = new Color(33, 255, 66);
color[3] = new Color(33, 0, 255);
color[4] = new Color(255, 0, 66);
g.setFont(new Font("黑体", Font.BOLD, 15));
g.drawString("编程语言", 25, 50);
g.drawString("所占比例", 520, 300);
g.setFont(new Font("SansSerif", Font.PLAIN, 12));
int salesValue = 0;
for (int i = 0; i < 440; i += 40)
{
g.setColor(Color.black);
g.drawString("" + salesValue + "%", (i + 80), 300);
g.setColor(Color.lightGray);
g.drawLine((i + 80), 65, (i + 80), 280);
salesValue += 10;
}
g.setColor(Color.black);
g.drawLine(80, 55, 80, 280);
g.drawLine(80, 280, 550, 280);
int drawWidth = 0;
for (int i = 0; i < programName.length; i++)
{
g.setColor(color[i]);
drawWidth = (int)(voteCounter[i] * 100 / totalCounter + 0.5) * 4;
g.fill3DRect(80, 78+i * 40, drawWidth, 25, true);
g.setColor(Color.black);
g.drawString("" + voteCounter[i] + " 票数 (" + (int)(voteCounter[i]
* 100 / totalCounter + 0.5) + "%)", 100+drawWidth, 95+i * 40);
g.drawString(programName[i], 40, 95+i * 40);
}
// 利用ImageIO类的write方法对图像进行编码
ServletOutputStream sos = response.getOutputStream();
ImageIO.write(image, "PNG", sos);
sos.close();
}
// 处理 HTTP post 请求, 和doGet一样
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}
}
/**************************************************************************
* (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. *
**************************************************************************/
运行图:
↑返回目录
前一篇: 生成验证码的Servlet
后一篇: 一个简单的投票程序