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

 

 

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());     }     */      }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章