Page: <%@ 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" >
Controller: 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.google.gson.gson;public class Calculatorservlet extends HttpServlet {/** * Constructor of the object. */public Calculatorservlet () {super ();} /** * Destruction of the servlet. <br> */public void Destroy () {Super.destroy ();//Just puts "destroy" string in log//Put your code here}/** * the D Oget method of the servlet. <br> * This method was called when a form have its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void Doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, IOException {//1 Chinese processing request. setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Gson Gson = new Gson ();//2 read into parameter int a = -1;int B = -1;try {a = Integer.parseint (Request.getparameter ("a")); b = Integer.pars Eint (Request.getparameter ("B")); catch (Exception e) {//creates 1 error message objects, which are written back in JSON. Method terminates prematurely. msg m = new msg (1, "the number entered is in the wrong format!") "); String json = Gson.tojson (m); Response.getwriter (). print (JSON); Response.getwriter (). Flush (); Response.getwriter (). Close (); return;} String op = Request.getparameter ("op");//3 create Calculator object Calculator c = New Calculator (A, B, op); c.cal ();//perform calculations, calculate results//4 turn into a JSON string returns string JSON = Gson.tojson (c); Response.getwriter (). print (JSON); Response.getwriter (). Flush (); Response.getwriter (). Close ();} /** * The DoPost method of the servlet. <br> * This method was called when a form have its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception if an error occurred * @throws IOException If an error occurred */public void DoPost (HttpServletRequest request, HttpServletResponse re Sponse) throws Servletexception, IOException {response.setcontenttype ("text/html"); PrintWriter out = Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ") out.println (" <HTML> "); Out.println (" < Head><title>a servlet</title>
Classes public class MSG {private int errcode;private String errmsg;public int Geterrcode () {return errcode;} used to prompt for error messages public void Seterrcode (int errcode) {this.errcode = Errcode;} Public Msg (int errcode, String errmsg) {super (); This.errcode = Errcode;errmsg = ErrMsg;}}
public class Calculator {private int a;private string op;private int b;private string show_op;private int result;public Ca Lculator (int A, int B, String op) {super (); THIS.A = a;this.b = B;this.op = op;} Public String Getop () {return op;} public void Setop (String op) {this.op = op;} public int GetResult () {return result;} public void Setresult (int result) {This.result = result;} Performs a calculation that calculates the Resultpublic void Cal () {if ("add") {"Equals (OP)") {result = A+b;show_op = "+";} else if ("Sub". Equals (OP)) {result = A-b;show_op = "-";} else if ("Multi". Equals (OP)) {result = A*b;show_op = "*";} else if ("div". Equals (OP)) {result = A/b;show_op = "/";}}}
A (+;-;*;/) b-----Demo----Bai