<%@ page language="java" import="java.util.*,java.math.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script language="javascript">
<!--
function checkNum(){
if(form1.num1.value==""){
window.alert("num1不能为空!");
return false;
}
if(form1.num2.value==""){
window.alert("num2不能为空!");
return false;
}
if(form1.num1.value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")){
window.alert("num1必须为数字");
return false;
}
if(form1.num2.value.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")){
window.alert("num2必须为数字");
return false;
}
}
-->
</script>
</head>
<body>
<%
String s_num1=request.getParameter("num1");
String s_num2=request.getParameter("num2");
String flag=request.getParameter("cal");
double num1=0.0;
double num2=0.0;
double result=0.0;
if(s_num1!=null&&s_num2!=null&&flag!=null){
num1=Double.parseDouble(s_num1);
num2=Double.parseDouble(s_num2);
if(flag.equals("+")){
result=num1+num2;
}
else if(flag.equals("-")){
result=num1-num2;
}
else if(flag.equals("*")){
result=num1*num2;
}
else if(flag.equals("/")){
if(num2==0.0){
out.print("除数不能为零!");
}
else{
result=num1/num2;
}
}
BigDecimal r=new BigDecimal(result);
r=r.setScale(10,BigDecimal.ROUND_HALF_UP);
result=r.doubleValue();
}
%>
四则运算<br>
<hr>
<form name="form1" action="input.jsp" method="post">
<input type="text" name="num1" size="14" maxlength="10" value="<%=num1%>">
<select name="cal" style="width:40px;heigth:21px">
<option value='+'>+</option>
<option value='-'>+</option>
<option value='*'>*</option>
<option value='/'>/</option>
</select>
<input type="text" name="num2"/ size="14" maxlength="10" value="<%=num2%>">
<input type="submit" value="=" style="width:50px;height:21px" onclick="return checkNum();">
<%=result%>
</form>
</body>
</html>