index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<!-- 用户提交验证 -->
<script type="text/javascript" language="javascript">
<!--
function checkNum(){
if(form1.num1.value==""){
window.alert("num1 值不能为空 !!哈哈");
return false;
}
//判断num1是不是一个数
if(Math.round(form1.num1.value)!=(form1.num1.value)){
window.alert("num1不是一个整数")
return false;
}
if(form1.num2.value==""){
window.alert("num2 值不能为空 !!哈哈");
return false;
}
//判断num2是不是一个数
if(Math.round(form1.num2.value)!=(form1.num2.value)){
window.alert("num2不是一个整数")
return false;
}
}
-->
</script>
</head>
<h1>我的计算器</h1>
<hr>
<body>
<form name ="form1" action="result.jsp" method ="post">
<input type="text" name ="num1" ></input><br>
<select name="flag">
<option value=+>+</option>
<option value=->-</option>
<option value=*>*</option>
<option value=/>/</option>
</select><br>
<input type="text" name="num2"/></input><br>
<input type="submit" value="提交" οnclick="return checkNum();"></input>
</form>
<hr>
</body>
</html>
result.jsp用于显示结果
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>
</head>
<body>
<%
String num1=request.getParameter("num1");
String num2 = request.getParameter("num2");
String flag = request.getParameter("flag");
int s_num1=Integer.parseInt(num1);
int s_num2=Integer.parseInt(num2);
int result=0;
if(flag.equals("+")){
//加
result=s_num1+s_num2;
}else if(flag.equals("-")){
//减
result=s_num1-s_num2;
}else if(flag.equals("/")){
result=s_num1/s_num2;
//除
}else{
//乘
result=s_num1*s_num2;
}
out.println("结果是:"+result);
%>
</body>
</html>