定義三個(gè)變量記錄每個(gè)人的投票數(shù)。點(diǎn)擊投票按鈕后,根據(jù)checkbox的勾選情況將三個(gè)變量累加。點(diǎn)擊查看按鈕時(shí)展示這三個(gè)變量
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的巴彥淖爾網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
把投票的信息放在 ServletContext對(duì)象 中。也就是 JSP 內(nèi)置對(duì)象中的application對(duì)象。因?yàn)橥镀毙畔⑹撬杏脩舳家吹男畔?,所以要把他放?ServletContext對(duì)象 中。這樣只要是 應(yīng)用一加載就會(huì)創(chuàng)建 ServletContext對(duì)象 直到應(yīng)用銷毀,也就是服務(wù)器關(guān)閉時(shí),ServletContext對(duì)象 才銷毀。這樣就使得所有用戶公用一個(gè)對(duì)象存儲(chǔ)信息。具體的實(shí)現(xiàn)那要根據(jù)你自己的需求,具體設(shè)計(jì)。
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 + "個(gè)訪問者");}}
上面是個(gè)類似的例子。你可以參考一下。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;這是需要導(dǎo)入的包
我之前做過類似的投票小項(xiàng)目,在這里把源碼發(fā)給你。你自己好好的參考一下。--------------------------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", "投票失??!請(qǐng)重新再試!");
}
}
} 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;第二段代碼為數(shù)據(jù)庫連接類;第三段代碼為Servlet控制類;第四段代碼為JSP顯示頁面。希望能夠解決你的問題!
p style=line-height: 150%SPAN style="FONT-SIZE: 12px"STRONG這是用文本文件作為存儲(chǔ)載體的投票系統(tǒng):/STRONG/SPAN
p style=line-height: 150%SPAN style="FONT-SIZE: 12px"STRONGvote.java:/STRONG/SPAN
SPAN style="FONT-SIZE: 12px"STRONG/STRONG p style=line-height: 150%
// Java Document
package vote;
import java.io.*;
import java.util.*;
public class vote extends Object
{
public String filePath="";
public int n;
private File voteFile;
private BufferedReader fileRead;
private PrintWriter fileWrite;
public String systemMessage="";
private String voteStr[]=new String[10];
public int voteNum[]=new int[10];
public void createFile()
throws FileNotFoundException
{
voteFile=new File(filePath);
if(!voteFile.exists())
{
fileWrite=new PrintWriter(new FileOutputStream(filePath));
for(int i=0;in;i++) fileWrite.println("0");
fileWrite.close();
}
}
public void writeFile()
throws FileNotFoundException
{
fileWrite=new PrintWriter(new FileOutputStream(filePath));
for(int i=0;in;i++)
{
fileWrite.println(voteNum[i]);
}
fileWrite.close();
}
public void readFile()
throws FileNotFoundException
{
fileRead=new BufferedReader(new FileReader(filePath));
for(int i=0;in;i++)
{
try
{voteStr[i]=fileRead.readLine();
}
catch(IOException f)
{
voteStr[i]="0";}
voteNum[i]=Integer.parseInt(voteStr[i]);
}
try
{
fileRead.close();
}
catch(IOException d)
{
systemMessage=d.toString();
}
}
}
p style=line-height: 150%SPAN style="LINE-HEIGHT: 15pt"SPAN style="FONT-SIZE: 12px"STRONGvote.jsp:/STRONG
%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %
%@ page import="java.util.*"%
%@ page import="java.lang.*"%
%@ page import="java.io.*"%
jsp:useBean id="vote" scope="request" class="vote.vote"/
%
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
if(vote1.compareTo("0")==0)
vote.voteNum[0]++;
if(vote1.compareTo("1")==0)
vote.voteNum[1]++;
if(vote1.compareTo("2")==0)
vote.voteNum[2]++;
if(vote1.compareTo("3")==0)
vote.voteNum[3]++;
vote.writeFile();
%
script language="javascript"
alert("感謝你投了寶貴的一票");
self.location="index.jsp";
/script/SPAN/SPAN
p style=line-height: 150%STRONG /STRONGSPAN style="LINE-HEIGHT: 15pt"SPAN style="FONT-SIZE: 12px"STRONGsee.jsp:/STRONG
%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %
%@ page import="java.util.*"%
%@ page import="java.lang.*"%
%@ page import="java.io.*"%
jsp:useBean id="vote" scope="request" class="vote.vote"/
%
String vote1=request.getParameter("lang");
vote.n=4;
vote.filePath="vote.txt";
vote.createFile();
vote.readFile();
int total=0;
float voteFlo[]=new float[5];
for(int i=0;i4;i++) total+=vote.voteNum[i];
for(int i=0;i4;i++) voteFlo[i]=150*((float)vote.voteNum[i]/(float)total);
%
html
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
title查看調(diào)查/title
link href="t1.css" rel="stylesheet" type="text/css"
/head
body
table width="30%" border="0" class="t1"
tr
td colspan="2"div align="center"調(diào)查結(jié)果/div/td
/tr
tr
td width="18%"JSP/td
td width="82%"img src="bar.gif" width=%=voteFlo[0]% height=8 %=vote.voteNum[0]%/td
/tr
tr
tdASP/td
tdimg src="bar.gif" width=%=voteFlo[1]% height=8 %=vote.voteNum[1]%/td
/tr
tr
tdPHP/td
tdimg src="bar.gif" width=%=voteFlo[2]% height=8 %=vote.voteNum[2]%/td
/tr
tr
td其他/td
tdimg src="bar.gif" width=%=voteFlo[3]% height=8 %=vote.voteNum[3]%/td
/tr
tr
td colspan="2"div align="center"a href="javascript:window.close();"關(guān)閉窗口/a/div/td
/tr
/table
/body
/html
/SPAN/SPAN
p style=line-height: 150%SPAN style="LINE-HEIGHT: 15pt"SPAN style="FONT-SIZE: 12px"SPAN style="LINE-HEIGHT: 15pt"SPAN style="FONT-SIZE: 12px"STRONGindex.jsp:/STRONG
/SPAN/SPAN
/SPAN/SPAN p style=line-height: 150%SPAN style="LINE-HEIGHT: 15pt"SPAN style="FONT-SIZE: 12px"%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %
html
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
title投票/title
link href="t1.css" rel="stylesheet" type="text/css"
/head
script language="javascript"
function cw()
{window.open("see.jsp","mywindow","toolbar=no,left=150,top=200,width=270,height=350,menubar=no,systemMenu=no");
}
/script
body
table width="15%" height="250" align="left"
tr
tdform name="form1" method="post" action="vote.jsp"
table width="100%" height="250" border="1" align="center" bordercolor="#9966CC" class="t1"
tr
tddiv align="left"你所使用的開發(fā)語言/div/td
/tr
tr
tdinput type="radio" name="lang" value="0"
JSP/td
/tr
tr
tdinput type="radio" name="lang" value="1"
ASP/td
/tr
tr
tdinput type="radio" name="lang" value="2"
PHP/td
/tr
tr
tdinput type="radio" name="lang" value="3"
其他 /td
/tr
tr
tddiv align="center"
input name="vote" type="image" src="poll.gif" width="40" height="20" border="0"
a href="javascript:cw()"img src="see.gif" width="40" height="20" border="0"/a/div/td
/tr
/table
/form/td
/tr
/table
/body
/html
/SPAN/SPAN/SPAN
區(qū)別就在于代碼內(nèi)容不同,Javaweb投票系統(tǒng)代碼是用JAVA寫一個(gè)投票系統(tǒng),代碼內(nèi)容是關(guān)于投票的,網(wǎng)頁制作代碼,代碼內(nèi)容是制作網(wǎng)頁。
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("請(qǐng)按1,2,3投票,Enter確認(rèn)");
System.out.println("按0--Enter結(jié)束投票,公布結(jié)果\n");
readline = sin.readLine(); // 從系統(tǒng)標(biāo)準(zhǔn)輸入讀入一字符串
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(); // 從系統(tǒng)標(biāo)準(zhǔn)輸入讀入一字符串
} // 繼續(xù)循環(huán)
System.out.println("================投票結(jié)束===============");
System.out.println("================結(jié)果公布===============");
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); // 出錯(cuò),則打印出錯(cuò)信息
}
}
}
網(wǎng)頁標(biāo)題:java投票系統(tǒng)源代碼 投票程序java
網(wǎng)頁路徑:http://chinadenli.net/article30/dodeiso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、定制網(wǎng)站、企業(yè)網(wǎng)站制作、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)