这是一个测试体重指数的程序
1.input.html
- <html>
- <head>
- <title>input.html</title>
- <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
- </head>
- <body>
- <form method = "get" action = "./calc.jsp" name = "testForm">
- 体重:<input type = "text" name = "weight" id = "weight" value = "60" size = 15 /><br>
- 身高:<input type = "text" name = "height" id = "height" value = "1.7" size = 15 /><br>
- <input type = "submit" value = "测试"/>
- </form>
- </body>
- </html>
2.calc.jsp
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <html>
- <head>
- <title> calculate </title>
- </head>
- <jsp:useBean id = "attribute" scope = "application" class = "pack.Attribute"/>
- <body>
- <%
- String height = new String(request.getParameter("height"));
- String weight = new String(request.getParameter("weight"));
- %>
- <jsp:setProperty name = "attribute" property = "height" value = "<%= height %>"/>;
- <jsp:setProperty name = "attribute" property = "weight" value = "<%= weight %>"/>;
- <jsp:forward page = "result.jsp"/>
- </body>
- </html>
3.result.jsp
- <%@ page contentType = "text/html; charset = GB2312" %>
- <html>
- <head>
- <title>My JSP starting page </title>
- </head>
- <jsp:useBean id = "attribute" scope = "application" class = "pack.Attribute"/>
- <body>
- <jsp:getProperty name = "attribute" property = "weightIndex"/>
- </body>
- </html>
4.Calculate.java
- package pack;
- public class Calculate {
- public int calc (double weight, double height)
- {
- //System.out.println(weight);
- //System.out.println(height);
- return (int) (weight / (height * height));
- }
- public String getWeightIndex (double weight, double height)
- {
- int index = calc(weight, height);
- //System.out.println(index);
- if (index >= 18 && index <= 25) {
- //return new String("正常体重");
- return new String("normal");
- } else if (index > 30) {
- //return new String("超重");
- return new String("overweight");
- } else if (index > 35) {
- return new String("a bit fat");
- } else if (index > 40) {
- return new String("too fat");
- }
- //return new String("瘦弱");
- return new String("too thin");
- }
- }
5.Attribute.java
- package pack;
- public class Attribute {
- double dWeight = 1.0;
- double dHeight = 1.0;
- int result = 0;
- String weightIndex = new String("no answer");
- String weight = new String("12345");
- String height = new String("12345");
- Calculate calculate = new Calculate();
- public String getWeight()
- {
- return weight;
- }
- public String getHeight()
- {
- return height;
- }
- public void setHeight(String height)
- {
- this.height = height;
- }
- public void setWeightIndex()
- {
- weightIndex = calWeightIndex(this.weight, this.height);
- }
- public String getWeightIndex()
- {
- return calWeightIndex(weight, height);
- }
- public String calWeightIndex(String weight, String height)
- {
- this.dHeight = Double.parseDouble(height);
- this.dWeight = Double.parseDouble(weight);
- //System.out.println(dHeight);
- return calculate.getWeightIndex(dWeight, dHeight);
- }
- /*
- public static void main (String[] argc)
- {
- Attribute attr = new Attribute();
- attr.setWeight("60");
- attr.setHeight("1.7");
- System.out.println(attr.getWeightIndex());
- }
- */
- }