Package pl.onet.dreamcalc.server

Source Code of pl.onet.dreamcalc.server.JsonDreamCalc

package pl.onet.dreamcalc.server;

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;

@SuppressWarnings("serial")
public class JsonDreamCalc extends HttpServlet {
 
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    double arg1, arg2, result = 0.0;
    String operator;
    PrintWriter out = resp.getWriter();
   
    String[] args = req.getParameter("q").split(" ");
 
    arg1 = Double.parseDouble(args[0]);
    arg2 = Double.parseDouble(args[1]);
    operator = args[2];

    if (operator.equals("p"))
      result = arg1 + arg2;
    else if (operator.equals("-"))
      result = arg1 - arg2;
    else if (operator.equals("*"))
      result = arg1 * arg2;
    else if (operator.equals("/"))
      result = arg1 / arg2;
   
    out.format("{\"result\": %s}", result);
    out.flush();
  }
}
TOP

Related Classes of pl.onet.dreamcalc.server.JsonDreamCalc

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.