jsp+Servlet+javaBean编写计算体重指数测量(BMI)的简单程序

本文详细介绍了如何使用JSP、Servlet和JavaBean技术组合编写一个计算体重指数(BMI)的程序,包括BMI.java中的bean定义、Bmiservlet中的业务逻辑处理和显示界面的JSP展示。通过实例演示了用户输入身高和体重后计算并显示BMI的过程。
摘要由CSDN通过智能技术生成

jsp+Servlet+javaBean编写计算体重指数测量(BMI)的简单程序

  1. BMI.java(javaBean代码)

public class BMI { 	
	private double bmi, weight, height;

	public double getBmi()
	 { 		
	 	return bmi; 	
	 }

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

	public double getWeight()
	 { 		
	 	return weight; 	
	 }

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

	public double getHeight() 
	{ 		
		return height; 	
	}

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

	public String ccBMI(double height, double weight) 
	{ 		
		bmi = weight / height / height; 		
		String Msg = String.valueOf(bmi); 		
		return Msg; 	}
    }

  1. servlet编写
package a_BMI;

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;

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

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String Msg = "";
		response.setContentType("text/html; charset=gb2312");

		if ((request.getParameter("height") == "" || request.getParameter("weight") == "")) {
			request.setAttribute("flag", "Error");
			request.getRequestDispatcher("showInput.jsp").forward(request, response);
		} else {
			double height = Double.valueOf(request.getParameter("height"));
			double weight = Double.valueOf(request.getParameter("weight"));
			System.out.print(new BMI().ccBMI(height, weight));
			if (weight == 0 || height == 0) {
				Msg = "please ensure your data";
			} else {
				Msg = new BMI().ccBMI(height, weight);
			}
			request.setAttribute("BMI", Msg);
			request.getRequestDispatcher("showInput.jsp").forward(request, response);
		}

		// System.out.println((String) request.getParameter("height"));
	}

}

  1. 显示界面(jsp编写)
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BMI</title>
<style type="text/css">
.center {
	text-align: center;
	margin-top: 150px;
}

.fon {
	font-size: 40px;
}

.fon2 {
	font-style: italic;
}

p {
	color: yellow;
}

font {
	color: yellow;
	font-style: italic;
}

body {
	background: url("wallhaven-o59vjp_3840x2160.png");
	background-size: 100% 100%;
}

input {
	background-color: transparent;
	outline-color: blue;
	color: yellow;
}

.clear {
	opacity: 0.3;
	color: #0000CD;
	background: #87CEFA; 
}
</style>
</head>
<body>
	<div class="center">
		<p class="fon">BMI Calculation</p>
		<p class="fon2">Please input your height and weigh</p>
		<form method="post" action="Bmiservlet">
			<font>Height:(m)&nbsp;&nbsp;</font><input type="text" name="height"
				name="height" style="width: 300px; heitht: 50px"
				placeholder="please input your height: "><br> <br>
			<font>Weight:(kg)&nbsp;&nbsp;</font><input type="text" name="weight"
				name="weight" style="width: 300px; heitht: 50px"
				placeholder="please input your weight: "><br>
			<br>
			<button type="submit"
				style="width: 80px; height: 40px; font-size: 20px" class="clear">submit</button>
			<br><br>
			<%
					String BMI  = (String) request.getAttribute("BMI");
					String text = "please ensure your data";
					if (text.equals(BMI)&&BMI != null) {
						out.println("<font>" + BMI + "</font>");
					}
					else{
						if(BMI != null)
						out.println("<font>Your BMI: " + BMI + "</font>");
					}
					%>
		</form>
	</div>
</body>
</html>
<script> 
//取出传回来的参数error并与yes比较
  var errori ='<%=request.getAttribute("flag")%>';
  if(errori=='Error'){
   alert("Error!");
  }
</script>

**

效果如下

**
在这里插入图片描述
在这里插入图片描述
输入为空时的报错
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值