?? fjp.java
字號:
/** * <p> Title: FreeJaPoll -Free Java Poll-</p> * <p> Description: Servlet that make simple adding a web survey based on Java Technology on your site </p> * <p>Copyright: Copyright(c) 2002; </p> * This file is part of FreeJaPoll. FreeJaPoll is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version. FreeJaPoll is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You have received a copy of the GNU General Public License along with FreeJaPoll * @author nkanter e-mail: nkanter@pensieroacromatico.org * @version 1.0 */package org.pensieroacromatico.freejapoll;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.util.StringTokenizer;import java.util.ResourceBundle;import java.util.GregorianCalendar;import java.text.DateFormat;public class FJP extends HttpServlet { static ResourceBundle res1 = ResourceBundle.getBundle("org.pensieroacromatico.freejapoll.Res"); private static final String CONTENT_TYPE = "text/html"; private static int[] arrayValori =new int[10]; private static int totale; private int numberOfChoice; private String choose, pageTitle, resultTitle, backGroundColor, textColor, tableColor, tableBorderColor, returnString, returnLink; private String imageBar, imageBarEnd, footer, header, headerLink, footerLink, noTerrorist, noTerroristPage; private final String DBFILE = res1.getString("tmp_initDb_nk"); private final String HOSTCOOKIE="www.pensieroacromatico.org"; private final String VOTATO="votato"; private final String IPLOG = "YOUR_ROOT_PATH/iplogs.nk"; private String[] labelChoose = new String[10]; private final String[] labelChooseParam ={"LabelChoose1", "LabelChoose2", "LabelChoose3", "LabelChoose4", "LabelChoose5", "LabelChoose6", "LabelChoose7", "LabelChoose8", "LabelChoose9", "LabelChoose10"}; private String temp, temp2; /** Servlet init method; it checks if exists the DBFILE and if there are some data stored in it. * @throws ServletException */ public void init() throws ServletException { caricaDati(); } /**Process the HTTP Get request; here are set the parameters before producing the * html file. * @param request * @param response * @throws ServletException * @throws IOException */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); settaParametri(request); caricaDati(); scriviHTML(out); } /**Process the HTTP Post request; here are set the parameters; then a control check * the "noTerrorist" option (if the flag is "Yes"); an additional control check the ip of * the remote user and the cookie on his machine, in order to make impossible to vote more than once * in a three-day period. Then, if voting is allowed, a new vote is added, the user is registered * (logging his ip and setting a cookie on his machine) and the html file is produced. * @param request * @param response * @throws ServletException * @throws IOException */ public synchronized void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); settaParametri(request); if(!controllaTerrorista(request)){ if(controllaCookie(request) && controllaIP(request)){ choose = request.getParameter("Choose"); caricaDati(); vota(choose); response.addCookie(aggiungiCookie()); aggiungiIP(request); scriviHTML(out); } else response.sendRedirect(res1.getString("_html_novote_htm")); } else response.sendRedirect(this.noTerroristPage); } /** * Destroy servlet method; it saves data on the default file. */ public void destroy() { super.destroy(); salvaDati(); } /** * This method check wich option is selected and add the new value to the respective * variabile; after that it saves data on the default file. * @param s The survey's option value chosen by the user */ private synchronized void vota(String s){ if(choose.compareToIgnoreCase(res1.getString("Uno"))==0) arrayValori[0]+=1; else if(choose.compareToIgnoreCase(res1.getString("Due"))==0) arrayValori[1]+=1; else if(choose.compareToIgnoreCase(res1.getString("Tre"))==0) arrayValori[2]+=1; else if(choose.compareToIgnoreCase(res1.getString("Quattro"))==0) arrayValori[3]+=1; else if(choose.compareToIgnoreCase(res1.getString("Cinque"))==0) arrayValori[4]+=1; else if(choose.compareToIgnoreCase(res1.getString("Sei"))==0) arrayValori[5]+=1; else if(choose.compareToIgnoreCase(res1.getString("Sette"))==0) arrayValori[6]+=1; else if(choose.compareToIgnoreCase(res1.getString("Otto"))==0) arrayValori[7]+=1; else if(choose.compareToIgnoreCase(res1.getString("Nove"))==0) arrayValori[8]+=1; else if(choose.compareToIgnoreCase(res1.getString("Dieci"))==0) arrayValori[9]+=1; totale++; salvaDati(); } /** * Method that sets all the parameters(taking them from the resource bundle and from * the html file). * @param req */ private void settaParametri(HttpServletRequest req){ this.pageTitle = res1.getString("Crew_Del_Pensiero"); this.resultTitle = res1.getString("_Logo_Contest_2K2"); this.backGroundColor = res1.getString("_999999"); this.textColor = res1.getString("_000000"); this.tableColor = res1.getString("_CCCCCC"); this.tableBorderColor = res1.getString("_000000"); this.returnString = res1.getString("Ritorna_a_Logo"); this.returnLink = res1.getString("_x_html"); this.numberOfChoice = Integer.parseInt(req.getParameter(res1.getString("NumberOfChoice"))); for(int i=0;i<numberOfChoice;i++){ labelChoose[i]=req.getParameter(labelChooseParam[i]); } this.footer =res1.getString("footer"); this.header = res1.getString("header"); if(header.compareToIgnoreCase("Yes")==0) this.headerLink = ""; else this.headerLink = null; if(footer.compareToIgnoreCase("Yes")==0) this.footerLink = ""; else this.footerLink = null; this.imageBar = res1.getString("_immagini_graficoBase"); this.imageBarEnd = res1.getString("_immagini"); this.noTerrorist = res1.getString("noTerrorist"); if(this.noTerrorist.equalsIgnoreCase("Yes")) this.noTerroristPage = res1.getString("_noTerroristVote_htm"); } /** * Method used to compute the length of the image bar to be displayed for each * option in the resulting html file. * @param i The value of an option (current amount of votes for this option) * @return The length in pixel of the image bar to be displayed */ private int calcolaMoltiplicatorePercentuale(int i){ int u = i; if(totale!=0) u *= (440/totale); return u; } /** * Method that saves all data in the default file. */ private synchronized void salvaDati(){ File f = new File(DBFILE); /*try{ f.createNewFile(); } catch(IOException e){ temp = e.toString(); }*/ FileWriter fileWriter = null; PrintWriter printWriter = null; try{ fileWriter = new FileWriter(f); printWriter = new PrintWriter(fileWriter); printWriter.println(totale); for(int i=0;i<arrayValori.length;i++){ printWriter.println(arrayValori[i]); } } catch(IOException e){ System.err.println("Error While writing file .nk"); temp = e.toString(); } catch(Exception e){ temp = e.toString(); } if(printWriter!=null) printWriter.close(); } /** * This method loads data from the dbfile */ private synchronized void caricaDati(){ FileReader fileReader =null; BufferedReader bufferedReader = null; try{ fileReader = new FileReader(DBFILE); bufferedReader = new BufferedReader(fileReader); String dati = bufferedReader.readLine(); if(dati!=null && dati!="") totale = Integer.parseInt(dati); int i=0; dati = bufferedReader.readLine(); while (dati!=null){ arrayValori[i] = Integer.parseInt(dati); i++; dati = bufferedReader.readLine(); } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -