jsp+Servlet+javaBean编写计算体重指数测量(BMI)的简单程序
- 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; }
}
- 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"));
}
}
- 显示界面(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) </font><input type="text" name="height"
name="height" style="width: 300px; heitht: 50px"
placeholder="please input your height: "><br> <br>
<font>Weight:(kg) </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>
**
效果如下
**
输入为空时的报错