javaweb 投票系统
程序员文章站
2024-02-02 12:50:04
...
最近做了一个简单的投票系统,来分享一下
1 先来看一下目录
2 效果图是这样的
首页,对一次演讲进行简单的评分
如果选项不完整,或者是没有选项,就会提示选项不完整,并且点击按钮进入选项页面
如果选项完整就会进入图像类
然后可以查看本次的总结果,是过还是不过
以后就是这个小程序的总体结果图
2 接下来,分享代码
2.1 主页面
<%@ page import="java.util.UUID" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
div.ex
{
background-color:#e5eecc;
padding:7px;
border:solid 1px #c3c3c3;
}
</style>
</head>
<body>
<h2>请为本次演讲评分</h2>
<form action="/统计/Damo1Servlet" name="demo" method="post" onsubmit="return dosubmit()">
<h3>1 素材内容是否吸引</h3>
<div class="ex">
<input type="radio" name="sex" value="one">吸引<br/>
<input type="radio" name="sex" value="two">不够吸引<br/><br/>
</div>
<h3>2 语调音量是否洪亮</h3>
<div class="ex">
<input type="radio" name="sound" value="one1">洪亮<br/>
<input type="radio" name="sound" value="two1">不够洪亮<br/><br/>
</div>
<h3>3 是否有感情色彩</h3>
<div class="ex">
<input type="radio" name="love" value="one2">有<br/>
<input type="radio" name="love" value="two2">不太明显<br/><br/>
</div>
<input id="ss" type="submit" name="name" value="提交并查看结果" >
</form>
</body>
<script>
function dosubmit(){
document.getElementById("ss").disabled="true";
alert("亲,只能提交一次哦");
return true;
}
</script>
</body>
</html>
2.2 选项不完整提示页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>您的选项不完整,请点击下方按钮,重新选择</p>
<form>
<input type="button" value="请重新选择"
onclick="javascrtpt:window.location.href='/统计/subit.jsp'" />
</form>
</body>
</html>
2.3 图像页面
<%@page import="com.servlet.Barchart1"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String fileName=Barchart1.getBarchart(session);
System.out.println(fileName);
%>
<img src="DisplayChart?filename=<%=fileName %>" width="700" height="500" border="0"/>
<form action="/统计/Damo2Servlet" name="demo" method="post">
<input type="submit" name="name" value="查看最终结果" >
</form>
</body>
</html>
3 接下来是配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>统计</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Damo1Servlet</display-name>
<servlet-name>Damo1Servlet</servlet-name>
<servlet-class>com.servlet.Damo1Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Damo1Servlet</servlet-name>
<url-pattern>/Damo1Servlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Damo2Servlet</display-name>
<servlet-name>Damo2Servlet</servlet-name>
<servlet-class>com.servlet.Damo2Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Damo2Servlet</servlet-name>
<url-pattern>/Damo2Servlet</url-pattern>
</servlet-mapping>
</web-app>
4 servlet类
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.org.apache.xpath.internal.operations.And;
/**
* 拿到前台数据
*/
public class Damo1Servlet extends HttpServlet {
// 创建一个集合,用来存储前台数据
static int sum;
static int num;
static int sum1;
static int num1;
static int sum2;
static int num2;
List<String> arrays1 = new ArrayList<String>();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String tokens = request.getParameter("name");
System.out.println(tokens);
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
response.setHeader("content-type", "text/html;charset=UTF-8");
// 模块1
String name = request.getParameter("sex");
// 模块2
String name1 = request.getParameter("sound");
// 模块3
String name2 = request.getParameter("love");
if (name == null || name1 == null || name2 == null) {
response.sendRedirect("damo.jsp");
} else {
if (name.equalsIgnoreCase("one")) {
sum++;
} else if (name.equalsIgnoreCase("two")) {
num++;
}
if (name1.equalsIgnoreCase("one1")) {
sum1++;
} else if (name1.equalsIgnoreCase("two1")) {
num1++;
}
if (name2.equalsIgnoreCase("one2")) {
sum2++;
} else if (name2.equalsIgnoreCase("two2")) {
num2++;
}
/*
* 2、重定向:response.sendRedirect("success.jsp");
* 重定向一般是为了防止用户提交完数据后点浏览器刷新或点后退之后产生重复提交
*/
response.sendRedirect("login2.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4 生成图表类。就是将数据转化为柱状图
package com.servlet;
import java.io.IOException;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
public class Barchart1 extends Damo1Servlet{
public static String getBarchart(HttpSession session) throws Exception {
DefaultCategoryDataset dataset=new DefaultCategoryDataset();
System.out.println(sum);
System.out.println(num);
dataset.addValue(sum, "素材内容", "吸引");
dataset.addValue(num, "素材内容", "不够吸引");
dataset.addValue(sum1, "语调音量", "洪亮");
dataset.addValue(num1, "语调音量", "不够洪亮");
dataset.addValue(sum2, "感情色彩", "有");
dataset.addValue(num2, "感情色彩", "不太明显");
JFreeChart chart = ChartFactory.createBarChart3D(
"本次演讲结果图", // 图表标题
"评分项目", // 文件夹轴的显示标签
"成绩", // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
true // 是否生成URL链接
);
String fileName =ServletUtilities.saveChartAsPNG(chart, 700, 500, null,session);
return fileName;
}
}
还有两个类,结构都是和生成图标类一样,包括一个js页面,结构都一样,及不粘贴了
上一篇: spring boot 打包jar
下一篇: spring boot 热部署