Calculadora Con JSP
Calculadora Con JSP
CHAOLOO
CALCULADORA
Pgina 1
Calculadora
CALCULADORA CON JSP, SERVLET Y CLASES JAVA usando Eclipse Helios
Estructura del proyecto.
Pgina 2
Calculadora
Creacin
Pgina 3
Calculadora
Pgina 4
Calculadora
Bienvenida.jsp
Pgina 5
Calculadora
Solucin.
Pgina 6
Calculadora
Cdigo. Contenedor.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <frameset cols="25%,*"> <frameset rows="50%,*"> <frame src="menu.jsp"> <frame src="descarga.jsp"> </frameset> <frameset> <frame src="bienvenida.jsp" name="contenido"> </frameset> </frameset> </html>
Pgina 7
Calculadora
Men.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <table border="1" width="200"> <tr> <th><a href="archivos/pa2.pdf" target="contenido">Caso</a></th> </tr> <tr> <th><a href="solucion.jsp" target="contenido">Solución</a></th> </tr> </table> </body> </html>
Caso.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <p> <a href="archivos/pa2.pdf" target="contenido"></a> </p> </body> </html>
Pgina 8
Calculadora
Bienvenida.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Programacion aplicada III Primer examen Parcial</h1> <h2>Prof: Richar Rodas</h2> </body> </html>
Descarga.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <p> <a href="archivos/CALCULADORA CON JSP.pdf" target="contenido">Descarga de la Solución</a> </p> </body>
Solucin.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <%!int visitas = 0;%> <%@page import="modelo.*"%> <link rel="stylesheet" type="text/css" href="css/estiloAll.css"> </head> <body> <form name="fomulario" method="post" action="ServletAll"> <div>
Pgina 9
Calculadora
<table class="tabla1" border="1" cellspacing="0" cellpadding="2"> <tr> <th class="th1">Calculadora</th> </tr> <tr> <td> <table class="tabla2"> <tr> <td>Valor 1</td> <td><input type="text" name="v1" class="text" value="<%if (request.getAttribute("Sv1") == null) out.println(""); else { out.println(request.getAttribute("Sv1")); }%>"> </td> </tr> <tr> <td>Operador</td> <td><input type="text" name="ope" class="text" value="<%if (request.getAttribute("Sop") == null) out.println(""); else { out.println(request.getAttribute("Sop")); }%>"> </td> </tr> <tr> <td>Valor 2</td> <td><input type="text" name="v2" class="text" value="<%if (request.getAttribute("Sv2") == null) out.println(""); else { out.println(request.getAttribute("Sv2")); }%>"> </td> </tr> <tr> <td>Resultado</td> <td><input type="text" name="resultado" class="text" value="<%if (request.getAttribute("Sresultado") == null) out.println(""); else { out.println(request.getAttribute("Sresultado")); }%>"> </td> </tr> <tr>
Pgina 10
Calculadora
<td colspan="2" align="right"> <table class="tabla3"> <tr> <td><input type="submit" name="boton" value="7" class="boton"> </td> <td><input type="submit" name="boton" value="8" class="boton"> </td> <td><input type="submit" name="boton" value="9" class="boton"> </td> <td><input type="submit" name="boton" value="/" class="boton"> </td> </tr> <tr> <td><input type="submit" name="boton" value="4" class="boton"> </td> <td><input type="submit" name="boton" value="5" class="boton"> </td> <td><input type="submit" name="boton" value="6" class="boton"> </td> <td><input type="submit" name="boton" value="*" class="boton"> </td> </tr> <tr> <td><input type="submit" name="boton" value="1" class="boton"> </td> <td><input type="submit" name="boton" value="2"
Pgina 11
Calculadora
class="boton"> </td> <td><input type="submit" name="boton" value="3" class="boton"> </td> <td><input type="submit" name="boton" value="-" class="boton"> </td> </tr> <tr> <td><input type="submit" name="boton" value="0" class="boton"> </td> <td><input type="submit" name="boton" value="^" class="boton"> </td> <td><input type="submit" name="boton" value="%" class="boton"> </td> <td><input type="submit" name="boton" value="+" class="boton"> </td> </tr> <tr> <td colspan="4"><input type="submit" name="boton" value="Procesar" class="procesar"> </td> </tr> </table> </td> </tr> </table> </td> </tr> <tr> <th> <% visitas++; out.println(visitas + " visita(s)"); %> </th>
Pgina 12
Calculadora
</tr> </table> </div> </form> </body> </html>
Modelo.Opersciones.java
package modelo; public class Operaciones { String String String String v1; v2; ope; resultado;
public Operaciones(String v1, String v2, String ope) { this.v1 = v1; this.v2 = v2; this.ope = ope; } public Operaciones() { } public double getSuma() { return Double.parseDouble(v1) + Double.parseDouble(v2); } public double getResta() { return Double.parseDouble(v1) - Double.parseDouble(v2); } public double getMultiplicacion() { return Double.parseDouble(v1) * Double.parseDouble(v2); } public double getDivision() { return Double.parseDouble(v1) / Double.parseDouble(v2); } public double getPotencia() { return Math.pow(Double.parseDouble(v1), Double.parseDouble(v2)); } public double getPorcentaje() { return Double.parseDouble(v1) * Double.parseDouble(v2) / 100; } public String getMsg() { String msg; if (v1 != "" && v2 != "" && ope != "") {
Pgina 13
Calculadora
msg = ""; } else { msg = "ERROR"; } return msg; } public String getResultado() { if (getMsg() == "") { switch (ope.charAt(0)) { case '+': resultado = getSuma() + ""; break; case '-': resultado = getResta() + ""; break; case '*': resultado = getMultiplicacion() + ""; break; case '/': resultado = getDivision() + ""; break; case '^': resultado = getPotencia() + ""; break; case '%': resultado = getPorcentaje() + ""; break; } } if (getMsg() == "ERROR") { resultado = getMsg(); } return resultado; } }
controladorServlet.ServletAll.java
package controladorServlet; import java.io.IOException; import import import import import import /** javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; modelo.*;
Pgina 14
Calculadora
* Servlet implementation class ServletAll */ @WebServlet("/ServletAll") public class ServletAll extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletAll() { 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 } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String String String String Sboton = request.getParameter("boton"); Sv1 = request.getParameter("v1"); Sv2 = request.getParameter("v2"); Sop = request.getParameter("ope");
boolean numero = false; if (!Sboton.equals("Procesar")) { for (int i = 0; i <= 9; i++) { if (Sboton.equals("" + i)) { numero = true; } } if (numero == true) { if (!Sv1.equals("")) { if (!Sv2.equals("")) { Sv1 = Sboton; Sv2 = ""; } else { Sv2 = Sboton; }
Pgina 15
Calculadora
} else { Sv1 = Sboton; } } else { Sop = Sboton; } request.setAttribute("Sv1", Sv1); request.setAttribute("Sv2", Sv2); request.setAttribute("Sop", Sop); } if (Sboton.equals("Procesar")) { Operaciones operaciones = new Operaciones(Sv1, Sv2, Sop); String Sresultado = operaciones.getResultado(); request.setAttribute("Sresultado", Sresultado); request.setAttribute("Sv1", Sv1); request.setAttribute("Sv2", Sv2); request.setAttribute("Sop", Sop); } request.getRequestDispatcher("solucion.jsp").forward(request, response); } }
Css.estiloAll.css
@CHARSET "ISO-8859-1"; div { text-align: center; } table.tabla1 { width: 270px; border-color: black; border-collapse: collapse; background-color:#E6E6E6; font-family:; } th.th1 { font-size:19px; font-family:; } table.tabla2 { text-align: left;
Pgina 16
Calculadora
} .boton { width: 35px; height:32px; }
.text { width:160px; } table td { font-family:tahoma; font-weight:bold; } .procesar{ font-size:17px; font-family:; font-weight:bold; width:160px; height:32px; }
Pgina 17