E ae blz
Então eu comecei montando o meu programa por partes, primeiro fiz o simples crieu uma página index.jsp que pede usuário e senha essa página manda a informação para um servlet que captura os dados e manda para uma interface de usuários que valida os dados e envia para o DAO que faz a conexão com banco de dado e por fim exibe em outra JSP, porém não estou conseguindo colocar os filtros para validar o login, por exemplo se o cara digita os dados certos é exibido os dados dele no jsp, se digita errado ele simplesmente exibe a jsp vazia.
Estudei o seu tutorial mas não estou conseguindo aplicar no meu projeto.
Olha como ele esta:
index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<br/>
<form method="POST" action="Home.do">
nome: <input type="text" name="nome">&nbsp;&nbsp<input type="submit" value="Consultar">
</form>
</body>
</html>
ValidaUsuario.class
/*
* ValidaUsuario.java
*
* Created on 17 de Julho de 2007, 22:04
*/
package Validacao;
import DAO.DAOUsuario;
import java.io.*;
import java.net.*;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;
import Interfaces.InterfaceUsuario;
/**
*
* @author alexandre
* @version
*/
public class ValidaUsuario extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
private String nome;
private String usuario;
private int erro;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, SQLException
{
nome = request.getParameter("nome");
// System.out.println("nome teste :" + nome);
InterfaceUsuario validar = new InterfaceUsuario();
validar.setNome(nome);
erro = validar.getErro();
if(erro == 0)
{
DAOUsuario resultado = new DAOUsuario();
resultado.setNome(nome);
request.setAttribute("usuario",resultado);
// request.setAttribute("lista",resultado);
RequestDispatcher view = request.getRequestDispatcher("home.jsp");
view.forward(request,response);
}
else
{
System.out.println("Erro aqui");
}
}
}
InterfaceUsuario
/*
* InterfaceUsuario.java
*
* Created on 17 de Julho de 2007, 22:07
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Interfaces;
/**
*
* @author alexandre
*/
public class InterfaceUsuario
{
private String nome;
private int erro = 0;
/** Creates a new instance of InterfaceUsuario */
public InterfaceUsuario()
{
}
public void setNome(String Nome)
{
this.nome = Nome;
if(nome.length()<1)
{
erro = 1;
}
}
public String getNome()
{
return nome;
}
public int getErro()
{
return erro;
}
}
Classe de array
/*
* ClassArray.java
*
* Created on 27 de Agosto de 2007, 21:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package Interfaces;
/**
*
* @author alexandre
*/
public class ClassArray
{
private String nome;
private String cargo;
private String usuario;
private String senha;
public String getNome()
{
return nome;
}
public void setNome(String nome)
{
this.nome = nome;
}
public String getCargo()
{
return cargo;
}
public void setCargo(String cargo)
{
this.cargo = cargo;
}
public String getUsuario()
{
return usuario;
}
public void setUsuario(String usuario)
{
this.usuario = usuario;
}
public String getSenha()
{
return senha;
}
public void setSenha(String senha)
{
this.senha = senha;
}
}
DAO
/*
* DAOUsuario.java
*
* Created on 17 de Julho de 2007, 22:18
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package DAO;
import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
import conexao.Conexao;
import Interfaces.ClassArray;
/**
*
* @author alexandre
*/
public class DAOUsuario
{
private String nome;
private String usuario;
private ResultSet rs;
private Connection conn;
private Statement sql;
//private ArrayList lista;
/** Creates a new instance of DAOUsuario */
public DAOUsuario() throws SQLException, IOException
{
conn = Conexao.connect();
}
public void setNome(String aNome) throws SQLException, IOException
{
nome = aNome;
PreparedStatement sql = conn.prepareStatement("select * from senhadeals where usuario = ?");
sql.setString(1,nome);
rs = sql.executeQuery();
while(rs.next())
{
usuario = rs.getString("nome");
}
}
public List getLista() throws SQLException
{
PreparedStatement sql = conn.prepareStatement("select * from senhadeals");
rs = sql.executeQuery();
List lista = new ArrayList();
while(rs.next())
{
lista.add(rs.getString("nome"));
}
return lista;
}
public List getDados() throws SQLException
{
PreparedStatement sql = conn.prepareStatement("select * from senhadeals");
rs = sql.executeQuery();
List dados = new ArrayList();
ClassArray array;
while(rs.next())
{
array = new ClassArray();
array.setNome(rs.getString("nome"));
array.setCargo(rs.getString("cargo"));
array.setUsuario(rs.getString("usuario"));
array.setSenha(rs.getString("senha"));
dados.add(array);
}
return dados;
}
public String getUsuario()
{
return usuario;
}
}
Conexão
/*
* Conexao.java
*
* Created on 17 de Julho de 2007, 21:58
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package conexao;
/**
*
* @author alexandre
*/
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author alexandre
* @version
*/
public class Conexao {
private static String NAME = "com.mysql.jdbc.Driver";
private static String URL = "jdbc:mysql://localhost/fidelity";
private static String LOGIN = "root";
private static String PASS = "";
private static Connection conn;
/**
*
* @return Conexao.
* @throws SQLException
* @throws IOException
*/
public static Connection connect() throws SQLException, IOException {
//Connection conn = null;
try {
Class.forName(NAME);
conn = DriverManager.getConnection(URL, LOGIN, PASS);
}
catch (ClassNotFoundException e)
{
System.out.print("\nNão foi possível estabelecer conexão com a base de dados.\n");
e.printStackTrace();
return null;
}
return conn;
}
}
Essa é a JSP que exibe os resultados:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>JSP Page<br></h1>
String de nome simples: <font color="red"> ${usuario.usuario} </font><br>
<br>
exemplo de List 1:<font color="blue">
<table>
<c:forEach var="nomes" items="${usuario.lista}">
<tr>
<td>${nomes}</td>
</tr>
</c:forEach>
</table></font><br><br>
exemplo de List 2:<font color="blue">
<table>
<c:forEach var="detalhes" items="${usuario.dados}">
<tr>
<td><c:out value="${detalhes.nome}" /> <c:out value="${detalhes.cargo}" /> <c:out value="${detalhes.usuario}" /> <c:out value="${detalhes.senha}" /></td>
</tr>
</c:forEach>
</table>
</body>
</html>
O XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<servlet>
<servlet-name>ValidaUsuario</servlet-name>
<servlet-class>Validacao.ValidaUsuario</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidaUsuario</servlet-name>
<url-pattern>/Home.do</url-pattern>
</servlet-mapping>
</web-app>
Tentei muito e não consegui colocar o esquema de sessão nesse código, tentei usando o seu exemplo e de outros tutoriais que li! Será que alguém poderia me dar um força e me expĺicar como eu coloco um filtro com sessão para deixar esse login funcionando corretamente?
Valeu pela força