Health.java

package model;

import java.io.Serializable;

public class Health 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 void setBmi(double bmi) {
    this.bmi = bmi;
  }

  public double getBmi() {
    return  this.bmi;
  }

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

  public String getBodyType() {
    return this.bodyType;
  }
}

      

HealthCheckLogic.java

package model;

public class HealthCheckLogic {
  public void execute(Health health) {

    double weight = health.getWeight();
    double height = health.getHeight();
    double bmi = weight / (height / 100.0 * height /100.0);
    health.setBmi(bmi);

    String bodyType;

    if (bmi < 18.5) {
      bodyType = "やせてる";
    } else if (bmi < 25) {
      bodyType = "ふつう";
    } else {
      bodyType = "ふとりぎみ";
    }

    health.setBodyType(bodyType);
  }
}
        

      

HealthCheck.java

package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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.Health;
import model.HealthCheckLogic;

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

    /**
      * @see HttpServlet#HttpServlet()
      */
    public HealthCheck() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/healthCheck.jsp");
dispatcher.forward(request, response);
  }

  /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  String weight = request.getParameter("weight");
  String height = request.getParameter("height");


  Health health = new Health();
  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("/WEB-INF/jsp/healthCheckResult.jsp");
      dispatcher.forward(request, response);
    }
}
        
      

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="/exsample7_3_2/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.Health" %>
      <%
      Health health = (Health)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>
      </body>
      </html>