conversor_temperatura.java 4.52 KB
Newer Older
roshka 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134


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;

/**
 * Servlet implementation class conversor_temperatura
 */
@WebServlet("/conversor_temperatura")
public class conversor_temperatura extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public conversor_temperatura() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter escribir = response.getWriter();
		try {
			double fahrenheit;
			double celsius;
			double kelvin;
			double entrada = Double.parseDouble(request.getParameter("P1"));
			double selector = Double.parseDouble(request.getParameter("P0"));
			if(selector == 0) {
				//Esto es si la temperatura esta en celsius
				celsius = entrada;
				fahrenheit = (entrada * 9/5) + 32;
				kelvin = entrada + 273.15;
				escribir.append(
						"<!DOCTYPE html>\n" + 
						"<html>\n" + 
						"<head>\n" + 
						"	<meta charset=\"UTF-8\">\n" + 
						"	<title>Resultado temperaturas</title>\n" + 
						"	</head>\n" + 
						"<body>\n" + 
						"	<h1>Los resultados son</h1>\n" + 
						"	<p>Celsius: "+celsius+"</p>\n" +
						"	<p>Fahrenheit: "+fahrenheit+"</p>\n" +
						"	<p>Kelvin: "+kelvin+"</p><br><br>\n" +
						"<a href=\"http://localhost:8080/_jweb-e001/conversor.html\">Volver al conversor de temperaturas</a><br><br>	" +
						"<a href=\"http://localhost:8080/_jweb-e001/index.html\">Volver al menu principal</a><br><br>	" +
						"</body>\n" + 
						"</html>"
						);
			}else if(selector == 1) {
				//Esto es si la temperatura esta en fahrenheit
				celsius = (entrada - 32) * 5/9;
				fahrenheit = entrada;
				kelvin =((entrada - 32)*5/9) + 273.15;
				escribir.append(
						"<!DOCTYPE html>\n" + 
						"<html>\n" + 
						"<head>\n" + 
						"	<meta charset=\"UTF-8\">\n" + 
						"	<title>Resultado temperaturas</title>\n" + 
						"	</head>\n" + 
						"<body>\n" + 
						"	<h1>Los resultados son</h1>\n" + 
						"	<p>Celsius: "+celsius+"</p>\n" +
						"	<p>Fahrenheit: "+fahrenheit+"</p>\n" +
						"	<p>Kelvin: "+kelvin+"</p><br><br>\n" +
						"<a href=\"http://localhost:8080/_jweb-e001/conversor.html\">Volver al conversor de temperaturas</a><br><br>	" +
						"<a href=\"http://localhost:8080/_jweb-e001/index.html\">Volver al menu principal</a><br><br>	" +
						"</body>\n" + 
						"</html>"
						);
			}else if(selector == 2) {
				//Esto es si la temperatura esta en kelvin
				celsius = entrada - 273.15;
				fahrenheit = ((entrada - 273.15)*9/5)+32;
				kelvin =entrada;
				escribir.append(
						"<!DOCTYPE html>\n" + 
						"<html>\n" + 
						"<head>\n" + 
						"	<meta charset=\"UTF-8\">\n" + 
						"	<title>Resultado temperaturas</title>\n" + 
						"	</head>\n" + 
						"<body>\n" + 
						"	<h1>Los resultados son:</h1>\n" + 
						"	<p>Celsius: "+celsius+"</p>\n" +
						"	<p>Fahrenheit: "+fahrenheit+"</p>\n" +
						"	<p>Kelvin: "+kelvin+"</p><br><br>\n" +
						"<a href=\"http://localhost:8080/_jweb-e001/conversor.html\">Volver al conversor de temperaturas</a><br><br>	" +
						"<a href=\"http://localhost:8080/_jweb-e001/index.html\">Volver al menu principal</a><br><br>	" +
						"</body>\n" + 
						"</html>"
						);
			}
		}catch(Exception e) {
			escribir.append(
					"<!DOCTYPE html>\n" + 
							"<html>\n" + 
							"<head>\n" + 
							"	<meta charset=\"UTF-8\">\n" + 
							"	<title>Resultado temperaturas</title>\n" + 
							"	</head>\n" + 
							"<body>\n" + 
							"	<h1>Los resultados son:</h1>\n" + 
							"	<p>La entrada solo admite numeros</p>\n" +
							"<a href=\"http://localhost:8080/_jweb-e001/conversor.html\">Volver al conversor de temperaturas</a><br><br>	" +
							"<a href=\"http://localhost:8080/_jweb-e001/index.html\">Volver al menu principal</a><br><br>	" +
							"</body>\n" + 
							"</html>"
					);
		}
		
		
	}

}