package com.roshka.webprojecttest.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/factorial") public class FactorialServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter pw = new PrintWriter(resp.getOutputStream()); Boolean valido=true; String parametro = req.getParameter("number"); if(parametro == null ) { initForm(pw); } else { if(parametro.equals("")) valido=false; for(int i = 0 ; i < parametro.length(); i++) { if( ( !Character.isDigit(parametro.charAt(i)) && i > 0) && !(Character.isDigit(parametro.charAt(i)) && i == 0 && parametro.charAt(i) != '-' ) ) { valido=false; break; } } if (!valido) { error(pw, parametro); } else { Integer numero = Integer.parseInt(parametro); if(numero > 100) { greaterThan100(pw, numero); } else if(numero<0) { negative(pw); } else{ long facto=1; for (long i = 2; i <=numero;i++) facto*=i; factoDisplay(pw, numero, facto); } } } pw.close(); } void factoDisplay(PrintWriter pw, Integer numero, Long facto) { pw.write("" + "
" + "El factorial de "+ numero+" es: " + facto +"
" + "