2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-02-24

BMI算出肥満度チェックアプリ

P201.JPG

▪️HealthCheck.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

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

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

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

<%@ 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
スクリーンショット 2016-02-24 17.24.23.png

▪️Test18 Dynamic Web Application ファイル構成図
スクリーンショット 2016-02-24 17.25.38.png

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?