体重指数 java web 关键代码
1.view_ Table.JSP
<form action="../controller_servlet" method="post" >
你的姓名是:<input type="text" name="mz" ><br>
你的学号是:<input type="text" name="xh" ><br>
你的身高为多少厘米:<input type="text" name="sg" ><br>
你的体重为多少千克:<input type="text" name="tz" ><br>
<input type="submit" value="提交">
<input type="reset" value="重填">
</form>
2.view_ Result.JSP
我的姓名是<%=session.getAttribute("mz") %><br>
我的学号为<%=session.getAttribute("xh") %><br>
我的体重标准为<%=session.getAttribute("jg") %><br>
3.model_ Bean.java
public class model_Bean {
private double sg;
private double tz;
public model_Bean(double sg ,double tz)
{
this.sg=sg;
this.tz=tz;
}
public double getSg() {
return sg;
}
public void setSg(double sg) {
this.sg = sg;
}
public double getTz() {
return tz;
}
public void setTz(double tz) {
this.tz = tz;
}
public double tzzs()
{
return (tz/((sg/100)*(sg/100)));
}
}
4.controller_ servlet. java
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
HttpSession session=request.getSession();
String mz=request.getParameter("mz");
String xh=request.getParameter("xh");
String sg=request.getParameter("sg");
String tz=request.getParameter("tz");
double sg1=Double.parseDouble(sg);
double tz1=Double.parseDouble(tz);
model_Bean zs = new model_Bean(sg1, tz1);
double result=0.0;
String jg=null;
result=zs.tzzs();
if(result>=18.5&&result<=25)
{
jg="体重在正常范围";
}
else if(result>25&&result<30)
{
jg="体重状况为超重";
}
else if(result>30)
{
jg="体重状况为较肥胖";
}else if(result <18.5)
{
jg="体重状况为偏瘦";
}
session.setAttribute("jg", jg);
session.setAttribute("mz", mz);
session.setAttribute("xh", xh);
request.getRequestDispatcher("/tizhong/view_Result.jsp").forward(request, response);