当前位置:编程学习 > JSP >>

jsp 和javabean结合的程序有问题

javabean部分 package javabean; import java.util.*; public class GuessGame { int answer; int guess; boolean success; String info; int counter; public GuessGame() { reset(); } public void setGuess(String guess) { counter++; try { this.guess = Integer.parseInt(guess); } catch (NumberFormatException e) { this.guess = -1; } if (this.guess == answer) { success = true; } else if (this.guess == -1) { info = "出错,再猜一次!"; } else if (this.guess < answer) { info = "您猜的价格小了!"; } else if (this.guess > answer) { info = "您猜的价格大了!"; } if(this.guess >1000){ info="请输入1到1000之间的数字!!"; } } //返回值 public boolean getSuccess() { return success; } //获得信息 public String getInfo() { return info; } //获得计数器值 public int getCounter() { return counter; } //获得答案 public int getAnswer(){ return answer; } public void reset() { answer = Math.abs(new Random().nextInt() % 1000) + 1; success = false; counter = 0; } } JSP部分: <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <%@page contentType="text/html,charset=gb2312" %> <%@page import="javabean.GuessGame"%> <html> <body> This is my JSP page. <br> <jsp:useBean id= "guessgame" class="GuessGame" scope="session" > </jsp:useBean> <jsp:setProperty name="guessgame" property="*" /><br> <% if(game.getCounter()==0) { %> <center> <font size = "4" color = "red">猜价格</font> <hr> <p align="center"><img src = "image.cake.jpg"alt="蛋糕"width="400"height="300"border="3"></p> <form method = "get"> <b>请输入价格</b> <input type ="text" name ="guess"> <input type = "submit" value="确定"> </form></center> <% } else if(guessgame.getSuccess()) { %> <center><b>猜对了,它归你了。你猜了<%=guessgame.getCounter() %>次</b> <br> <a href = "testGuest.jsp">再来一次?</a></center> <% guessgame.reset(); } else{ %> <form method ="get"> <center><b> 继续努力,<%= guessgame.getInfo() %> 你已经猜了 <%= guessgame.getCounter()%>次 <br> <input type = "text" name="guess"> <input type = "submit" value="确定"> 输入你猜的价格 </b></center> </form> <% } %> </body> </html>
答案:有可能
1:通过name查找,有重复
2:添加的点击事件的写法,当前浏览器不支持
其他:<jsp:useBean id= "guessgame" class="javabean.GuessGame" scope="session" >
if(guessgame.getCounter()==0)

好像还有错误,我没找出来,你仔细看看代码 
<%@page contentType="text/html;charset=gb2312" %>
这个中间是“;”,不是逗号,终于找到了 

上一个:jsp 如何限制表单,实现只能填写特定的数据。
下一个:从数据库里取出的数据如何传递到另外的jsp页面中

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,