十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
把投票的信息放在 ServletContext对象 中。也就是 JSP 内置对象中的application对象。因为投票信息是所有用户都要看的信息,所以要把他放在 ServletContext对象 中。这样只要是 应用一加载就会创建 ServletContext对象 直到应用销毁,也就是服务器关闭时,ServletContext对象 才销毁。这样就使得所有用户公用一个对象存储信息。具体的实现那要根据你自己的需求,具体设计。
创新互联专注于企业成都全网营销推广、网站重做改版、滕州网站定制设计、自适应品牌网站建设、HTML5、商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为滕州等各大城市提供网站开发制作服务。
public class CServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {int n = 0;ServletContext application = getServletContext();Integer num = (Integer) application.getAttribute("num");if (num != null) {n = num;}application.setAttribute("num", ++n);response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.println("你是第" + n + "个访问者");}}
上面是个类似的例子。你可以参考一下。package servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;这是需要导入的包
区别就在于代码内容不同,Javaweb投票系统代码是用JAVA写一个投票系统,代码内容是关于投票的,网页制作代码,代码内容是制作网页。
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Scanner;
public class Test {
public static void Ballots() {
Scanner sc = new Scanner(System.in);
int[] count = new int[7];
String instr = "";
int spoiled = 0, sum = 0;
for (int i = 0; i count.length; i++) {
count[i] = 0;
}
System.out.println("输入方框数字代码投票给歌手,输入.结束 ");
do {
System.out.print("张学友[0], 刘德华[1], 小刚[2], 孙燕姿[3], 梁静茹[4], 王杰[5], 刘若英[6])? ");
instr = sc.nextLine();
if (instr.equals("0")) {
count[0]++;
} else if (instr.equals("1")) {
count[1]++;
} else if (instr.equals("2")) {
count[2]++;
} else if (instr.equals("3")) {
count[3]++;
} else if (instr.equals("4")) {
count[4]++;
} else if (instr.equals("5")) {
count[5]++;
} else if (instr.equals("6")) {
count[6]++;
} else {
if (!instr.equals(".")) {
spoiled++;
}
}
} while (!instr.equals("."));
System.out.println("投票结束,结果是 : \n");
System.out.println("歌手\t\t得票数");
System.out.println("======\t\t=========");
for (int i = 0; i count.length; i++) {
if (i == 0) {
System.out.println(" 张学友 \t\t" + count[i]);
} else if (i == 1) {
System.out.println(" 刘德华 \t\t" + count[i]);
} else if (i == 2) {
System.out.println(" 小刚 \t\t" + count[i]);
} else if (i == 3) {
System.out.println(" 孙燕姿 \t\t" + count[i]);
} else if (i == 4) {
System.out.println(" 梁静茹 \t\t" + count[i]);
} else if (i == 5) {
System.out.println(" 王杰 \t\t" + count[i]);
}else if (i == 6) {
System.out.println(" 刘若英 \t\t" + count[i]);
}
}
for (int t : count) {
sum += t;
}
System.out.println("弃权票数: " + spoiled);
System.out.println("总票数: " + sum);
}
public static void main(String[] args) {
System.out.println("投票开始:");
Scanner sc = new Scanner(System.in);
String is = "";
do {
Ballots();
System.out.println("继续第二轮投票? (y/n):");
is = sc.nextLine();
} while (is.equals("y"));
}
}
我之前做过类似的投票小项目,在这里把源码发给你。你自己好好的参考一下。--------------------------package com.tv.bean;public class TVBean {
private String tvName;
private int tvCount;
public String getTvName() {
return tvName;
}
public void setTvName(String tvName) {
this.tvName = tvName;
}
public int getTvCount() {
return tvCount;
}
public void setTvCount(int tvCount) {
this.tvCount = tvCount;
}
}------------------------------package com.tv.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;import com.tv.bean.TVBean;
public class DBUtil {
private Connection con;
private PreparedStatement ps = null;
private ResultSet rs;
public void getCon(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=TV","sa","");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeCon(){
try {
if(rs !=null) rs.close();
if(ps != null) ps.close();
if(con != null) con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public ArrayList getAll(){
ArrayList al = new ArrayList();
this.getCon();
String sql = "select * from TVInfo order by tvCount desc";
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
TVBean tb = new TVBean();
tb.setTvName(rs.getString(1));
tb.setTvCount(rs.getInt(2));
al.add(tb);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.closeCon();
}
return al;
}
public boolean updateByName(String name){
this.getCon();
String sql = "update TVInfo set tvCount=tvCount+1 where tvName='"+name+"'";
try {
ps = con.prepareStatement(sql);
int i = ps.executeUpdate();
if(i 0) return true;
else return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
this.closeCon();
}
}
}
---------------------------------------package com.tv.servlet;import java.io.IOException;
import java.util.ArrayList;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.tv.dao.DBUtil;public class VoteServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
DBUtil dao = new DBUtil();
String [] name = request.getParameterValues("tvs");
int num = name.length;
for(int i =0; i num; i++){
if(dao.updateByName(name[i])){
request.setAttribute("to", "投票成功!");
ArrayList al = dao.getAll();
HttpSession session = request.getSession();
session.setAttribute("al", al);
request.getRequestDispatcher("success.jsp").forward(request, response);
}else{
request.setAttribute("to", "投票失败!请重新再试!");
}
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
-----------------------------------%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
%@taglib prefix="c" uri=" "%html
head
title电视投票/title
/head body
center
form action="vote" method="post"
table border="1" align="center"
tr align="center"
td
h2
选择您最喜欢的电视剧并投上一票
/h2
/td
/tr
tr
td
input type="checkbox" name="tvs" value="咏春" /
咏春
/td
/tr
tr
td
input type="checkbox" name="tvs" value="金婚" /
金婚
/td
/tr
tr
td
input type="checkbox" name="tvs" value="士兵突击" /
士兵突击
/td
/tr
tr
td
input type="checkbox" name="tvs" value="少年张三丰" /
少年张三丰
/td
/tr
tr
td align="center"
input type="submit" value=" 提交 " /
input type="reset" value=" 重置 " /
/td
/tr
/table
/form
/center
/body
/html
-------------------------------------第一段代码为JavaBean;第二段代码为数据库连接类;第三段代码为Servlet控制类;第四段代码为JSP显示页面。希望能够解决你的问题!
先定义一个学生的对象,里面有名字,和票数两个属性,再定义一个排序函数,返回值为数组,就是取得三个数的数组,打印输出,或者可以自己想一个更加面向对象的结构实现,在简单问题上多用面向对象去思考问题 。
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int cj, yxsum = 0, tgsum = 0, bjgsum = 0;
while ((cj = in.nextInt()) != 0) {
if (cj = 85) {
yxsum++;
} else if (cj = 60) {
tgsum++;
} else {
bjgsum++;
System.out.println(yxsum);
System.out.println(tgsum);
System.out.println(bjgsum);
}
扩展资料:
当编辑并运行一个Java程序时,需要同时涉及到这四种方面。使用文字编辑软件(例如记事本、写字板、UltraEdit等)或集成开发环境(Eclipse、MyEclipse等)在Java源文件中定义不同的类;
通过调用类(这些类实现了Java API)中的方法来访问资源系统,把源文件编译生成一种二进制中间码,存储在class文件中,再通过运行与操作系统平台环境相对应的Java虚拟机来运行class文件,执行编译产生的字节码,调用class文件中实现的方法来满足程序的Java API调用。
参考资料来源:百度百科-Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Vote {
public static void main(String args[]) {
try {
String[] persons = new String[]{"1","2","3"};
Integer[] votes = new Integer[]{0,0,0};
BufferedReader sin = new BufferedReader(new InputStreamReader(
System.in));
String readline;
System.out.println("================投票开始===============");
System.out.println("请按1,2,3投票,Enter确认");
System.out.println("按0--Enter结束投票,公布结果\n");
readline = sin.readLine(); // 从系统标准输入读入一字符串
while (!readline.equals("0")) {
String voteStr = readline;
if(persons[0].equals(voteStr)){
votes[0] +=1;
}else if(persons[1].equals(voteStr)){
votes[1] +=1;
}else if(persons[2].equals(voteStr)){
votes[2] +=1;
}else{
System.out.println("无效的投票!");
}
readline = sin.readLine(); // 从系统标准输入读入一字符串
} // 继续循环
System.out.println("================投票结束===============");
System.out.println("================结果公布===============");
System.out.println(persons[0]+"================"+persons[1]+"==============="+persons[2]);
System.out.println(votes[0]+"================"+votes[1]+"==============="+votes[2]);
} catch (Exception e) {
System.out.println("Error" + e); // 出错,则打印出错信息
}
}
}