Download Examen-2Parcial_MyHotmail

Document related concepts
no text concepts found
Transcript
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
Objetivo:
Elaborar una aplicación web que por medio de una base de datos permita el control de
acceso a usuarios mediante el correo electrónico y la contraseña, que pueda acceder a su
bandeja de entrada, mostrando los correos que ha recibido y finamelnet que pueda enviar
un correo electrónico a otros usuarios.
1.- Clase que realiza la conexión a la base de datos:
import java.sql.*;
/**
*
* @author Ivan
*/
public class Conexion {
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Connection dataBaseConnection= null;
Class.forName("com.mysql.jdbc.Driver");
String urlBD = "jdbc:mysql://localhost/web?user=root&password=ivan";
dataBaseConnection = DriverManager.getConnection(urlBD);
//java.sql.Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test",
"root", "ivan" );
return dataBaseConnection;
}
}
2.- La siguiente clase realiza las peticiones de los servidores, controla las opciones de
usuario, La clase contiene métodos de get y post. Sin embargo en la aplicación esta
configurada para utilizar el método post. Pese a esto contiene instrucciones de sql que
además de consultas permiten eliminar y actualizar el registro en las opciones prepara la
bandeja de entrada al usuario registrado.
//El método POST que utiliza FORM:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Extend HttpServlet class
public class ServletController extends HttpServlet {
// Method to handle GET method request.
boolean flag=true;
String color;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String action = request.getParameter("action");
try {
if (action.equals("alta")) {
Connection con = Conexion.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO producto VALUES(?,?,?)");
ps.setString(1, request.getParameter("isbn"));
ps.setString(2, request.getParameter("nombre"));
ps.setString(3, request.getParameter("precio"));
ps.execute();
String title = "Registro Agregado a la BAse de Datos";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+ "<ul>\n"
+ " <li><b>ISBN</b>: "
+ request.getParameter("isbn") + "\n"
+ " <li><b>Nombre</b>: "
+ request.getParameter("nombre") + "\n"
+ " <li><b>Precio</b>: "
+ request.getParameter("precio") + "\n"
+ "</ul>\n"
+" <a href=\"index.html\">Regresar</a> "
+ "</body></html>");
}
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
if (action.equals("baja")) {
Connection con = Conexion.getConnection();
PreparedStatement ps = con.prepareStatement("DELETE FROM producto WHERE isbn=?");
ps.setString(1, request.getParameter("isbn"));
ps.execute();
String title = "El registro se ha eliminado Exitosamente";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+" <a href=\"index.html\">Regresar</a> "
+ "</body></html>");
}
if(action.equals("modificacion")){
Connection con = Conexion.getConnection();
PreparedStatement ps = con.prepareStatement("UPDATE producto SET nombre=?, precio=?
WHERE isbn=?");
ps.setString(1, request.getParameter("nombre"));
ps.setString(2, request.getParameter("precio"));
ps.setString(3, request.getParameter("isbn"));
ps.execute();
String title = "El registro se ha modificado Exitosamente";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+" <a href=\"index.html\">Regresar</a> "
+ "</body></html>");
}
if(action.equals("consulta")){
Connection con = Conexion.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM producto WHERE isbn=?");
ps.setString(1, request.getParameter("isbn"));
ResultSet rs=ps.executeQuery();
rs.next();
String n1=rs.getString("nombre");
String n2=rs.getString("precio");
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
String n3=request.getParameter("isbn");
String title = "Informacion del registro: "+n3;
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+ "<ul>\n"
+ " <li><b>Nombre</b>: "
+ n1 + "\n"
+ " <li><b>Precio</b>: "
+ n2 + "\n"
+ "</ul>\n"
+" <a href=\"index.html\">Regresar</a> "
+ "</body></html>");
}
}
catch (ClassNotFoundException e) {
} catch (SQLException ae) {
}
}
// Method to handle POST method request.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String action = request.getParameter("action");
try {
if (action.equals("sesion")) {
Connection con = Conexion.getConnection();
PreparedStatement ps = con.prepareStatement("SELECT * FROM usuario WHERE login=?");
ps.setString(1, request.getParameter("login"));
//
ps.setString(2, request.getParameter("pass"));
ResultSet rs = ps.executeQuery();
rs.next();
String n1 = rs.getString("login");
String n2 = rs.getString("pass");
String n3 = request.getParameter("login");
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
//si entra a este if debe mostrar los correos del usuario con el select
if (n2.equals(request.getParameter("pass"))) {
PreparedStatement psy = con.prepareStatement("SELECT * FROM correo WHERE
destinatario=?");
psy.setString(1, n3);
ResultSet rsy = psy.executeQuery();
//mientras aya refistros
/*while(rsy.next()){
String dataBaseName = rsy.getString("asunto");
PrintWriter p= response.getWriter();
p.println(dataBaseName);
}*/
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + "Bandeja de entrada de "+n3+ "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + "Bandeja de entrada de " +n3+ "</h1>\n");
out.println("<table align=\"center\">");
out.println("<thead> <tr> <th>Remitente</th><th>Asunto</th> </tr> </thead>");
while (rsy.next()) {
String dataBaseName = rsy.getString("asunto");
String kienlomando = rsy.getString("remitente");
if (flag) {
color = "#99FFCC";
} else {
color = "#FFFFCC";
}
out.println("<tr style=\"background:" + color + " \"><td>"
+ "<input type=\"radio\" name=\"bds\" value=\"" + kienlomando + "\">"
+ kienlomando + "</input></td>");
out.println("<td><input type=\"radio\" name=\"bds\" value=\"" + dataBaseName + "\">"
+ dataBaseName + "</input></td><tr>");
flag = !flag;
}
out.println("</table>");
out.println("<a href=\"index.jsp\">Enviar Correo</a>"
+ "<br />"
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
+ "<br />"
+ " <a href=\"index.html\">Regresar</a> "
+ "</body></html>");
} else {
String title = "Error de Inicio de Sesion";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+ " <a href=\"iniciarsesion.html\">Regresar</a> "
+ "</body></html>");
}
}
if(action.equals("enviar")){
// out.println("<h1>Estas en enviar </h1>");
Connection con = Conexion.getConnection();
PreparedStatement ps = con.prepareStatement("INSERT INTO correo VALUES(?,?,?,?)");
ps.setString(1, null);
ps.setString(2, request.getParameter("asunto"));
ps.setString(3, request.getParameter("destinatario"));
ps.setString(4, request.getParameter("remitente"));
ps.execute();
String title = "El mensaje se ha enviado a "+request.getParameter("destinatario");
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 "
+ "transitional//en\">\n";
out.println(docType
+ "<html>\n"
+ "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n"
+ "<h1 align=\"center\">" + title + "</h1>\n"
+ "<ul>\n"
+ " <li><b>Asunto</b>: "
+ request.getParameter("asunto") + "\n"
+ " <li><b>Destinatario</b>: "
+ request.getParameter("destinatario") + "\n"
+ "</ul>\n"
+" <a href=\"index.html\">Regresar</a> "
+ "</body></html>");
}
} catch (ClassNotFoundException e) {
out.println("<center><h1>Hubo un error x_X</h1>" + " <br /> <a
href=\"iniciarsesion.html\">Regresar</a> </center>");
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
} catch (SQLException ae) {
out.println("<h1> <center>Hubo Otro Error x_x</h1>" + "<br /> <a
href=\"iniciarsesion.html\">Regresar</a> </center>");
}
}
}
3.- A continuación se presentan los formularios
Iniciar Sesion
Datos de Usuario
Login:
Pass:
Iniciar Sesion
 Regresar
