本文主要是介绍WAP手机上的问卷调查系统的构建,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
普通的网页问卷调查系统大家一定都见过,但是大家有没有试过在WAP上进行问卷调查呢?估计大部分的朋友都没有见过,那就让我们来写一个吧!
想一想怎么实现这个系统呢?首先建立一个页面,显示将就哪一个问题进行调查或投票,一般是出现一个复选框,给出问题和若干选项,服务器收集投票,存入日志文件或存入数据库,并能显示问卷调查结果,这就是一个问卷调查系统的构思。其实在WAP手机上也同样用这种思想来构建问卷调查系统,但是必须顾及手机的特点:显示面积小,且要结合WML编程。
我在下面给出了一个相当简单的手机问卷调查系统的Java Servlet编写的,为了简单起见我没有使用数据库而是使用了一个日志文件存放投票信息,其实这个程序的主要目的还是为了让大家看看Java编程的思想,就是结构化和可重用性。
现在我把这个程序的使用方法介绍一下,编译程序WapVoteServlet.class以后,放入运行的Java Servlet目录下。你可以使用自己的WML素材来编写WML页面,只是把WapVoteServlet作为存储和浏览结果的一段脚本程序;当然,如果你对WML不是非常熟悉的话,你也可以用本程序来生成调查问卷。下面我将就第二种用法来介绍,第一种用法请朋友们自己参阅WML相应的资料。
用法是: http://your_wap_host/servlet/WapVoteServlet?config=config_file ?act=vote
用于提交投票或调查选项
http://your_wap_host/servlet/WapVoteServlet?config=config_file ?act=log
用来显示投票或调查结果
http://your_wap_host/servlet/WapVoteServlet?config=config_file ?act=view
用来生成调查,可以完全不知道如何编写WML
配置文件中的参数的详解:
log=your_file_is_here
log文件是用来存储投票或调查结果的文件,这个参数是强制的,必须写出它所在的路径
after=http://your_wap_host/your_page.htm
after为用户提交投票或调查后所显示的页面,默认为当前的投票结果
cookies=0
使用cookie是为了防止用户多次投票,默认值为0 即不使用cookie,cookies=1为使用
bgcolor=#FFFFFF
背景颜色(默认为白色)
fgcolor=#000000
前景颜色(默认为黑色)
size=2
字体大小
face=Verdana,Arial
默认字体
votecolor=#FF0000
投票结果是以棒状图显示出来,所以必须定义棒的颜色
title=Your Survey
你的调查的标题
options=Your option1,Your Option2,Your Option3
你的选项如对于天极网的喜好程度“ 喜欢,比较喜欢,不喜欢 ”,这个参数是强制参数,每个选项以逗号分开
column=1
选项在页面中排列的位置 column等于1表示在同一纵列,0表示在同一行
日志文件的格式是:
文本文件,用逗号隔开各个不同的值,每一行包括:客户机IP地址,日期和选项值 。
配置文件实例:
#
# vote config file
#
log=c:/catalina/logs/votelog.txt
after=c:/catalina/webapps/examples/servlet/vote.html
options=搜狐,新浪,网易
column=0
title=您喜欢哪一个门户网站
cookies=1
bgcolor=#FFFFFF
fgcolor=#000000
size=2
face=Verdana,Arial
votecolor=#FF0000
现在让我们来看一看源程序吧:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WapVoteServlet extends HttpServlet
{
public WapVoteServlet()
{
}
private static final String CONFIG = "config";
private static final String ACTION = "act";
private static final String VOTE = "vote";
private static final String LOG = "log";
private static final String AFTER = "after";
private static final String VIEW = "view";
private static final String COOKIES = "cookies";
private static final String BGCOLOR = "bgcolor";
private static final String FGCOLOR = "fgcolor";
private static final String SIZE = "size";
private static final String FACE = "face";
private static final String TITLE = "title";
private static final String COLUMN = "column";
private static final String VOTECOLOR = "votecolor";
private static final String DEFBGCOLOR = "#FFFFFF";
private static final String DEFFGCOLOR = "#000000";
private static final String DEFVOTECOLOR = "#FF0000";
private static final String DEFCOOKIES = "0";
private static final String DEFCOLUMN = "1";
private static final String DEFTITLE = "A Free & Simple Vote System";
private static final String OPTIONS = "options";
private static final String EDITED = "edited";
private static final String FICT = "fct";
private static final String WAPVOTE = "wpv";
private static final int MAX_WML = 900;
private static final int MAX_VOTES = 20;
private static String NEWLINE = "/n";
private static Hashtable cfgs;
private static Hashtable forLock;
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
NEWLINE = System.getProperty("line.separator");
cfgs = new Hashtable();
forLock = new Hashtable();
}
file:// 由于使用POST发送表单,所以现用doPost来处理POST请求
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response); file://调用doGet去处理POST请求
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
file:// 用于处理GET请求
throws ServletException, IOException
{
String s = "";
String s1 = "";
s = HttpUtils.getRequestURL(request).toString();// 把收到的请求转化成字符串
int i;
if((i = s.indexOf("?")) > 0) file:// 想一想为什么要这么写?
s = s.substring(0, i);
s1 = request.getQueryString(); file:// 取的请求的字符串
if(s1 == null)// 如果为空,既是没有写上配置文件名,故要发出错误信息
{
errorMessage("不能读到配置文件", null, request, response);
return;
}
String s2 = getFromQuery(s1, "config=");// 读取请求中"&"后的字符串
if(s2.length() == 0)
s2 = s1;
String s3 = getFromQuery(s1, "act=");
Hashtable hashtable = getConfig(s2);// 读取配置文件
if(hashtable.get("log") == null)// 如果配置文件中没有log参数,则出现错误信息
{
errorMessage("不能从你的配置文件中发现日志文件名!", hashtable, request, response);
return;
}
if(s3.length() == 0) file:// s3为act后的字符串
s3 = "vote";
if(((String)hashtable.get("cookies")).equals("1") && s3.equals("vote"))
{
Cookie acookie[] = request.getCookies(); file:// 设立cookie是为了防止用户多次投票
file:// 下面的循环是为了能找出你是否已经投过票
if(acookie != null)
{
for(int j = 0; j < acookie.length; j++)
{
Cookie cookie = acookie[j];
if(s2.equals(cookie.getName()))
{
errorMessage("你的投票被取消了", hashtable, request, response);
return;
}
}
}
Cookie cookie1 = new Cookie(s2, "yes");
cookie1.setMaxAge(0x15180);
response.addCookie(cookie1);
}
if(s3.equals("vote"))
{
takeVote(s, s2, hashtable, request, response);
return;
}
if(s3.equals("log"))
{
showLog(s, hashtable, request, response);
return;
}
if(hashtable.get("options") == null)
{
errorMessage("不能读入你的配置值", hashtable, request, response);
return;
}
else
{
showVote(s, s2, hashtable, request, response);
return;
}
}
private void readConfig(String s, Hashtable hashtable) file:// 读取配置文件
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(s)));
String s1;
while((s1 = reader.readLine()) != null) file:// 从配置文件中读入一行参数字符串
{
s1 = s1.trim(); file:// 移去s1中的空格
if(s1.length() > 0)
{
int i = s1.indexOf("=");// 在s1中寻找“=”,i为被=分成的段数
if(i > 0 && i < s1.length() - 1 && s1.charAt(0) != '#' && !s1.startsWith("//"))
file:// 参数的第一个字符不能为#和//
hashtable.put(s1.substring(0, i).trim(), s1.substring(i + 1).trim());
file:// 把等号前后的字符串分别存入哈西表中
}
}
reader.close();
File file = new File(s);
hashtable.put("edited", String.valueOf(file.lastModified()));
}
catch(Exception _ex) { }
if(hashtable.get("bgcolor") == null)
hashtable.put("bgcolor", "#FFFFFF"); file:// 向哈西表中写入默认的背景色
if(hashtable.get("fgcolor") == null)
hashtable.put("fgcolor", "#000000"); file:// 向哈西表中写入默认的前景色
if(hashtable.get("column") == null)
hashtable.put("column", "1"); file:// 向哈西表中写入默认的column值
if(hashtable.get("cookies") == null)
hashtable.put("cookies", "0"); file:// 向哈西表中写入默认的cookies值为0
if(hashtable.get("title") == null)
hashtable.put("title", "A Free & Simple Vote System");
file:// 向哈西表中写入默认的标题
if(hashtable.get("votecolor") == null)
hashtable.put("votecolor", "#FF0000"); file:// 向哈西表中写入默认投票色
hashtable.put("fct", new Integer(0));
}
private Hashtable getConfig(String s) file:// 打开配置文件
{
Hashtable hashtable;
if((hashtable = (Hashtable)cfgs.get(s)) != null)
{
File file = new File(s);
String s1 = (String)hashtable.get("edited");
if(s1.equals(String.valueOf(file.lastModified())))
return hashtable; file:// 如果文件被编辑过,则返回哈西表
cfgs.remove(s);
hashtable = null;
}
hashtable = new Hashtable();
readConfig(s, hashtable);
cfgs.put(s, hashtable);
String s2 = (String)hashtable.get("log");
if(s2 != null && forLock.get(s2) == null)
forLock.put(s2, new Object());
return hashtable;
}
private void showVote(String s, String s1, Hashtable hashtable, HttpServletRequest request, HttpServletResponse response)
throws IOException
{
String s2 = (String)hashtable.get("options");
String s3 = (String)hashtable.get("column");
String s4 = (String)hashtable.get("title");
String s6 = getFont(hashtable);
String s7 = "";
String s8 = "";
if(request.getHeader("Accept").indexOf("wap.wml") >= 0)
PrintWriter OUT;
response.setContentType("text/vnd.wap.wml");
OUT = response.getWriter();
OUT.println("BR>
OUT.println("<!DOCTYPE wml PUBLIC /"-//WAPFORUM//DTD WML 1.1//EN/" /"http://www.wapforum.org/DTD/wml_1.1.xml/">");
OUT.println("<wml>");
OUT.println("<CARD id='/"showvote/"' title='/""' ?/? + (String)hashtable.get(?title?)>");
OUT.println("<DO label='/"Vote/"' type='/"accept/"'>");
OUT.println("<GO ?/? + method='/"post/"' vote? ?=" + s1 + " ?act? &? ?config? ??? s href='/""'>");
OUT.println("<POSTFIELD value='/"$(sVote)/"/' name='/"wpv/"'>");
OUT.println("</GO>");
OUT.println("</DO>");
OUT.println("<P mode='/"nowrap/"'><B>" + s4 + "</B>
");
OUT.println(" <P mode='/"nowrap/"'>");
OUT.println("BR>
StringTokenizer str = new StringTokenizer(s2, ",");
while(str.hasMoreTokens())
{
String s5 = str.nextToken();
OUT.println("BR>
OUT.println("</SELECT>");
OUT.println("
");
OUT.println("</CARD>");
OUT.println("</WML>");
OUT.flush();
OUT.close();
}
private void takeVote(String s, String s1, Hashtable hashtable, HttpServletRequest request, HttpServletResponse response)
throws IOException
{
String s2 = (String)hashtable.get("log");
String s3 = (String)hashtable.get("after");
String s6 = "";
Enumeration enumeration = request.getParameterNames();
if(request.getHeader("Accept").indexOf("wap.wml") >= 0)
file://判断是否为WAP设备接收
{
s6 = request.getParameter("wpv");
if(s6 == null)
s6 = "";
}
if(s6.length() > 0)
writeLog(hashtable, request, s6, s2);
if(s3 == null) file://如果s3为空,则显示日志文件
{
showLog(s, hashtable, request, response);
return;
}
else file://否则发送重定向
{
String s4 = s + "?" + "config" + "=" + s1 + "&" + "act" + "=" + "log";
response.sendRedirect(s4);
return;
}
}
private void writeLog(Hashtable hashtable, HttpServletRequest request, String s, String s1)//写日志文件
{
String s2 = request.getRemoteAddr(); file://取得客户机的IP地址
try {
synchronized(forLock.get(s1))
file://注意:使用synchronized关键字,说明同一时间只能有一个写动作,其余的要在队列中等待
{
int i = ((Integer)hashtable.get("fct")).intValue();
file://取得日志文件的fct值
if(i >= 20)
{
return;
}
hashtable.put("fct", new Integer(i + 1));//把fct值加1放回哈希表
FileWriter filewriter = new FileWriter(s1, true);
PrintWriter OUT = new PrintWriter(filewriter);
OUT.println((s2 != null ? s2 : "未知地址") + "," + new Date() + "," + s);
OUT.flush();
OUT.close();
}
return;
}
catch(IOException _ex)
{
return;
}
}
private void errorMessage(String s, Hashtable hashtable, HttpServletRequest request, HttpServletResponse response)//输出错误信息
throws IOException
{
PrintWriter OUT = null;
response.setContentType("text/vnd.wap.wml");//设置为WML文件格式
OUT = response.getWriter();
OUT.println("");
OUT.println("");
OUT.println("<WML>");
OUT.println("");
OUT.println(" " + s + "");
OUT.println("");
OUT.println("");
OUT.flush();
OUT.close();
}
private void showLog(String s, Hashtable hashtable, HttpServletRequest request, HttpServletResponse response)
throws IOException
{
String s1 = (String)hashtable.get("log");
String s2 = getFont(hashtable);
Vector vector = new Vector();
Hashtable hashtable1 = new Hashtable();
StringBuffer stringbuffer = new StringBuffer("");
long l = 0L;
String s7 = (String)hashtable.get("title");
try {
synchronized(forLock.get(s1))
{
BufferedReader bufferedreader = new BufferedReader(new putStreamReader(new FileInputStream(s1)));
String s3;
while((s3 = bufferedreader.readLine()) != null) file://读入一条参数
vector.addElement(s3); file://作为一个元素加入向量中
bufferedreader.close();
}
}
catch(Exception _ex) { }
if(request.getHeader("Accept").indexOf("wap.wml") >= 0)
for(int j = 0; j < vector.size(); j++)
{
String s4 = (String)vector.elementAt(j); file://取得向量中第j+1个元素
int i = s4.indexOf(","); file://i表示s4被逗号“,”分隔开的段数
if(i > 0 && i != s4.length() - 1)
s4 = s4.substring(i + 1);
i = s4.indexOf(",");
if(i > 0 && i != s4.length() - 1)
s4 = s4.substring(i + 1);
Long long1;
if((long1 = (Long)hashtable1.get(s4)) == null)
{
hashtable1.put(s4, new Long(1L));
}
else
{
hashtable1.remove(s4);
hashtable1.put(s4, new Long(long1.longValue() + 1L));
}
l++;
}
vector = null;
PrintWriter OUT;
response.setContentType("text/vnd.wap.wml");
OUT = response.getWriter();
OUT.println("");
OUT.println("");
OUT.println("");
OUT.println("<CARD id='/"voteres/"' title='/""' ?/? + (String)hashtable.get(?title?)><P>");
if(hashtable1.size() == 0)
OUT.println(" 我们现在还未收到任何投票");
else
{
OUT.println(" " + s7 + "");
while(hashtable1.size() > 0)
{
Enumeration enumeration = hashtable1.keys();
long l1 = 0L;
String s6 = null;
while(enumeration.hasMoreElements())
{
String s5 = (String)enumeration.nextElement();
long l2 = ((Long)hashtable1.get(s5)).longValue();
if(l2 > l1)
{
s6 = s5;
l1 = l2;
}
}
hashtable1.remove(s6);
stringbuffer.append(" " + s6 + "" + NEWLINE);
stringbuffer.append(" " + l1 + " (" + formatValue((float)((100D * (double)(float)l1) / (double)(float)l)) + "%)" + NEWLINE);
if(stringbuffer.length() > 900){
break;
}
OUT.println(stringbuffer.toString());
}
OUT.println("");
OUT.println("");
hashtable1 = null;
OUT.flush();
OUT.close();
}
private String getFont(Hashtable hashtable)
{
String s1 = "
String s;
if((s = (String)hashtable.get("face")) != null){
s1 = s1 + " face=/"" + s + "/"";
if((s = (String)hashtable.get("size")) != null){
s1 = s1 + " size=/"" + s + "/"";
return s1 + ">";
}
private String getFromQuery(String s, String s1) file://从s中找出s1
{
if(s == null)
return "";//如果s为空,当然返回“”
int i;
if((i = s.indexOf(s1)) < 0)//如果s中找不到s1,也返回空值
return "";
String s2 = s.substring(i + s1.length());
file://把s1所表示字符串后的值赋予s2,Config=conf_file&act=vote
if((i = s2.indexOf("&")) < 0) file://如果s2中没有&,则返回s2的值
return s2;
else
return s2.substring(0, i); file://否则返回&后的字符串
}
private String formatValue(float f)
{
String s = String.valueOf(f);
int i = s.indexOf(".");
if(i > 0 && i + 4 < s.length() && s.indexOf("e") < 0 && s.indexOf("E") < 0){
s = s.substring(0, i + 4);
return s;
}}</SELECT>
这篇关于WAP手机上的问卷调查系统的构建的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!