JSP BMIアプリ(リクエストスコープ)P200


##BMI算出肥満度チェックアプリ
![P201.JPG](https://qiita-image-store.s3.amazonaws.com/0/95187/9b8ba91d-a0aa-23b0-39dd-a267333d28f3.jpeg)

▪️HealthCheck.java

```java
package servlet;

import javax.servlet.RequestDispatcher;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.HealthBean;
import model.HealthCheckLogic;

@WebServlet("/HealthCheck")
public class HealthCheck extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// フォワード
		RequestDispatcher dispatcher = request.getRequestDispatcher("/healthCheck.jsp");
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// リクエストパラメータを取得
		String weight = request.getParameter("weight"); // 身長
		String height = request.getParameter("height"); // 体重

		// 入力値をプロパティに設定
		HealthBean health = new HealthBean();
		health.setHeight(Double.parseDouble(height));
		health.setWeight(Double.parseDouble(weight));

		// 健康診断を実行し結果を設定
		HealthCheckLogic healthCheckLogic = new HealthCheckLogic();
		healthCheckLogic.execute(health);

		// リクエストスコープに保存
		request.setAttribute("health", health);

		// フォワード
		RequestDispatcher dispatcher = request.getRequestDispatcher("/healthCheckResult.jsp");
		dispatcher.forward(request, response);

	}

}
```

▪️HealthBean.java

```java
package model;

import java.io.Serializable;

public class HealthBean implements Serializable {
	private double height, weight, bmi;
	private String bodyType;

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public double getBmi() {
		return bmi;
	}

	public void setBmi(double bmi) {
		this.bmi = bmi;
	}

	public String getBodyType() {
		return bodyType;
	}

	public void setBodyType(String bodyType) {
		this.bodyType = bodyType;
	}

}
```

▪️HealthCheckLogic.java

```java
package model;

public class HealthCheckLogic {
	public void execute(HealthBean health) {
		// BMIを算出して設定
		double weight = health.getWeight();
		double height = health.getHeight();
		double bmi = weight / (height / 100.0 * height / 100.0);
		health.setBmi(bmi);

		// BMI指数から体型を判定して設定
		String bodyType;
		if (bmi < 18.5) {
			bodyType = "痩せ型";
		} else if (bmi < 25) {
			bodyType = "普通";
		} else {
			bodyType = "肥満";
		}
		health.setBodyType(bodyType);
	}

}
```

▪️healthCheck.jsp

```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>スッキリ健康診断</title>
</head>
<body>
	<h1>スッキリ健康診断</h1>
	<form action="/Test18/HealthCheck" method="post">
		身長:<input type="text" name="height">(cm)<br> 体重:<input
			type="text" name="weight">(kg)<br> <input type="submit"
			value="診断">
	</form>
</body>
</html>
```

▪️healthCheckResult.jsp

```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="model.HealthBean"%>
<%
	//リクエストスコープに保存されたHealthBeanインスタンスを取得
	HealthBean health = (HealthBean) request.getAttribute("health");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>スッキリ健康診断</title>
</head>
<body>
	<h1>スッキリ健康診断の結果</h1>
	<p>
		身長:<%=health.getHeight()%><br> 体重:<%=health.getWeight()%><br>
		BMI:<%=health.getBmi()%><br> 体重:<%=health.getBodyType()%>
	</p>
	<a href="/Test18/HealthCheck">戻る</a>
</body>
</html>
```

▪️HealthCheck.java 実行結果
![スクリーンショット 2016-02-24 17.22.14.png](https://qiita-image-store.s3.amazonaws.com/0/95187/e8a8498d-3d69-1af5-5138-5f28e89b23df.png)
![スクリーンショット 2016-02-24 17.24.23.png](https://qiita-image-store.s3.amazonaws.com/0/95187/324d3094-0c55-536f-b0ef-c6d3e6cc12d1.png)

▪️Test18 Dynamic Web Application ファイル構成図
![スクリーンショット 2016-02-24 17.25.38.png](https://qiita-image-store.s3.amazonaws.com/0/95187/5620e449-c110-d01b-9ddb-778c9d12911f.png)