⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 feedback.java

📁 wap push功能程序 WAP开发网 : http://www.wapkf.com
💻 JAVA
字号:
package feedback;import push.Winner;import push.PushInitiator;import members.Members;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;/** * Feedback class handles the feedback section of your WML home pages. * * Initialization parameters: *   "questionsfile" determines the name of the file containing your questions  *   "feedbackfile" determines the name of the file where the feedback is saved * *    The first line in your questionsfile contains the different choises the user can choose from *    if the line starts with a "!" the answer will be a String * * @version 1.0 * @since 1.0 * @see HttpServlet */public final class Feedback extends HttpServlet {    private static final String XML_VERSION = "<?xml version=\"1.0\"?>";    private static final String CONTENT_TYPE = "text/vnd.wap.wml";    private static final String DOC_TYPE = "<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\"\n" +	"  \"http://www.wapforum.org/DTD/wml_1.1.xml\">";    private static int numberOfQuestions=0;    private static String grades;    private static final Vector questions = new Vector();    private static final String HOME_PAGE = "/index.jsp";    private static String feedbackFile = null;    private static Winner winner = null;    /**     * This is the initialization code that runs only once. (when you start the servlet)     * The servlet is being placed into service.     */    public void init() throws ServletException {	String questionsFile = getInitParameter ("questionsfile");	questionsFile = getServletContext().getRealPath(questionsFile);	feedbackFile = getInitParameter ("feedbackfile");	feedbackFile = getServletContext().getRealPath(feedbackFile);	try {	    BufferedReader reader = new BufferedReader(new FileReader(questionsFile));	    grades = reader.readLine();	    String line;	    while((line = reader.readLine()) != null) {		if(line.trim().equals(""))  break;		questions.add(line);	    }	}	catch (java.io.IOException e) {	    System.err.println(e);	}	numberOfQuestions = questions.size();    }    /**     * Called by the server to allow a servlet to handle a GET request.      *     * @param request a <code>HttpServletRequest</code> value     * @param response a <code>HttpServletResponse</code> value     * @exception ServletException if an error occurs     * @exception IOException if an error occurs     */    public void doGet(HttpServletRequest request, HttpServletResponse response) 	throws ServletException, IOException {	doPost(request, response);    }    /**     * Called by the server to allow a servlet to handle a POST request.     *     * @param request a <code>HttpServletRequest</code> value     * @param response a <code>HttpServletResponse</code> value     * @exception ServletException if an error occurs     * @exception IOException if an error occurs     */    public void doPost(HttpServletRequest request, HttpServletResponse response) 	throws ServletException, IOException {	/* Session tracking. See the description in index.jsp */	HttpSession session = request.getSession();	boolean cookiesCheck = request.getHeader("cookie") != null && request.isRequestedSessionIdValid();	final String jsessionID = cookiesCheck ? "" : ";jsessionid=" + session.getId();	/* Initialize a winner selecting thread that selects a winner from the set of users who has given feedback.           It will send a push message to the winner */	if(winner == null) {	    PushInitiator pusher = Members.getInitiator();	    if(pusher != null) {		winner = new Winner(pusher, "http://" + request.getServerName() + ":" + 				    request.getServerPort() + request.getContextPath() + 				    "/services/winner.jsp", Members.getPushAddressType());		new Thread(winner).start();	    }	}	/* start generating the response ... */	response.setContentType(CONTENT_TYPE);	PrintWriter out = response.getWriter();      	out.println(XML_VERSION);	out.println(DOC_TYPE);	out.println("<wml>");	out.println("<!-- provides a way back using the prev element -->\n" +		    "<template>" +		    " <do type=\"prev\">" + 		    "  <prev/>" +		    " </do>" +		    "</template>");	if(request.getParameter("action") == null) {	    out.println("<!-- questions card -->\n" +			"<card title=\"Feedback\" id=\"questionsCard\">" +			"<do type=\"accept\" label=\"Send form\">" +			"<go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">");	    for (int i = 0; i < numberOfQuestions; ++i)		out.println("<postfield name=\"answer" + i + "\" value=\"$(choise" + i + ")\"/>");	    out.println("<postfield name=\"action\" value=\"write\"/>");	    out.println("<postfield name=\"sendname\" value=\"$(sendname)\"/>");	    out.println("</go></do>" +			"<p>Please grade our mobile service:<br/>");	    String gradesOut = "";	    StringTokenizer st = new StringTokenizer(grades);	    for(int j=1; st.hasMoreTokens(); ++j)		gradesOut = gradesOut + "<option value=\"" + j + "\">" + st.nextToken() + "</option>\n";	    for (int i=0; i < numberOfQuestions; ++i) {		String question = (String)questions.get(i);		if (question.startsWith("!")){		    out.println(question.substring(1) + "&#160;");		    out.println("<input name=\"choise" + i + "\"/>");		}		else{		    out.println(question + "&#160;");		    out.println("<select value=\"Empty\" title=\"" + question + "\" name=\"choise" + i + "\">");		    out.println("<option value=\"Empty\">Select</option>");		    out.println(gradesOut);		    out.println("</select>");		}	    }	    if (session.getAttribute("name") != null){ 		out.println("<select name=\"sendname\" value=\"yes\">" + 			    "<option value=\"yes\">Include name</option>" +			    "<option value=\"no\">Do not include name</option>" +			    "</select>");	    } 	    out.println("<anchor title=\"Send form\">Send form" +			"<go method=\"post\" href=\"" + request.getRequestURI() + jsessionID + "\">");	    for (int i = 0; i < numberOfQuestions; ++i)		out.println("<postfield name=\"answer" + i + "\" value=\"$(choise" + i + ")\"/>");	    out.println("<postfield name=\"action\" value=\"write\"/>");	    out.println("<postfield name=\"sendname\" value=\"$(sendname)\"/>");	    out.println("</go></anchor></p>" +			"</card>");	}	else {	    out.println("<!-- Thanks about the feedback -->\n" +			"<card title=\"Thank you\" id=\"thanks\">" +			" <do type=\"accept\" label=\"Ok\">" +			"  <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" +			" </do>" +			" <p>Your message has been delivered.<br/>Thank you for your feedback!<br/>" +			((session.getAttribute("name") != null) ?  			 "Note! Each member feedback (if name included) enters a draw for wonderful prizes.<br/>" : "") +			" <anchor title=\"Info pages\">Info pages" +			"  <go href=\"" + request.getContextPath() + HOME_PAGE + jsessionID + "\"/>" +			" </anchor>" +			" </p>" +			"</card>");	    /* finally, update the feedback file. */	    synchronized (feedbackFile) {		PrintWriter writer = new PrintWriter(new FileOutputStream(feedbackFile, true), true);		for (int i = 0; i < numberOfQuestions; ++i){		    String answer = request.getParameter("answer" + i);		    writer.print(answer + ";");		}		if (request.getParameter("sendname") != null && request.getParameter("sendname").equals("yes")) 		    writer.println((String)session.getAttribute("name"));		else 		    writer.println();		writer.flush();	    }	}	out.println("</wml>");    }    /**     * <code>viewVotes</code> views the information related to received feedbacks, i.e.,      * average grades for each question.     *     * @return a <code>String</code> value     */    public static String viewVotes() {	String returnStr = "";	try {	    int numberOfLines = 0;	    double[] stats = new double[numberOfQuestions];	    int[] empty = new int[numberOfQuestions];	    synchronized(feedbackFile) {		BufferedReader reader = new BufferedReader(new FileReader(feedbackFile));		StringTokenizer st = null;		String line;		while ((line = reader.readLine()) != null){		    st = new StringTokenizer(line, ";");		    for (int i = 0; i < numberOfQuestions; ++i){			String token = st.nextToken();			if (!(token.equals("Empty"))) stats[i] = stats[i] + Integer.parseInt(token);			else ++empty[i];		    }		    ++numberOfLines;		}	    }	    for (int j = 0; j < numberOfQuestions; ++j)		returnStr = returnStr + questions.get(j) + ": " + 		    Math.rint(100.0*stats[j] / (numberOfLines - empty[j]))/100.0 + "<br/>";	    returnStr = returnStr + "Votes: " + numberOfLines;	}	catch(Exception e){ System.err.println(e); }	return returnStr;    }   /**     * Selects winner from a set of users who have returned feedback     *     * @param feedbackFile a <code>String</code> value     * @return a <code>String</code> value     */    public static String winner() {	try{	    synchronized(feedbackFile) {		BufferedReader reader = new BufferedReader(new FileReader(feedbackFile));		int numberOfPossibilities = 0;		while (reader.readLine() != null) ++numberOfPossibilities;		int winner = 1 + (int)(Math.random() * numberOfPossibilities);		String winnerStr ="";		reader = new BufferedReader(new FileReader(feedbackFile));		for (int i = 0; i < winner; ++i) winnerStr = reader.readLine();		StringTokenizer st = new StringTokenizer(winnerStr, ";");		while (st.hasMoreTokens()) winnerStr = st.nextToken();		return winnerStr;	    }	}	catch(FileNotFoundException e){}	catch(IOException e){}	return "";    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -