JavaBean 在jsp的实现

这是一个测试体重指数的程序
1.input.html
  1. <html>
  2.   <head>
  3.     <title>input.html</title>
  4.     
  5.     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  6.   </head>
  7.   <body>
  8.       <form method = "get" action = "./calc.jsp" name = "testForm">
  9.         体重:<input type = "text"  name = "weight" id = "weight" value = "60" size = 15 /><br>
  10.         身高:<input type = "text"  name = "height" id = "height" value = "1.7" size = 15 /><br>
  11.         <input type = "submit" value = "测试"/>
  12.       </form>
  13.   </body>
  14. </html>
2.calc.jsp
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
  2. <html>
  3.     <head>
  4.         <title> calculate </title>
  5.     </head>
  6.     <jsp:useBean id = "attribute" scope = "application" class = "pack.Attribute"/>
  7.     
  8.     <body>
  9.         <%
  10.         String height = new String(request.getParameter("height"));
  11.         String weight = new String(request.getParameter("weight"));
  12.         %>
  13.         <jsp:setProperty name = "attribute" property = "height" value = "<%= height %>"/>;
  14.         <jsp:setProperty name = "attribute" property = "weight" value = "<%= weight %>"/>;
  15.         
  16.         <jsp:forward page = "result.jsp"/>
  17.         
  18.          
  19.     </body>
  20. </html>

3.result.jsp

  1. <%@ page contentType = "text/html; charset = GB2312" %>
  2. <html>
  3.   <head>
  4.     <title>My JSP  starting page </title>
  5.   </head>
  6.   
  7.   <jsp:useBean id = "attribute" scope = "application" class = "pack.Attribute"/>
  8.   
  9.   <body>
  10.     
  11.    <jsp:getProperty name = "attribute" property = "weightIndex"/>
  12.     
  13.   </body>
  14. </html>

4.Calculate.java

 

  1. package pack;
  2. public class Calculate {
  3.     
  4.     public int calc (double weight, double height)
  5.     {
  6.         //System.out.println(weight);
  7.         //System.out.println(height);
  8.         return (int) (weight / (height * height));
  9.     }
  10.       
  11.     public String getWeightIndex (double weight, double height)
  12.     {
  13.         int index = calc(weight, height);
  14.         //System.out.println(index);
  15.         if (index >= 18 && index <= 25) {
  16.             //return new String("正常体重");
  17.             return new String("normal");
  18.         } else if (index > 30) {
  19.             //return new String("超重");
  20.             return new String("overweight");
  21.         } else if (index > 35) {
  22.             return new String("a bit fat");
  23.         } else if (index > 40) {
  24.             return new String("too fat");
  25.         }
  26.         //return new String("瘦弱");
  27.         return new String("too thin");
  28.     }
  29.     
  30. }

5.Attribute.java

 

 

  1. package pack;
  2. public class Attribute {
  3.     double    dWeight = 1.0;
  4.     double    dHeight = 1.0;
  5.     int       result  = 0;
  6.     
  7.     String    weightIndex =  new String("no answer");
  8.     String    weight      =  new String("12345");
  9.     String    height      =  new String("12345");
  10.     Calculate calculate = new Calculate();  
  11.      
  12.     public String getWeight()
  13.     {
  14.         return weight;
  15.     }
  16.     
  17.     public String getHeight()
  18.     {
  19.         return height;
  20.     }
  21.     
  22.     
  23.     public void setHeight(String height)
  24.     {
  25.         this.height = height;
  26.     }
  27.     
  28.     public void setWeightIndex()
  29.     {
  30.         weightIndex = calWeightIndex(this.weight, this.height);
  31.     }
  32.     
  33.     public String getWeightIndex()
  34.     {
  35.         return calWeightIndex(weight, height);
  36.     }
  37.     
  38.     public String calWeightIndex(String weight, String height)
  39.     {
  40.         this.dHeight = Double.parseDouble(height);
  41.         this.dWeight = Double.parseDouble(weight);
  42.         //System.out.println(dHeight);
  43.         
  44.         return calculate.getWeightIndex(dWeight, dHeight);
  45.     }
  46.     
  47.     /*
  48.     public static void main (String[] argc) 
  49.     {
  50.         Attribute attr = new Attribute();
  51.         attr.setWeight("60");
  52.         attr.setHeight("1.7");
  53.         System.out.println(attr.getWeightIndex());
  54.     }
  55.     */
  56.     
  57. }

你可能感兴趣的:(jsp,测试,application,Class,action,stylesheet)