普通的网页问卷调查系统大家一定都见过,但是大家有没有试过在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)
闽公网安备 35060202000074号