Skip to content

Instantly share code, notes, and snippets.

@mtov

mtov/IMCServlet.java

Created Nov 14, 2019
Embed
What would you like to do?
Exemplo de Testabilidade - Servlet
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
class IMCModel {
public double calculaIMC(String p1, String a1)
throws NumberFormatException {
double p = Double.parseDouble(p1);
double a = Double.parseDouble(a1);
double imc = p / (a * a);
return Math.floor(imc * 100) / 100;
}
}
public class IMCServlet extends HttpServlet {
IMCModel model = new IMCModel();
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String peso = req.getParameter("peso");
String altura = req.getParameter("altura");
try {
double imc = model.calculaIMC(peso, altura);
out.println("IMC: " + imc);
}
catch (NumberFormatException e) {
out.println("Dados devem ser numéricos");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.