導航結構圖:html
關鍵問題的解決方法描述:java
1. 建立新主題的時候,能夠輸入多個投票選項,選項的個數是不定的,因此須要要動態的獲取用戶輸入的選項。解決的方法是利用頁面的局部刷新爲用戶動態生成投票選項輸入框。爲用戶提供一個選項添加按鈕,當用戶須要添加輸入投票選項的時候就能夠按按鈕生成一個選項輸入框。sql
2. 投票的選項的數量是不肯定,這些數據都須要保存進數據庫,如何爲數據庫建表,建表的字段又該怎麼創建。解決這個問題的方法是將每個投票主題的全部選項都用同一個字段保存。將每個選項的內容所有放在一塊兒,用本身定義的分隔符分開,而後保存在同一個字段裏,這樣就解決了投票選項個數不肯定,但依舊能夠保存進數據庫的問題。數據庫
3. 用戶設置的投票方式:單選或多選。解決這個問題的方法是隻容許單選的投票用單選框提供給用戶投票,容許多選的投票提供複選框給用戶投票。當要獲取用戶投票的數據時,遍歷全部的選項,對有全部被選中的單選框或是複選框進行記錄。app
4. 開啓和關閉一個投票。解決這個問題的方法是在數據庫中爲每個主題建立一個字段用於記錄該主題是否容許投票。該字段只有2個值:0和1。當取值爲0時,不容許投票,只容許查看,當取值爲1時容許投票。jsp
核心代碼:測試
客戶端頁面局部刷新JavaScript:
url
function createRequest(url,theme,q){ var index="div_vote0"+q; //alert(index); http_request = false; if(window.XMLHttpRequest){ http_request = new XMLHttpRequest(); }else if(window.ActiveXObject){ try{http_request = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{ http_request = new ActiveXObject("Microsoft.XMLHTTP");} catch(e){ } } } if(!http_request){ alert("cannt"); return false; } http_request.onreadystatechange=function(){getResult(index);}//getResult; http_request.open('POST',url,true); http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); http_request.send("theme="+theme); } //查看 function getResult(index){ var t1=index+"1"; var t2=index+"2"; if(http_request.readyState==4){ if(http_request.status==200){ document.getElementById(index).innerHTML=http_request.responseText; document.getElementById(t1).innerHTML = "<input type=\"button\" class=\"three\" value=\"收起\" onclick=\"abc(\'"+index+"\')\">"; document.getElementById(t2).innerHTML = ""; }else{ document.getElementById("div_vote0").innerHTML="操做失敗!"; document.getElementById(t1).innerHTML = "<input type=\"button\" class=\"three\" value=\"收起\" onclick=\"abc(\'"+index+"\')\">"; document.getElementById(t2).innerHTML = ""; } } } function abc(index){ var t1=index+"1"; var t2=index+"2"; document.getElementById(index).innerHTML = ""; document.getElementById(t1).innerHTML = ""; document.getElementById(t2).innerHTML = ""; }
在Servlet裏向數據庫中添加數據:spa
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ String data = new String(request.getParameter("mydata").getBytes("iso-8859-1"),"utf-8"); String radio = request.getParameter("theme_radio"); int theme_radio = Integer.parseInt(radio); String[] str = data.split("="); String[] temps =str[1].split(","); String temp=""; for(int i=0;i<temps.length;i++){ temp+="0,"; out.println("============temps="+temps[i]); } String sql = "insert into data(theme,temp,able,mult) values(?,?,?,?)"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, str[0]); ps.setString(2, str[1]); ps.setInt(3, 1); ps.setInt(4,theme_radio); int row = ps.executeUpdate(); //out.println("=====================row="+row); ps.close(); sql = "insert into votes(theme,temp) values(?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, str[0]); ps.setString(2, temp); row = ps.executeUpdate(); //out.println("=====================temp row="+row); ps.close(); } catch (SQLException ex) { Logger.getLogger(AddthemeServlet.class.getName()).log(Level.SEVERE, null, ex); } response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter wout = response.getWriter(); wout.print("<h4 ><font color=red>主題爲\""+str[0]+"\"的投票添加成功!</font></h4>"); wout.flush(); wout.close(); }
在Servlet裏驗證用戶登陸:
code
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ String code = (String)request.getSession().getAttribute("validate_code"); String sql = "select * from data"; String user = request.getParameter("name").toString(); String pwd = request.getParameter("pwd").toString(); String mycode = request.getParameter("code"); String dbuser=""; String dbpwd = ""; boolean flag1=false; if(code.equals(mycode)) flag1=true; try { stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ int i = rs.getInt("able"); if(i==3){ dbuser = rs.getString("theme"); dbpwd = rs.getString("temp"); break ; } } rs.close(); stmt.close(); } catch (SQLException ex) { Logger.getLogger(IndexServlet.class.getName()).log(Level.SEVERE, null, ex); } if(!flag1){ String message = "<h4 ><font color=red>驗證碼不正確!</font></h4>"; request.setAttribute("message", message); request.getRequestDispatcher("login.jsp").forward(request, response); } if(user.equals(dbuser)&&pwd.equals(dbpwd)){ request.getRequestDispatcher("job.jsp").forward(request, response); } else { String message = "<h4 ><font color=red>用戶名或密碼不正確!</font></h4>"; request.setAttribute("message", message); request.getRequestDispatcher("login.jsp").forward(request, response); } }
在Servlet裏處理投票:
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{ String theme = new String(request.getParameter("theme").getBytes("iso-8859-1"),"utf-8"); String votes = new String(request.getParameter("votes").getBytes("iso-8859-1"),"utf-8"); String sql = "select * from votes"; String temp=""; String IPAddr = getIpAddr(request); //得到IP int sum = 0; boolean ipFlag = false; if(votes==null||votes.equals("")) { return ; } int newtime = getDate(); //得到當前時間 PreparedStatement ps; try { stmt = conn.createStatement(); //===================================== ResultSet rs = stmt.executeQuery(sql); sql = "select * from ipaddr"; rs = stmt.executeQuery(sql); String themes=""; while(rs.next()){ String ipAddr = rs.getString("ip"); if(ipAddr.equals(IPAddr)){ themes = rs.getString("themes"); int oldtime = rs.getInt("time"); sum = newtime - oldtime; ipFlag = true; break; } } rs.close(); if(!ipFlag){ //若是ip不存在 sql = "insert into ipaddr(ip,time,themes) values(?,?,?)"; ps = conn.prepareStatement(sql); ps = conn.prepareStatement(sql); ps.setString(1, IPAddr); ps.setInt(2, newtime); ps.setString(3, theme+","); int row = ps.executeUpdate(); ps.close(); }else if(!findTheme(themes,theme)) { if(themes==null) themes=""; ipFlag=false; sql = "update ipaddr set themes=? where ip=?"; ps = conn.prepareStatement(sql); String mythemes = themes+theme+","; ps.setString(1, mythemes); ps.setString(2, IPAddr); ps.executeUpdate(); ps.close(); }else { ipFlag = true; } sql = "select * from votes"; rs = stmt.executeQuery(sql); while(rs.next()){ String theme1 = rs.getString("theme"); if(theme1.equals(theme)){ temp = rs.getString("temp"); break; } } temp = deal(temp,votes); //獲取數據 sql = "update votes set temp=? where theme=?"; ps = conn.prepareStatement(sql); ps.setString(1, temp); ps.setString(2, theme); ps.executeUpdate(); ps.close(); } catch (SQLException ex) { Logger.getLogger(IndexServlet.class.getName()).log(Level.SEVERE, null, ex); } response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter wout = response.getWriter(); if(!ipFlag) wout.print("<h4 ><font color=blue>謝謝您的投票!<hr></font></h4>"); wout.flush(); wout.close(); }
系統測試截圖
1 系統開始時的界面:
用戶剛進入系統時的開始界面。用戶能夠查看系統已有的投票,也能夠爲其中的某個主題進行投票。
2 對其中的某個主題進行投票:
有些投票只容許單選,有些投票能夠多選。
3 管理員登錄界面:
4 管理員權限界面:
只有管理員登錄後才能夠執行的操做。
5 發起一個新的投票:
按添加項按鈕能夠動態添加須要的項目。
6 管理開啓關閉投票:
7 用戶名和密碼修改: