JSP BMI 어플리케이션(오실로그래프 요청) P200

BMI 비만 측정 어플리케이션



▪️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 실행 결과


▪Test18 Dynamic Web Application 파일 맵

좋은 웹페이지 즐겨찾기