<%@ 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 '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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="index.jsp" method="post">
<input type="text" name="num1">
<select name="opt">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" name="num2">
<input type="submit" value="Ìá½»"/>
</form>
<%
String str1=request.getParameter("num1");
String str2=request.getParameter("num2");
String str3=request.getParameter("opt");
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
if(str3.equals("+")){
out.print(a+str3+b+"="+(a+b));
}
if(str3.equals("-")){
out.print(a+str3+b+"="+(a-b));
}
if(str3.equals("*")){
out.print(a+str3+b+"="+(a*b));
}
if(str3.equals("/")){
out.print(a+str3+b+"="+(a/b));
}
%>
</body>
</html>
Ϊʲôǿת³öÏÖ500´íÎó
jsp
------½â¾ö·½°¸--------------------
µÚÒ»£¬
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
´òÓ¡³öÀ´str1ºÍstr2¿´¿´
µÚ¶þ£¬
if(str3.equals("/")){
out.print(a+str3+b+"="+(a/b));
}
bΪ0Ôõô°ì£¿
------½â¾ö·½°¸--------------------
´úÂë¾Í²»¿´ÁË£¬¾ÍÊÇ°ïÄãÕÒµ½ÎÊÌ⣬ҲֻÊǽâ¾öÕâ´ÎµÄÒ»¸öСÎÊÌ⣡
·³öÈÕÖ¾ÀïµÄ±¨´í¶ÑÕ»£¬ÕÒµ½¶ÔÓ¦µÄ.java:LINE£¬´ÓÁÙʱĿ¼ÕÒµ½Õâ¸öjsp¶ÔÓ¦µÄjavaÎļþ£¬Ò»¿´±ãÖª£¬ÒÔºóÅöµ½500¶¼Õâô¸É¡£
------½â¾ö·½°¸--------------------
int a=Integer.parseInt(str1);
int b=Integer.parseInt(str2);
str1ºÍstr2ת»»Ö®Ç°×îºÃÏÈ×îÏÂÊÇ·ñΪ""»ònullÅжϣ¬Èç¹û²»Îª¿ÕÊDz»ÊÇÄãµÄstr1»òstr2Ì«³¤ÁË£¿
------½â¾ö·½°¸--------------------
Äã¿ÉÒԸijÉÕâÑù£¬¼ÓÅжϣ¬str1,str2,str3·Ç¿Õ
int a = 0;
int b = 0;
if(str1!=null){
a=Integer.parseInt(str1);
}
if(str2!=null){
b=Integer.parseInt(str2);
}
if(str3!=null){
if(str3.equals("+")){
out.print(a+str3+b+"="+(a+b));
}
if(str3.equals("-")){
out.print(a+str3+b+"="+(a-b));
}
if(str3.equals("*")){
out.print(a+str3+b+"="+(a*b));
}
if(str3.equals("/")){
out.print(a+str3+b+"="+(a/b));
}
}