新聞中心
這篇文章主要為大家詳細(xì)介紹了Java制作投票系統(tǒng),文中示例代碼介紹的非常詳細(xì),零基礎(chǔ)也能參考此文章,感興趣的小伙伴們可以參考一下。
創(chuàng)新互聯(lián)建站是一家專業(yè)提供梁子湖企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、成都做網(wǎng)站、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為梁子湖眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計公司優(yōu)惠進(jìn)行中。
代碼展示:
package com.tarena.wgh.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class PollServlet extends HttpServlet {
/**
* The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("GBK"); //設(shè)置請求的編碼方式 String item=request.getParameter("item"); //獲取投票項 ServletContext servletContext=request.getSession().getServletContext(); //獲取ServletContext對象該對象在application范圍內(nèi)有效 Map map=null; if(servletContext.getAttribute("pollResult")!=null){ map=(Map)servletContext.getAttribute("pollResult"); //獲取投票結(jié)果 map.put(item,Integer.parseInt(map.get(item).toString())+1); //將當(dāng)前的投票項加1 }else{ //初始化一個保存投票信息的Map集合,并將選定投票項的投票數(shù)設(shè)置為1,其他為0 String[] arr={"基礎(chǔ)教程類","實例集錦類","經(jīng)驗技巧類","速查手冊類","案例剖析類"}; map=new HashMap(); for(int i=0;i}
關(guān)于Java制作投票系統(tǒng)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果喜歡這篇文章,不如把它分享出去讓更多的人看到。
網(wǎng)站欄目:Java制作投票系統(tǒng)代碼
URL鏈接:http://www.dlmjj.cn/article/pighhg.html