Calculadora
Autor: Carlos Chata Yauri Página 1
18-8-2012
CHAOLOO CALCULADORA
Calculadora
Autor: Carlos Chata Yauri Página 2
CALCULADORA CON JSP, SERVLET Y CLASES JAVA
usando Eclipse Helios
Estructura del proyecto.
Calculadora
Autor: Carlos Chata Yauri Página 3
Creación
Calculadora
Autor: Carlos Chata Yauri Página 4
Calculadora
Autor: Carlos Chata Yauri Página 5
Bienvenida.jsp
Calculadora
Autor: Carlos Chata Yauri Página 6
Solución.
Calculadora
Autor: Carlos Chata Yauri Página 7
Código.
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>
Calculadora
Autor: Carlos Chata Yauri Página 8
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>
Calculadora
Autor: Carlos Chata Yauri Página 9
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>
Solución.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>
Calculadora
Autor: Carlos Chata Yauri Página 10
<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>
Calculadora
Autor: Carlos Chata Yauri Página 11
<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"
Calculadora
Autor: Carlos Chata Yauri Página 12
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>
Calculadora
Autor: Carlos Chata Yauri Página 13
</tr>
</table>
</div>
</form>
</body>
</html>
Modelo.Opersciones.java
package modelo;
public class Operaciones {
String v1;
String v2;
String ope;
String 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 != "") {
Calculadora
Autor: Carlos Chata Yauri Página 14
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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.*;
/**
Calculadora
Autor: Carlos Chata Yauri Página 15
* 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 Sboton = request.getParameter("boton");
String Sv1 = request.getParameter("v1");
String Sv2 = request.getParameter("v2");
String 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;
}
Calculadora
Autor: Carlos Chata Yauri Página 16
} 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;
Calculadora
Autor: Carlos Chata Yauri Página 17
}
.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;
}