做了一个用户调查小程序,在累积调查结果时用JSP+JAVABEAN来实现:代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.io.*"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<jsp:useBean id="votes" class="votes.vote" scope="request"/>
<body>
<%
String tou=request.getParameter("tou");
votes.checkFile();
votes.setString();
votes.readFile(tou);
votes.getString();
%>
<a href="vote.jsp">感谢您投票,返回</a>
</body>
</html>
却不能实现结果

解决方案 »

  1.   

    我的JAVABEAN程序如下:
    package votes;
    import java.io.*;
    public class vote
    { public int n=5;
    public String ms[]=new String[10];
    public int gs[]=new int[10];
    public BufferedReader file;
    public String filePath="E:\\Tomcat 5.0\\webapps\\jsp-examples\\onlineOvte\\vote.txt";
    public PrintWriter pw;
    public void setString()throws Exception{

    file=new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
    for(int i=0;i<n;i++){

    ms[i]=file.readLine();


    }
    for(int i=0;i<n;i++){
    gs[i]=Integer.parseInt(ms[i]);
    System.out.println(ms[i]);
    }

    }
    public void checkFile()throws Exception{
    File files=new File(filePath);

    if(files.exists()==false){
    pw=new PrintWriter(new FileOutputStream(filePath));
    for(int i=0;i<n;i++){
    pw.println(0);
    }
    pw.close();
    }
    }
    public void readFile(String ch){
    String gf=ch;
    if(gf!=null&&gf.compareTo("0")==0)gs[0]++;
    if(gf!=null&&gf.compareTo("1")==0)gs[1]++;
    if(gf!=null&&gf.compareTo("2")==0)gs[2]++;
    if(gf!=null&&gf.compareTo("3")==0)gs[3]++;
    if(gf!=null&&gf.compareTo("4")==0)gs[4]++;
    }

    public void getString()throws Exception{

    pw=new PrintWriter(new FileOutputStream(filePath));
    for(int i=0;i<n;i++){
    pw.println(gs[i]);
    System.out.println(gs[i]);
    }
    pw.close();
    }
    public static void main(String args[])throws Exception{
    vote vot=new vote();

    vot.checkFile();
    vot.setString();
    vot.readFile("0");
    vot.getString();

    }
    }
      

  2.   

    经测试JAVABEAN程序是正确的,我又将获取提交的JSP程序改为如下却可以实现累加改动后的DOVOTE。JSP程序如下:
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <%@ page import="java.io.*"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <jsp:useBean id="votes" class="votes.vote" scope="request"/>
    <body>
    <%
    String tou=request.getParameter("tou");
    votes.checkFile();
    votes.setString();
    if(tou!=null&&tou.compareTo("0")==0)
    votes.gs[0]++;
    if(tou!=null&&tou.compareTo("1")==0)
    votes.gs[1]++;
    if(tou!=null&&tou.compareTo("2")==0)
    votes.gs[2]++;
    if(tou!=null&&tou.compareTo("3")==0)
    votes.gs[3]++;
    if(tou!=null&&tou.compareTo("4")==0)
    votes.gs[4]++;
    votes.getString();
    %>
    <a href="vote.jsp">感谢您投票,返回</a>
    </body>
    </html>
    这个程序可以实现功能,我不明白的是我把其中的
    if(tou!=null&&tou.compareTo("0")==0)
    votes.gs[0]++;
    if(tou!=null&&tou.compareTo("1")==0)
    votes.gs[1]++;
    if(tou!=null&&tou.compareTo("2")==0)
    votes.gs[2]++;
    if(tou!=null&&tou.compareTo("3")==0)
    votes.gs[3]++;
    if(tou!=null&&tou.compareTo("4")==0)
    votes.gs[4]++;
    在JAVABEAN中用方法实现然后调用为什么却不行呢??
    非常奇怪的问题,我百思不得解,请高手指点
      

  3.   

    表单程序如下:VOTE。JSP
    <%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    <h1 align="center">市场调查<br />
    </h1>
    <form id="form1" name="form1" method="post" action="dovote.jsp">
      <div >
        <input type="radio" name="tou" value="0" />
      每台电脑在15000元以上
      <br />
       <input type="radio" name="tou" value="1" />
      每台电脑在15000-10000元之间<br />
      <input type="radio" name="tou" value="2" checked/>
      每台在10000-8000元之间<br />
      <input type="radio" name="tou" value="3" />
      每台在8000-5000之间<br />
      <input type="radio" name="tou" value="4" />
      每台在5000元以下<br />
      <input type="submit" name="Submit" value="提交" />
      <input name="vote" type="button" id="vote" value="查看" />
      </div>
    </form>
    </body>
    </html>
    以上是整个程序的原代码,希望大伙帮我找找原因,保证问题已解决就结贴,可以再加分
      

  4.   

    在你的bean里面加上空构造
    public vote(){
    }
      

  5.   

    不需要的,这个问题已经解决了,其实代码没有问题,就是我的tomcat没有部署热部署