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("/tabla-multiplicar") public class TablaMultiplicarServelet 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.length() <1) 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) { invalid(pw, parametro); } else { Integer numero = Integer.parseInt(parametro); if(numero > 100) { greaterThan100(pw, numero); } else if(numero<0) { negative(pw); } else{ theTable(pw, numero); } } } pw.close(); } void initForm(PrintWriter pw) { pw.write("" + "" + "Hola, Aqui te enseƱo las tablas de la multiplicacion" + "" + "" + "

Soy un generador de tablas de multiplicar

" + "
" + "
" + "Enter 1st number:
" + "" + "
" + "
" + "" + ""); } void theTable(PrintWriter pw, Integer numero) { pw.write("" + "" + "Hola, bienvenidos al multiplicador" + "" + "" + "

Soy un generador de tablas de multiplicar

" + "
" + "
" + "Enter 1st number:
" + "" + "
" + "
" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + ""+ "" + ""+ "" +"" + "" + "" + "" + "" + ""); } void invalid(PrintWriter pw, String parametro) { pw.write("" + "" + "Error de parametro" + "" + "" + "No se puede generarla tabla de multiplicar. Valor del parametro \"" + parametro + "\" invalido." + "" + ""); } void greaterThan100(PrintWriter pw, Integer numero) { pw.write("" + "" + "Error de parametro" + "" + "" + "Mi programa solo conoce la tabla de multiplicar hasta el numero 100. El numero pasado fue: "+numero + "" + ""); } void negative(PrintWriter pw) { pw.write("" + "" + "Error de parametro" + "" + "" + "Mi programa no sabe las tablas de multiplicar de numeros negativos" + "" + ""); } }
"+numero+"*1 ="+(numero*1) + "
"+numero+"*2 ="+(numero*2) + "
"+numero+"*3 ="+(numero*3) + "
"+numero+"*4 ="+(numero*4) + "
"+numero+"*5 ="+(numero*5) + "
"+numero+"*6 ="+(numero*6) + "
"+numero+"*7 ="+(numero*7) + "
"+numero+"*8 ="+(numero*8) + "
"+numero+"*9 ="+(numero*9) + "
"+numero+"*10="+(numero*10)+ "