import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ServletExemplo1 extends HttpServlet implements SingleThreadModel
{
 String Name ="", Value ="";
 Enumeration Values;
 String NomeUsuario="", SenhaUsuario="", Matricula="";
 boolean NomeUsuarioOK = false, SenhaUsuarioOK = false;
 Connection con = null;
 Statement st = null;
 ResultSet rs = null;
  
  public void init()
  {
  	System.out.println("Carregando o driver");
  	try{
	  	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	  	System.out.print("Abrindo a conexao...");
	  	con = DriverManager.getConnection("jdbc:odbc:teste");
	  }
	  catch(SQLException sql)
	  {
	  	System.out.println("Erro conectando ao db: ");
	  	sql.printStackTrace(System.out);
	  
	  }
	  catch(ClassNotFoundException sql)
	  {
	  	System.out.println("Erro registrando o Driver: ");
	  	sql.printStackTrace(System.out);
	  
	  }
	  
  	System.out.println("OK");
}
  
 public void service(HttpServletRequest Pedido, HttpServletResponse Resposta)
                    throws ServletException, IOException
 {
  Resposta.setContentType("text/html");
  PrintWriter SaidaCliente = Resposta.getWriter();
  
  try
  {
   String IdentificacaoFormulario = Pedido.getParameterValues("NomeFormulario")[0];
   System.out.println("Metodo: "+ Pedido.getMethod());
   System.out.println("Formulário : " + IdentificacaoFormulario);
   Values = Pedido.getParameterNames();

   while(Values.hasMoreElements())
   {
    Name = (String)Values.nextElement();
    Value = Pedido.getParameterValues(Name)[0];
    System.out.println("Nome-> "+Name+"    Valor-> "+Value);
   
		if (Name.compareTo("NomeUsuario") == 0)
    		{   
		        NomeUsuario = Value;
	    	}
		if (Name.compareTo("SenhaUsuario") == 0)
	    	{
			SenhaUsuario = Value;
	    	}
		if (Name.compareTo("Matricula") == 0)
	    	{
			Matricula= Value;
		}
	}
	
	{
	String sql = "select nome, senha, matricula from teste where nome='"+NomeUsuario+"' and senha='"+ SenhaUsuario +"' and matricula='"+ Matricula +"'";
	System.out.println("SQL: "+sql);
	st = con.createStatement();
	rs = st.executeQuery(sql);
	if(rs.next())
	{
    		NomeUsuarioOK = true;
    		SenhaUsuarioOK = true;
    		st.close();
	 	rs.close();	
	}
	else
	{
		NomeUsuarioOK = false;
    		SenhaUsuarioOK = false;
    		st.close();
	 	rs.close();
	}
	}
	/*{
	String sql = "update matricula from teste set matricula='"+ Matricula +"'";
	System.out.println("SQL: "+sql);
	st = con.createStatement();
	rs = st.executeQuery(sql);
	if(rs.next())
	{
    		st.close();
	 	rs.close();	
	}
	}*/

  if ((NomeUsuarioOK) && (SenhaUsuarioOK))
   {
    SaidaCliente.println("<HTML>");
    SaidaCliente.println("<HEAD>");
    SaidaCliente.println("<TITLE> Resposta do Servlet - Lista de Arquivos para Download </TITLE>");
    SaidaCliente.println("</HEAD>");
    SaidaCliente.println("<BODY LINK=#0000FF VLINK=#FF0000 ALINK=#FF0000 BACKGROUND=#000000>");
    SaidaCliente.println("<H2><CENTER> Arquivos Disponíveis</CENTER></H2></CENTER>");  
    SaidaCliente.println("Senha do Usuario: <FONT COLOR = #0000FF> "+ SenhaUsuario +"</FONT><BR><BR>");
    SaidaCliente.println("Nome do usuário <FONT COLOR = #0000FF>" + NomeUsuario +"</FONT><BR><BR>");
    SaidaCliente.println("Busca n de matricula<FONT COLOR = #0000FF>" + Matricula +"</FONT><BR><BR>");
    SaidaCliente.println("</BODY></HTML>"); 
    SaidaCliente.close();
   }
   else
   {
    SaidaCliente.println("<HTML>");
    SaidaCliente.println("<HEAD>");
    SaidaCliente.println("<TITLE> Resposta do Servlet - Lista de Arquivos para Download </TITLE>");
    SaidaCliente.println("</HEAD>");
    SaidaCliente.println("<BODY LINK=#0000FF VLINK=#FF0000 ALINK=#FF0000 BACKGROUND=#000000>");
    SaidaCliente.println("<H2><CENTER> Arquivos Disponíveis</CENTER></H2></CENTER>");  
    SaidaCliente.println("Senha do Usuario errada: <FONT COLOR = #0000FF> "+ SenhaUsuario +"</FONT><BR><BR>");
    SaidaCliente.println("Nome do usuário errado <FONT COLOR = #0000FF>" + NomeUsuario +"</FONT><BR><BR>");
    SaidaCliente.println("Busca n de matricula<FONT COLOR = #0000FF>" + Matricula +"</FONT><BR><BR>");
    SaidaCliente.println("</BODY></HTML>"); 
    SaidaCliente.close();
   }
  }
  catch (SQLException sql)
  {
  	System.out.println("Erro na conexao com o banco");
  	sql.printStackTrace(System.out);
  }
  catch(Exception Erro)
  {
   Erro.printStackTrace();
   SaidaCliente.println("Ocorreu um problema durante o registro de suas respostas...");
  }
  SaidaCliente.close();
 }
 
 public void destroy()
 {
 	System.out.print("Fechando.. ");
 	try{
 		st.close();
 		rs.close();
 		con.close();
 	}
 	catch(SQLException sql)
 	{
 	   System.out.println("Erro fechando a conexao ");
 	}
 	System.out.println("OK");
}
}