servlet绘图入门:模拟投票统计的Servlet,servlet绘,ervlet绘图,rvlet绘图入,vlet绘图入门,let绘图入门,et绘图入门模,t绘图入门模拟,绘图入门模拟投,图入门模拟投票,入门模拟投票统,门模拟投票统计,模拟投票统计的,模拟投票统计的S,拟投票统计的Se,投票统计的Ser,票统计的Serv,统计的Servl,计的Servle servlet绘图入门:模拟投票统计的Servlet----编程资料集中营--八角123--bajiao123.com
编程资料集中营
 | 网站首页 | 文章中心 | 编程资料2 | 软件下载 | BT下载 | 八卦星闻 | 音乐在线 | 在线游戏 | 免费电影 | 进入问吧 | 
servlet绘图入门:模拟投票统计的Servlet, 一、web.xml文件,在这里配置servlet。<?xmlversion="1.0"encoding="GB2312"?><!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.2//EN&q,
您现在的位置: 编程资料,学习资料,c,c++,vc,vc++,java,jsp,j2ee,j2me,asp,php >> 文章中心 >> JAVA 专区 >> JSP 技术 >> 文章正文
【字体:
servlet绘图入门:模拟投票统计的Servlet   进入问吧

本站地址:http://www.bajiao123.com

作者:admin    文章来源:本站    点击数:    更新时间:2007-5-25    

servlet绘图入门:模拟投票统计的Servlet

 


一、web.xml文件,在这里配置servlet。

<?xml version="1.0" encoding="GB2312"?>

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

<servlet>
 <servlet-name>InitVote</servlet-name>
   <description>调用初始化参数的网站投票Servlet</description>
 <servlet-class>com.fatcat.webchart.InitVoteServlet</servlet-class>
 <init-param>
   <param-name>Python</param-name>
   <param-value>10</param-value>
 </init-param>

<init-param>
   <param-name>Java</param-name>
   <param-value>10</param-value>
 </init-param>
 <init-param>
    <param-name>CSharp</param-name>
    <param-value>10</param-value>
 </init-param>
 <init-param>
    <param-name>Perl</param-name>
    <param-value>10</param-value>
 </init-param>
 <init-param>
     <param-name>PHP</param-name>
     <param-value>10</param-value>
 </init-param>
</servlet>

<servlet-mapping>
   <servlet-name>InitVote</servlet-name>
   <url-pattern>/initVote</url-pattern>
</servlet-mapping>

</web-app>

二、投票页面html文件
<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>投票统计</title>
</head>

<body>
<form action = "/Test/initVote" method = "get">
<p>
选择您最熟悉的编程语言</p>
<p>
  <input type="radio" name="program" value="0" />Python <br />
  <input type="radio" name="program" value="1" />Java <br />
  <input type="radio" name="program" value="2" />C# <br />
  <input type="radio" name="program" value="3" />Perl <br />
  <input type="radio" name="program" value="4" />PHP <br />
</p>
<p>
  <input name="submit" type = "submit" value = " 投 票 " />
</p>
</form>
</body>
</html>


如图:


三、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.                                 *

 **************************************************************************/

运行图:


[1] [2] 下一页

   

进入问吧

本站地址:http://www.bajiao123.com

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 高级搜索
    编程资料集中营