首先創建一個calculate.jsp 這是用Javascript代碼來驗證,代碼如下: <script type="text/javascript"> function check(){ if(form.n1.value==""&&form.value.n2==""){ window.alert( ...
首先創建一個calculate.jsp
這是用Javascript代碼來驗證,代碼如下:
<script type="text/javascript">
function check(){
if(form.n1.value==""&&form.value.n2==""){
window.alert("值不能為空");
return false;
}
if(isNaN(form.n1.value)&&isNaN(form.n2.vavlue)){
window.alert("請確認輸入的是否是數字!");
return false;
}
if(form.operator.value=="/"&&form.n2.value=="0"){
window.alert("除數不能為0,請重新輸入! ");
return false;
}
}
</script>
使用Java代碼來計算,代碼如下:
<%
String operator=request.getParameter("operator");
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
int result=0;
if("+".equals(operator)){
result= n1+n2;
}
else if("-".equals(operator)){
result=n1-n2;
}
else if("*".equals(operator)){
result=n1*n2;
}
else {
result=n1/n2;
}
request.setAttribute("n1", n1);
request.setAttribute("n2", n2);
request.setAttribute("result", result); //往request作用域里放一個值
request.getRequestDispatcher("calculate.jsp").forward(request,response);
%>
form表單來寫一個簡單的頁面,代碼如下:
<form name="form" action="calculate_do.jsp" method="post" >
<input name="n1" value="<%= request.getAttribute("n1")==null?"":request.getAttribute("n1") %>">
<select name="operator">
<option value="+" <%= "+".equals(operator)?"selected":"" %>>+</option>
<option value="-" <%= "-".equals(operator)?"selected":"" %>>-</option>
<option value="*" <%= "*".equals(operator)?"selected":"" %>>*</option>
<option value="/" <%= "/".equals(operator)?"selected":"" %>>/</option>
</select>
<input name="n2" value="<%= request.getAttribute("n2")==null?"":request.getAttribute("n2") %>">
<input type="submit" value="=" onclick="return check();" >
<input name="result" value="<%= request.getAttribute("result")==null?"":request.getAttribute("result") %>">
</form>
源代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'calaulate.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 type="text/javascript">
function check(){
if(form.n1.value==""&&form.value.n2==""){
window.alert("值不能為空");
return false;
}
if(isNaN(form.n1.value)&&isNaN(form.n2.vavlue)){
window.alert("請確認輸入的是否是數字!");
return false;
}
if(form.operator.value=="/"&&form.n2.value=="0"){
window.alert("除數不能為0,請重新輸入! ");
return false;
}
}
</script>
</head>
<body>
<%
String operator=request.getParameter("operator");
int n1=Integer.parseInt(request.getParameter("n1"));
int n2=Integer.parseInt(request.getParameter("n2"));
int result=0;
if("+".equals(operator)){
result= n1+n2;
}
else if("-".equals(operator)){
result=n1-n2;
}
else if("*".equals(operator)){
result=n1*n2;
}
else {
result=n1/n2;
}
request.setAttribute("n1", n1);
request.setAttribute("n2", n2);
request.setAttribute("result", result); //往request作用域里放一個值
request.getRequestDispatcher("calculate.jsp").forward(request,response);
%>
<form name="form" action="calculate_do.jsp" method="post" >
<input name="n1" value="<%= request.getAttribute("n1")==null?"":request.getAttribute("n1") %>">
<select name="operator">
<option value="+" <%= "+".equals(operator)?"selected":"" %>>+</option>
<option value="-" <%= "-".equals(operator)?"selected":"" %>>-</option>
<option value="*" <%= "*".equals(operator)?"selected":"" %>>*</option>
<option value="/" <%= "/".equals(operator)?"selected":"" %>>/</option>
</select>
<input name="n2" value="<%= request.getAttribute("n2")==null?"":request.getAttribute("n2") %>">
<input type="submit" value="=" onclick="return check();" >
<input name="result" value="<%= request.getAttribute("result")==null?"":request.getAttribute("result") %>">
</form>
</body>
</html>