其他分享
首页 > 其他分享> > 全局刷新例题

全局刷新例题

作者:互联网

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>全局刷新</title>
</head>
<body>
      <p>全局刷新计算bmi</p>
      <form action="/myWeb/bmiServlet" method="get">
        姓名:<input type="text" name="name"><br/>
        体重(公斤):<input type="text" name="w"><br/>
        身高(米):<input type="text" name="h"><br/>
        <input type="submit" value="提交">
      </form>
</body>
</html>

BmiServlet.class

public class BmiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收请求参数
        String name = req.getParameter("name");
        String weight = req.getParameter("w");
        String height = req.getParameter("h");

        //计算bmi=体重/身高*身高
        float h = Float.valueOf(height);
        float w = Float.valueOf(weight);
        float bmi = w/(h*h);
        //判断bmi的范围
        String msg ="";
        if (bmi<=18.5){
            msg = "您比较瘦";
        }else if (bmi>18.5&&bmi<=23.9){
            msg="正常的";
        }else if (bmi>24&&bmi<=27){
            msg = "比较胖";
        }else {
            msg = "非常胖";
        }
        System.out.println("msg = "+msg);
        String msg1 = "您好,"+name+" 先生/女士,您的BMI: "+bmi+", 你的身体: "+msg;

        req.setAttribute("msg",msg1);

        req.getRequestDispatcher("/result.jsp").forward(req,resp);
    }
}

result.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>结果界面</title>
</head>
<body>
        <p>显示bmi计算结果</p>
        <h3>${requestScope.msg}</h3>
</body>
</html>

标签:bmi,String,float,req,getParameter,刷新,msg,全局,例题
来源: https://blog.csdn.net/Newnono/article/details/120241405