作业题目:
1、编写JavaBean:保存三角形信息
2、三个页面:
第一个:输入三角形三条边的信息给JavaBean
第二个:获得信息存入JavaBean,有个超链接,跳转到第三个页面
第三个:显示三角形的三条边长
显示是否构成三角形,如果构成,输出周长和面积。
思想:太简单了故不需要思想。(难以启齿)
javabean:(triangle.java)
package anyi; import java.lang.Math; public class triangle { private double l1; private double l2; private double l3; private double p; private double area; private double lang; private String f; public triangle() { } public String getF() { if (l1 + l2 > l3 && l1 + l3 > l2 && l2 + l3 > l1) { f = "是"; p = (l1 + l2 + l3) / 2; } else f = "否"; return f; } public double getL1() { return l1; } public void setL1(double l1) { this.l1 = l1; } public double getL2() { return l2; } public void setL2(double l2) { this.l2 = l2; } public double getL3() { return l3; } public void setL3(double l3) { this.l3 = l3; } public double getarea() { area=-1; if (f.equals("是")) this.area = Math.sqrt(p * (p - l1) * (p - l2) * (p - l3)); return area; } public double getlang() { lang=-1; if (f.equals("是")) this.lang = l1 + l2 + l3; return lang; } }
第一个JSP页面:(first.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="anyi.triangle" %> <jsp:useBean id="p1" class="anyi.triangle" scope="session"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 输入三角形三边长吧:<br><br> <form name="p1" method="post" action="second.jsp"> 第一条:<input type="text" name="l1"><br><br> 第二条:<input type="text" name="l2"><br><br> 第三条:<input type="text" name="l3"><br><br> <input type="submit" name="s1" value="确定"> <input type="reset" name="r1" value="重置"> </form> </body> </html>
第二个JSP页面:(second.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="anyi.triangle" %> <jsp:useBean id="p1" class="anyi.triangle" scope="session"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <%request.setCharacterEncoding("utf-8");%> <jsp:setProperty name="p1" property="*" /> <br><a href="thrid.jsp">点我跳转展示三角形信息喔!</a> </body> </html>
第三个JSP页面:(third.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="anyi.triangle" %> <jsp:useBean id="p1" class="anyi.triangle" scope="session"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 三角形信息如下:<br><br> 边一:<jsp:getProperty property="l1" name="p1"/><br><br> 边二:<jsp:getProperty property="l2" name="p1"/><br><br> 边三:<jsp:getProperty property="l3" name="p1"/><br><br> 是否构成三角形:<jsp:getProperty property="f" name="p1"/><br><br> <% if(p1.getF().equals("是")){ %> 三角形周长为:<jsp:getProperty property="lang" name="p1"/><br><br> 三角形面积为:<jsp:getProperty property="area" name="p1"/><br><br> <% } %> </body> </html>