Está en la página 1de 8

INFORME

1. MODELO
1.1. CBase.java

package Modelo;
import java.sql.*;
public class CBase {
private Connection con = null;
public String pcError = null;
public ResultSet rs = null;
public void onConnect(){ try
{
Class.forName("org.postgresql.Driver");
this.con=DriverManager.getConnection("jdbc:postgresql://localhost:5432/BD1","postgr
es","postgres");
if(con==null){
this.pcError = "Problema con la conexcion...Sorry.";
}
}
catch (Exception e){
this.pcError = "Problema con la conexcion...Sorry.";
}
}
public void onDisconnect(){ try
{
con.close();
} catch (Exception e){
this.pcError = "Problema al cerrar la Base de Datos.";
}
}
public void comSelect(String consulta){
Statement s; String
cad = " "; try{
onConnect();
s=con.createStatement();
if(consulta.substring(0,6).equalsIgnoreCase("SELECT"))
{
rs=s.executeQuery(consulta);

}
else
{
s.executeUpdate(consulta);
}
//onDisconnect();
}catch(Exception e){
this.pcError = "Problema al realizar la consulta...Sorry.";
onDisconnect();
}
}
}

1.2. CLogin.java
package Modelo; import
java.sql.*; public class
CLogin {
public String pcError = " ";
protected String Codigo = null;
public String Nombre = null;
protected String Clave = " ";

private String cad = "8aabj";


public void setCodigo(String Codigo) {
this.Codigo = Codigo;
}
public void setNombre(String Nombre) {
this.Nombre = Nombre;
}
public void setClave(String Clave) {
this.Clave = Clave;
}
public boolean CodigoExiste(String codigo)
{
CBase usuario = new CBase();
boolean van = false;
try{
String Consulta = "Select codigo, nombre, clave from usuarios where codigo =
'"+codigo+"'";
usuario.comSelect(Consulta);

// ResultSet results =usuario.rs;


while(usuario.rs.next())
{
this.Codigo = usuario.rs.getString("codigo");
this.Nombre = usuario.rs.getString("nombre");
this.Clave = usuario.rs.getString("clave");
van = true;

}
if(van) return
true;
else
{
this.pcError += "Codigo no existe<br/>";
return false;
}
}catch(Exception e){
this.pcError += e.toString();
return false;
}
}
public boolean Ingresar(String Codigo, String Contrasea)
{
if(this.CodigoExiste(Codigo) && this.Clave.equals(cad + Contrasea))
{
return true;
}
else
{
pcError += "Contrasea erronea<br/>";
return false;
}
}
}

1.3. Usuario.java
package Modelo;

public class Usuario {


String Codigo;
String Nombre;
String Clave; String
Mensaje; public
Usuario()
{
this.Codigo = "1234";
this.Nombre = null;
this.Clave = null;
this.Mensaje = " ";

}
public Usuario(String Codigo, String Nombre, String Clave, String Mensaje) {
this.Codigo = Codigo;
this.Nombre = Nombre;
this.Clave = Clave;
this.Mensaje = Mensaje;

}
public String getMensaje() {
return Mensaje;
}
public void setMensaje(String Mensaje) {
this.Mensaje = Mensaje;
}
public void setCodigo(String value)
{
this.Codigo = value;
}
public void setNombre(String value)
{
this.Nombre = value;
}
public void setClave(String value)
{
this.Clave = value;
}
public String getCodigo()
{
return this.Codigo;
}
public String getNombre()
{
return this.Nombre;

}
public String getClave()
{
return this.Clave;
}
}

2. CONTROLADOR
2.1. Controlar.java package
Controlador;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
Modelo.Usuario;

import Modelo.CBase;
import Modelo.CLogin;
import java.sql.*;
public class Controlar extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Usuario u1 = new Usuario();
String Codigo = request.getParameter("Codigo");
String Clave = request.getParameter("Clave");
if(Codigo.equals("") || Clave.equals(""))
{

u1.setMensaje("Llene los Campos");


u1.setCodigo(Codigo);
despachar(request,response,"index.jsp",u1);
}
else
{
CLogin usuario = new CLogin();
if(usuario.Ingresar(Codigo, Clave))
{

u1.setNombre(usuario.Nombre);

despachar(request,response,"hola.jsp",u1);
}
else
{
u1.setMensaje(usuario.pcError);
u1.setCodigo(Codigo);
despachar(request,response,"index.jsp",u1);
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
//mtodo para mandar a la vista correcta
public void despachar(HttpServletRequest request,HttpServletResponse
response,String url,Usuario u1) throws ServletException, IOException
{
request.getSession().setAttribute("user", u1);
request.getRequestDispatcher(url).forward(request, response);

}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}

2.2 Web.xml
Estas lineas deben estar dentro del web.xml

3. VISTAS
3.1. index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="user" class = "Modelo.Usuario" scope="session"/> <meta
HTTP-EQUIV="Content-Type" content="text/html; charset=utf-8" /> <html>
<head>
<title>Login</title>
</head>
<body>
<form action="Controlar.do">
<table>
<tr>
<td>Cdigo:</td>
<td><input type="text" id = "Codigo" maxlength="40" name="Codigo"
value=<%= user.getCodigo()%> autofocus /></td>
</tr>
<tr>
<td>Clave:</td>
<td><input type = "password" id = "Clave" maxlength = "20" name =
"Clave"/></td>
</tr><tr>
<td></td>
<td> Mensaje: <%= user.getMensaje() %> </td>
</tr>
<tr>
<td><input type = "submit" name="Boton1" value = "Iniciar" /></td>
</tr>
</table>
</form>
</body>
</html>

3.2. hola.jsp

<jsp:useBean id="user" class = "Modelo.Usuario" scope="session"/>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hola <%=user.getNombre() %></h1>
</body>
</html>

También podría gustarte