<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function myCheck(){
var numbe=document.getElementById("area").value;
var clips=document.getElementById("item");
var nm=/^[0-9]{1,9}$/;
if(nm.test(numbe)){
clips.innerHTML="输入正确!".fontcolor("green");
document.form1.submit();
}
else{
clips.innerHTML="输入错误!".fontcolor("red");
return false;
}
}
</script>
</head>
<body style="background-color:rgb(49,121,227);">
<%!
final double PI=3.14;
public double getArea(double r){ //计算圆的面积
double area=PI*r*r;
return area;
}
public double getLength(double r){ //计算圆的周长
double length=2*PI*r;
return length;
}
%>
<form action="" id="form1">
<input type="text" name="area" id="area" placeholder="请输入你的数"/><span id="item"></span><br><br>
<input type="submit" value="确定" onclick="return myCheck()"/><br>
</form>
<%
String r1=request.getParameter("area"); //获取input里面的内容
if(r1!=null){ //不为空的情况下再让程序执行
double r=Double.parseDouble(r1); //把input输入的String类型转化为double类型
out.println("半径为"+r+"的圆的面积是:"+getArea(r)+"<br>");
out.println("半径为"+r+"的圆的周长是:"+getLength(r)+"<br>");
}
else{
out.println("输入错误!");
}
%>
</body>
</html>