- <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>
- <%@ 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