<!-To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<body>
<h1>Iniciar Sesion </h1>
<form action="ServletController" method="POST">
<table border="1">
<thead>
<tr>
<th>Datos de Usuario</th>
</tr>
</thead>
<tbody>
<tr>
<td>Login: <input type="text" name="login" /></td>
</tr>
<tr>
<td>Pass: <input type="text" name="pass" /></td>
</tr>
<tr>
<td><input type="hidden" name="action" value="sesion" />
<input type="submit" value="Iniciar Sesion" />
</td>
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
</tr>
</tbody>
</table>
</form>
<li><a href="index.html">Regresar</a></li>
</body>
</html>
<%-Document : index
Created on : 15/04/2012, 02:19:06 AM
Author : Ivan
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<body>
<h1>Enviar Correo Electronico </h1>
<form action="ServletController" method="POST">
<table border="1">
<thead>
<tr>
<th colspan="2">Enviar Correo Electronico</th>
</tr>
</thead>
<tbody>
<tr>
<td>De:</td>
<td><input type="text" name="remitente" /></td>
</tr>
<tr>
<td>Para:</td>
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail
<td><input type="text" name="destinatario" /></td>
</tr>
<tr>
<td>Asunto:</td>
<td><input type="text" name="asunto" /></td>
</tr>
<tr>
<td><input type="hidden" name="action" value="enviar" /></td>
<td><input type="submit" value="Enviar Correo" /></td>
</tr>
</tbody>
</table>
</form>
<li><a href="index.html">Regresar</a></li>
</body>
</html>
4.-Capturas de pantalla
IPN ESCOM
LOPEZ MARTINEZ CESAR IVAN
PROGRAMACION DE SISTEMAS I
Examen 2 Parcial: MyHotmail