JSP(3)使用jsp实现简单的计算器

蔚落 2024-02-17 21:32 101阅读 0赞

1 目录结构

这里写图片描述 这里写图片描述

2 代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4. <head>
  5. <title>My JSP 'MyCalInterface.jsp' starting page</title>
  6. <meta http-equiv="pragma" content="no-cache">
  7. <meta http-equiv="cache-control" content="no-cache">
  8. <meta http-equiv="expires" content="0">
  9. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  10. <meta http-equiv="description" content="This is my page">
  11. <!--
  12. <link rel="stylesheet" type="text/css" href="styles.css">
  13. -->
  14. <%-- 引用js文件 --%>
  15. <script type="text/javascript" src="js/js1.js"></script>
  16. </head>
  17. <body>
  18. <%-- 使用js验证输入的两种方式,①在form属性中验证onsubmit ②在按钮事件中验证onclick --%>
  19. <form action="/CalJsp/Result.jsp" method="post" onsubmit="return checkNum()">
  20. 请输入第一个数:<input type="text" id="num1" name="num1" /><br />
  21. 请输入第二个数:<input type="text" id="num2" name="num2" /><br/>
  22. 请选择运算符:<select name="operator">
  23. <option>+</option>
  24. <option>-</option>
  25. <option>*</option>
  26. <option>/</option>
  27. </select><br/>
  28. <input type="submit" onclick="return checkNum()" value="计算" />
  29. </form>
  30. </body>
  31. </html>
  32. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  33. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  34. <html>
  35. <head>
  36. <title>My JSP 'Result.jsp' starting page</title>
  37. <meta http-equiv="pragma" content="no-cache">
  38. <meta http-equiv="cache-control" content="no-cache">
  39. <meta http-equiv="expires" content="0">
  40. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  41. <meta http-equiv="description" content="This is my page">
  42. </head>
  43. <body>
  44. <%
  45. <%-- 获取提交的数据,与servlet一样 --%>
  46. String num1 = request.getParameter("num1");
  47. String num2 = request.getParameter("num2");
  48. String oper = request.getParameter("operator");
  49. double res = 0;
  50. double d_num1 = Double.parseDouble(num1);
  51. double d_num2 = Double.parseDouble(num2);
  52. if(oper.equals("+")) { res = d_num1 + d_num2; }
  53. else if (oper.equals("-")) { res = d_num1 - d_num2; }
  54. else if (oper.equals("*")) { res = d_num1 * d_num2; }
  55. else if (oper.equals("/")) { res = d_num1 / d_num2; }
  56. out.println("结果是:" + res);
  57. %>
  58. </body>
  59. </html>

js1文件:http://blog.csdn.net/u013943420/article/details/70157168#t2

3 将以上代码合并到一个文件(界面与计算操作合为一个文件)

  1. <%-- 只保留变化的部分 --%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  3. <html>
  4. <head>
  5. </head>
  6. <%
  7. String num1 = request.getParameter("num1");
  8. String num2 = request.getParameter("num2");
  9. String oper = request.getParameter("operator");
  10. double res = 0;
  11. if (num1!=null && num2!=null && oper!=null) {
  12. double d_num1 = Double.parseDouble(num1);
  13. double d_num2 = Double.parseDouble(num2);
  14. if(oper.equals("+")) { res = d_num1 + d_num2; }
  15. else if (oper.equals("-")) { res = d_num1 - d_num2; }
  16. else if (oper.equals("*")) { res = d_num1 * d_num2; }
  17. else if (oper.equals("/")) { res = d_num1 / d_num2; }
  18. }
  19. %>
  20. <body>
  21. <form action="/CalJsp/MyCalInterface.jsp" method="post" onsubmit="return checkNum()">
  22. <%-- 提交表单后,输入框内数字保留 --%>
  23. 请输入第一个数:<input type="text" id="num1" value="<%= num1==null?"":num1 %>" name="num1" /><br />
  24. 请输入第二个数:<input type="text" id="num2" value="<%= num2 %>" name="num2" /><br/>
  25. 请选择运算符:<select name="operator">
  26. <option>+</option>
  27. <option>-</option>
  28. <option>*</option>
  29. <option>/</option>
  30. </select><br/>
  31. <input type="submit" onclick="return checkNum()" value="计算" />
  32. </form>
  33. 结果是:<%= res %>
  34. </body>
  35. </html>

发表评论

表情:
评论列表 (有 0 条评论,101人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JSP 简单计算器

    要求: 2、编写一个类实现加、减、乘、除、取余 计算,在JSP页面中完成如下功能: (1)输入两个操作数,选择运算符号,提交数据; (2)接收数据,进行运算,将运算结