1.引入fastjson-1.2.43.jar 包到lib下面,下载地址链接: https://pan.baidu.com/s/1pLxGe9P 密码: 6xsw
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("/Servlet")
public class CalculateServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/json;charset=utf-8");
String numA = request.getParameter("numA");
String numB = request.getParameter("numB");
Float fnumA = Float.parseFloat(numA);
Float fnumB = Float.parseFloat(numB);
Float sum, minus, multiply, divide;
sum = fnumA + fnumB;
minus = fnumA - fnumB;
multiply = fnumA * fnumB;
divide = fnumA / fnumB;
DecimalFormat df = new DecimalFormat("#0.00");//取小数点后两位四舍五入
String sminus = df.format(minus);
String ssum = df.format(sum);
String smultiply = df.format(multiply);
String sdivide = df.format(divide);
Map<String,Object> map = new HashMap<String,Object>();
map.put("sum",ssum);
map.put("minus",sminus);
map.put("multiply",smultiply);
map.put("divide",sdivide);
JSONObject json = JSONObject.parseObject(JSON.toJSONString(map));//把map转为json数据
PrintWriter out = response.getWriter();
out.write(json.toString());
out.flush();
out.close();
}
}
前端jsp代码入下
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/12/18 Time: 14:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>综合测试</title> <script src="static/js/jquery.js"></script> </head> <br> <form id="f"><!--method="post" action="Servlet"--> <label>numA:</label> <input type="text" name="numA" id="numA" /><div id="A"></div><br/> <label>numB:</label> <input type="text" name="numB" id="numB" /><div id="B"></div><br/> <input type="button" value="运算" id="btn"/> </form> <span>和为:</span><span id="sum"></span></br> <span>差为:</span><span id="minus"></span></br> <span>积为:</span><span id="multiply"></span></br> <span>商为:</span><span id="divide"></span></br> <script type="text/javascript"> var reg = /^\d+(\.\d+)?$/;//判断是数字 $("#numA").blur(function () { var numA=$("#numA").val(); numA = $.trim(numA); if(numA=="") $("#A").text("numA为空!"); else if(reg.test(numA)==false) $("#A").text("numA不是数字!"); }); $("#numA").focus(function () { $("#A").empty(); }); $("#numB").blur(function () { var numB=$("#numB").val(); numB = $.trim(numB);//去除空格符 if(numB=="") $("#B").text("numB为空!"); else if(reg.test(numB)==false) $("#B").text("numB不是数字!"); else if(numB==0) $("#B").text("error:分母不能为0!"); }); $("#numB").focus(function () { $("#B").empty(); }); $("#btn").click(function () { var numA=$("#numA").val(); var numB=$("#numB").val(); if(numA!=""&&numB!="") $.get("/Servlet",$("#f").serialize(),function (data) { console.log(data); $("#sum").text(data.sum); $("#minus").text(data.minus); $("#multiply").text(data.multiply); $("#divide").text(data.divide); });}); </script> </body> </html>
*注意如果出错,请将fastjson-1.2.43.jar复制一份到tomcat 的lib 目录下,然后重启项目。