很多人都想办法了,可找不到原因!
MathBeab.java
___________________________________________________
package test;
public class MathBean {
private String a;
private String b;
private double result;
private String operator;
public String getA() {
if(a==null)
a= " ";
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
if(b==null)
b= " ";
return b;
}
public void setB(String b) {
this.b = b;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public double getResult() {
return result;
}
public void forResult() {
double one=Double.parseDouble(a);
double two=Double.parseDouble(b);
try {
if (this.equals( "+ ")) result=one+two;
else if (this.equals( "- ")) result=one-two;
else if (this.equals( "* ")) result=one*two;
else if (this.equals( "/ ")) result=one/two;
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}
___ForRrsult.JSP________________________________________
<%@ page language= "java " contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<jsp:useBean id= "forResult "class= "test.MathBean "scope= "request ">
<jsp:setProperty name= "forResult " property= "* "/>
</jsp:useBean>
<html>
<head>
<meta http-equiv= "content-type " content= "text/html; charset=gb2312 "/>
<title> 一简单的计数器 </title>
</head>
<body> <center>
<form action= "forResult.jsp " method= "POST ">
<table border= "1 " width= "300 "bgcolor= "#ffffc0 ">
<tr> <td> 计算结果 </td>
<%
try{forResult.forResult();}
catch(Exception e){e.printStackTrace();}%>
<td> <%=forResult.getA()+forResult.getOperator()+forResult.getB()%> =
<%=forResult.getResult()%> </td> </tr>
<tr> <td> 第一个数 </td>
<td> <input type= "text "name= "a "> </td> </tr>
<tr> <td> 选择操作 </td>
<td> <SELECT name= "operator ">
<OPTION value= "+ "> + </OPTION>
<OPTION value= "- "> - </OPTION>
<OPTION value= "* "> * </OPTION>
<OPTION value= "/ "> / </OPTION>
</SELECT> </td>
</tr>
<TR> <TD> 第二个数 </TD>
<td> <INPUT type= "text "name= "b "> </td> </TR>
<TR> <TD> <INPUT type= "submit "value= "确定 "> </TD>
<td> <INPUT type= "reset "value= "取消 "> </td> </TR>
</table>
</form>
</center>
This is my JSP page. <br>
</body>
</html>
---------------------------------------------------
页面一出来就出现
计算结果:null+null=null
.....
.....
当我提交时就出现404错误!
请问这个怎么的? 这样解决呢?
[解决办法]
在调用这个方法前.你必须先判断,什么时候去调用你这个方法,并不是一开始就调用的forResult.forResult();
如果你想在文本框中输入a,b的值后再去调用这个方法,你就必须写上
if(a != null && b != null){
forResult.forResult();
}
你文本框中得到的a,b的值你方法中并没有得到这2个值去执行,也就是你没有传过去值...