marcossousaPJ 30 de jan. de 2005
Salve,
você deve criar um método estático público em uma classe para verificar se o usuário é válido como este:
public static boolean login & # 40 ; String nome , String senha & # 41 ;
& # 123 ;
try
& # 123 ;
Class . forName & # 40 ; & quot ; org . gjt . mm . mysql . Driver & quot ; & # 41 ;;
Connection con = DriverManager . getConnection & # 40 ; & quot ; jdbc & # 58 ; mysql & # 58 ; //localhost:3306/Teste");
Statement stm = con . createStatement & # 40 ; & # 41 ;;
String sql = & quot ; SELECT nome FROM usuario & quot ; +
& quot ; WHERE nomeUsuario = ' & quot ; + nome + & quot ; ' & quot ; +
& quot ; AND senha = ' & quot ; + senha + & quot ; ' & quot ;;
ResultSet res = stm . executeQuery & # 40 ; sql & # 41 ;;
if & # 40 ; res . next & # 40 ; & # 41 ; & # 41 ;
& # 123 ;
res . close & # 40 ; & # 41 ;;
stm . close & # 40 ; & # 41 ;;
con . close & # 40 ; & # 41 ;;
return true ;
& # 125 ;
res . close & # 40 ; & # 41 ;;
stm . close & # 40 ; & # 41 ;;
con . close & # 40 ; & # 41 ;;
& # 125 ;
catch & # 40 ; ClassNotFoundException e & # 41 ;
& # 123 ;
System . out . println & # 40 ; e . toString & # 40 ; & # 41 ; & # 41 ;;
& # 125 ;
catch & # 40 ; SQLException e & # 41 ;
& # 123 ;
System . out . println & # 40 ; e . toString & # 40 ; & # 41 ; & # 41 ;;
& # 125 ;
catch & # 40 ; Exception e & # 41 ;
& # 123 ;
System . out . println & # 40 ; e . toString & # 40 ; & # 41 ; & # 41 ;;
& # 125 ;
return false ;
& # 125 ;
aí depois você manipula com o httpsession assim:
HttpSession secao = request.getSession(true);
secao.setAttribute("nomeUsuario", new String( nomeUsuario ) );
secao.setAttribute("senha", new String( senha) );
aí para verificar autenticidade:
secao .getAttribute & #40 ;"nomeUsuario" );
secao .getAttribute & #40 ;"senha");
// verifica autenticidade
if & #40 ;login( nomeUsuario, senha))
// sucesso
else
// dados incorretos
isso ajuda