为什么运行不了呢?
这是一个简单的小程序,为什么总是提示<jsp:useBean id="calculator" scope=""request class="myjsp.simplecalculate">中的 id="calculator"错误,而且程序中的所有的calculator都
会报错,这是怎么一回事?请求高手解答,谢谢
calculate.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html; charset=utf-8" %>
<jsp:useBean id="calculator" scope=""request class="myjsp.simplecalculate">
<jsp:setProperty name="calculator" property="*"/>
</jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>简单计算器</title>
<meta http-equiv="Content-Type content="text/html;charset=gb2312">
</head>
<body>
<p align=center><b>简单计算器</b></p>
<hr>
计算结果:<%
try
{
calculator.calculate();
out.println(calculator.getFiirst()+calculator.getOperator()+calculator.getSecond()+"="+calculator.getResult());
}
catch(Exception e){out.println(e.getMessage());}
%>
<hr>
<form action="calculate.jsp" method=get>
<table width="75%">
<tr>
<td align=right>第一个参数</td>
<td><input type=text name="second"></td>
</tr>
<tr>
<td align=right>操作符</td>
<td><select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</td>
</tr>
<tr>
<td align=right>第二个参数</td>
<td><input type=text name="second"></td>
</tr>
<tr>
<td></td>
<td><input type=sumbit value=计算></td>
</tr>
</table>
</form>
</body>
</html>
package myjsp;
public class simplecalculate {
private String first;
private String second;
private double result;
private String operator;
public void setFirst(String first){
this.first=first;
}
public void setSecond(String second){
this.second=second;
}
public void setOperator(String operator){
this.operator=operator;
}
public String getFirst(){
return this.first;
}
public String getSecond(){
return this.second;
}
public String getOperator(){
return this.operator;
}
public double getResult(){
return this.result;
}
public void calculator(){
double one=Double.parseDouble(first);
double two=Double.parseDouble(second);
try{
if(operator.equals("+"))result=one+two;
else if(operator.equals("-"))result=one+two;
else if(operator.equals("*"))result=one+two;
else if(operator.equals("/"))result=one+two;
}
catch(Exception e){System.out.println(e);}
}
}
[解决办法]
这个是笔误么?
<jsp:useBean id="calculator" scope=""request class="myjsp.simplecalculate">
[解决办法]
两个问题:
1、useBean 不能跟 <%%> 这种Java代码直接混用,因为作用域不同;
也就是你得:simplecalculate calculator = new simplecalculate();
2、拼写错误吧,你的函数名称是:
public void calculator()