日期:2014-05-17  浏览次数:20678 次

servlet 的
html的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>calculator</title>
</head>
<body>
<form method="post" action="calculator.do">

 <label>请输入两个整数</label>
  <p>
  操作数1<input name="op1" type="text"/>
  操作数2 <input name="op2" type="text"/>
  </p>
  <p>
  <input name="rel" type="submit" value="plus" />
  
</p>
</form>
</body>
</html>



servlet的

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletExceptionIOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out=response.getWriter();


String op1 = request.getParameter("op1");
String op2 = request.getParameter("op2");
String[] rel= request.getParameterValues("rel");

int result = 0;
int op11=Integer.parseInt(op1);
int op12=Integer.parseInt(op2);
System.out.println(rel.toString());
if(rel[0].equals("plus")) 
{result=op11+op12;}
       else if(rel[0].equals("minus"))
{result=op11-op12;}

out.println("<html><head>");
out.println("<title>结果</title>");
out.println("</head><body>");

out.println("运算结果是:"+result+"分");
out.println("</body></html>");
servlet html

------解决方案--------------------
给你的form起个名如:myForm
function chuli(fuhao){
  myForm.action = "calculator.do?f="+fuhao;
  myForm.submit();
}
------解决方案--------------------
给你的form起个名如:myForm
function chuli(fuhao){
  myForm.action = "calculator.do?f="+fuhao;
  myForm.submit();

.....
<input name="rel" type="submit" value="plus" chuli("plus")/> 
<input name="rel" type="submit" value="muls" chuli("muls")/>