TablaMultiplicarResponseServelet.java 3.52 KB
Newer Older
Matias Ferreira committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

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-response")
public class TablaMultiplicarResponseServelet 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.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) {
			pw.write("<html>"
					+ "<head>"
					+ "<title>Error de parametro</title>"
					+ "</head>"
					+ "<body>"
					+ "No se puede generarla tabla de multiplicar. Valor del parametro \"" + parametro + "\" invalido."
					+ "</body>"
					+ "</html>");

		}
		else {
			Integer numero = Integer.parseInt(parametro);
			if(numero > 100) {
				pw.write("<html>"
						+ "<head>"
						+ "<title>Error de parametro</title>"
						+ "</head>"
						+ "<body>"
						+ "Mi programa solo conoce la tabla de multiplicar hasta el numero 100. El numero pasado fue: "+numero
						+ "</body>"
						+ "</html>");
				
			}
			else if(numero<0) {
				pw.write("<html>"
						+ "<head>"
						+ "<title>Error de parametro</title>"
						+ "</head>"
						+ "<body>"
						+ "Mi programa no sabe las tablas de multiplicar de numeros negativos"
						+ "</body>"
						+ "</html>");
			}
			else{
				pw.write("<html>"
						+ "<head>"
						+ "<title>Hola, bienvenidos al multiplicador</title>"
						+ "</head>"
						+ "<body>"
						+ "<h1>Soy un generador de tablas de multiplicar</h1>"
70 71 72 73 74 75
						+ "<div>"
						+ "<form action=\"tabla-multiplicar-response\">"
						+ "Enter 1st number: <input type=\"text\"  name=number><br>"
						+ "<input type=\"submit\">"
						+ "</form>"
						+ "</div>"
Matias Ferreira committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
						+ "<table style=\"width:5%\">"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>1 </th>" +"<th>=</th>" + "<th>"+(numero*1) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>2 </th>" +"<th>=</th>" + "<th>"+(numero*2) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>3 </th>" +"<th>=</th>" + "<th>"+(numero*3) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>4 </th>" +"<th>=</th>" + "<th>"+(numero*4) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>5 </th>" +"<th>=</th>" + "<th>"+(numero*5) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>6 </th>" +"<th>=</th>" + "<th>"+(numero*6) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>7 </th>" +"<th>=</th>" + "<th>"+(numero*7) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>8 </th>" +"<th>=</th>" + "<th>"+(numero*8) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>9 </th>" +"<th>=</th>" + "<th>"+(numero*9) + "</th>" + "</tr>"
						+ "<tr>"+ "<th>"+numero+"</th>" + "<th>*</th>"+ "<th>10</th>" +"<th>=</th>" + "<th>"+(numero*10)+ "</th>" + "</tr>"
						+ "</tdr>"
						+ "</body>"
						+ "</html>");				
			}
		}
		pw.close();
	}
}