1.先在eclipse 中新建一个项目,在项目的【Java Resources】目录下的【src】新建一个包(我这里的包名是Triangle),在此包中新建一个类(我这里的类名是Triangle),打开class输入以下代码:
package Triangle;
public class Triangle {
private int sideA;
private int sideB;
private int sideC;
private double area;
private boolean isTriangle;
public int getSideA() {
return sideA;
}
public void setSideA(int sideA) {
this.sideA = sideA;
}
public int getSideB() {
return sideB;
}
public void setSideB(int sideB) {
this.sideB = sideB;
}
public int getSideC() {
return sideC;
}
public void setSideC(int sideC) {
this.sideC = sideC;
}
public double getArea() {
double p=(sideA+sideB+sideC)/2.0;
area=Math.sqrt(p*(p-sideA)*(p-sideB)*(p-sideC));
return area;
}
public boolean isTriangle(){
if(sideA<sideB+sideC&&sideB<sideA+sideC&&sideC<sideA+sideB)
isTriangle=true;
else
isTriangle=false;
return isTriangle;
}
}
2.在项目中的【WebContent】下新建一个JSP File文件,在此JSP文件中输入以下代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean class="JavaBean.Triangle" id="tg"></jsp:useBean>
<jsp:setProperty property="*" name="tg"/>
<form action="#" method="get" name="tgInfo">
<p>边A长度:<input type="text" name="sideA"/></p>
<p>边B长度:<input type="text" name="sideB"/></p>
<p>边C长度:<input type="text" name="sideC"/></p>
<p><input type="submit" value="计算"/></p>
</form>
<%
if(tg.getSideA()==0||tg.getSideA()==0||tg.getSideA()==0){
%>
<p style="color:red;">三角形边长不能为零</p>
<%
}else{
%>
<h1>三角形信息</h1>
<p>边长A:<jsp:getProperty property="sideA" name="tg"/></p>
<p>边长B:<jsp:getProperty property="sideB" name="tg"/></p>
<p>边长C:<jsp:getProperty property="sideC" name="tg"/></p>
<%
if(tg.isTriangle()==true){
%>
<p>面积:<jsp:getProperty property="area" name="tg"/></p>
<%
}else{
%>
<p style="color:red;">不满足三角形</p>
<%
}
}
%>
</body>
</html>
运行结果如下: