用jsp servlet 做一个简易计算器.docx
-
资源ID:3658272
资源大小:37.59KB
全文页数:6页
- 资源格式: DOCX
下载积分:6.99金币
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
|
用jsp servlet 做一个简易计算器.docx
用jsp servlet 做一个简易计算器用jsp servlet 做一个简易计算 Jsp程序代码: <% 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> <title>calculator</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> <% String first = (String)request.getAttribute("first"); String second = (String)request.getAttribute("second"); String operator = (String)request.getAttribute("operator"); String result = (String)request.getAttribute("result"); %> <h1>简易计算器</h1> <form method="post" action="CalculatorServlet"> <input value="<%=(first=null?"":first) %>"/> <select name="operator" > <option value="+">+</option> name="first" <option value="-">-</option> <option value="/">/</option> <option value="*">*</option> <option value="pow">square</option> <option value="sqrt">radication</option> </select> <input name="second" value="<%=(second=null?"":second) %>"/> =<input name="result" value="<%=(result=null?"":result) %>" /> <input value="计算" type="submit"> </form> </body> </html> 新建一个java bean 程序如下: package com.wp.bean; public class Calculator public double getSecond return second; public void setFirst(double first) this.first = first; public double getFirst return first; private double first; private double second; private String operator; public void setSecond(double second) this.second = second; public String getOperator return operator; public void setOperator(String operator) public String calculate double result = 0; this.operator = operator; String exceptions = "" try if("+".equals(operator)result = first + second; if("-".equals(operator)result = first - second; if("*".equals(operator)result = first * second; if("/".equals(operator)&&second!=0)result = first / second; Servlet 程序如下: if("sqrt".equals(operator)result = Math.sqrt(first); if("pow".equals(operator)result = Math.pow(first,2); catch (Exception e) return Double.toString(result)+exceptions; e.printStackTrace; exceptions = "异常" . package com.wp.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.wp.bean.Calculator; public class CalculatorServlet extends HttpServlet public void doGet(HttpServletRequest HttpServletResponse response) throws ServletException, IOException request, this.doPost(request,response); public void doPost(HttpServletRequest request, HttpServletResponse response) ; if(!second.isEmpty)cal.setSecond(Double.valueOf(second) String first = request.getParameter("first"); String second = request.getParameter("second"); String operator = request.getParameter("operator"); Calculator cal = new Calculator; if(!first.isEmpty)cal.setFirst(Double.valueOf(first); throws ServletException, IOException cal.setOperator(operator); String result = cal.calculate; System.out.println(result); request.setAttribute("result",result); request.setAttribute("first",first); request.setAttribute("second",second); request.setAttribute("operator",operator); request.getRequestDispatcher("index.jsp").forward(request,response); . 程序亲测,可用