[b]Olá,
Sou iniciante em java, estou aprendendo pela apostila da Caelum fj21 e a minha dúvida é: como eu faço para verificar no banco de dados, se existe o email e a senha que vieram dos parametros, caso seja verdadeiro cria a sessão e direciona para a pagina home.jsp, caso seja falso redireciona para a pagina de erro. :?: :?: :?:
Segue o código:[/b]
JSP com os parametros:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Pagina Principal</title>
<link href="./CSS/index_pagina.css" rel="stylesheet" type="text/css" />
</head>
<body id = "corpo">
<div id="formato">
<form action="mvc" method="post">
<label id = "leb"><strong>E-mail:</strong></label>
<input type="text" name="email" id="username" value="" />
<label id = "leb"><strong>Senha:</strong></label>
<input type="password" name="senha" id="password" value="" />
<input type="hidden" name="logica" value="LoginLogout"/>
<input name="entrar" type="submit" id="buttons" value="Entrar" />
</form>
</div>
<div id="logo">
<img src="./IMG/Planejamento.gif"/>
</div>
<div id="cadastre_se">
<a href="cadastro.jsp" target="_blank">Cadastre-se!</a>
</div>
<div id = "corpo_meio">
<img src="./IMG/emp.gif" />
</div>
</body>
</html>
Servlet Controladora:
package Controle;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Contratos.Logica;
@WebServlet(description = "Servlet_Controladora", urlPatterns = { "/ControllerServlet" })
public class ControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String parametro = request.getParameter("logica");
String bolaDaVez = "ClassesProjetoLogica." + parametro;
try {
Class<?> classe = Class.forName(bolaDaVez);
Logica logica = (Logica) classe.newInstance();
logica.executa(request, response);
} catch (Exception e) {
throw new ServletException("A lógica de negócios causou uma exceção", e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
Classe que recebe e envia os parametros para o DAO:
package ClassesProjetoLogica;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import AplicBD.Altentic_DAO;
import Contratos.Logica;
import Modelo.Usuarios;
public class LoginLogout implements Logica {
public void executa(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email").trim();
String senha = request.getParameter("senha").trim();
Usuarios usuarios = new Usuarios();
usuarios.setemailUser(email);
usuarios.setSenhaUser(senha);
Altentic_DAO altenticar = new Altentic_DAO();
altenticar.Autenticar(usuarios);
}
}
E o DAO em questão: A dúvida surge aqui. Como eu faço para implementar a sessão?
package AplicBD;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import Modelo.Usuarios;
public class Altentic_DAO {
private Connection connection;
public Altentic_DAO(){
this.connection = new Fabrica_Conexao().getConnection();
}
public void Autenticar(Usuarios usuarios){
String sql = "select email, senha from mis_op_usuarios";
try{
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.execute();
stmt.close();
}catch(SQLException e){
throw new RuntimeException(e);
}
}
}
Atenciosamente!