遇到的问题:
jsp网页制作中,if和else以及带有return语句获取属性值为null,两个条件只有一个条件可运行成功。
就是我希望在网页中输出当矩形长宽一样和不一样时,周长的计算方式不一样。
Rectangle.java代码如下:
package red.star;
public class Rectangle {
double sideA=-1,sideB=-1;
double circumference;
boolean isSquare;
public void setSideA(double a) {
sideA=a;
}
public double getSideA() {
return sideA;
}
public void setSideB(double b) {
sideB=b;
}
public double getSideB() {
return sideB;
}
public boolean getIsSquare(){
if(sideA==sideB)
isSquare=true;
else
isSquare=false;
return isSquare;
}
public void setCircumference(double c) {
circumference=c;
if(isSquare==true){
c=sideA*4;
}else{
c=sideA+sideB;
}
}
public double getCircumference(){
return circumference;
}
}
home.jsp代码如下:
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<style>
#textStyle{
font-family:宋体;font-size:36;color:blue
}
</style>
<% request.setCharacterEncoding("utf-8");
%>
<jsp:useBean id="rectangle" class="red.star.Rectangle" scope="request"/>
<HTML><body id = textStyle bgcolor=#ffccff>
<form action="" method="post" >
输入矩形的两条边:
边A:<input type=text name="sideA" id = textStyle value=0 size=5/>
边B:<input type=text name="sideB" id = textStyle value=0 size=5/>
<br><input type=submit id = textStyle value="提交"/>
</form>
<jsp:setProperty name="rectangle" property="*"/>
矩形的两条边是:
<jsp:getProperty name="rectangle" property="sideA"/>,
<jsp:getProperty name="rectangle" property="sideB"/>,
<br><b>这是一个正方形吗?<jsp:getProperty name="rectangle" property="isSquare"/>
<br>它的周长是:<jsp:getProperty name="rectangle" property="circumference"/></b>
</body></HTML>
网页出错:
当输入的矩形边框长度不一样时,无法计算出矩形的周长。