Boa tarde Colegas,
Estou estudando Java Web com o Curso da Caelum FJ-21. De acordo com a apostila é encorajado o uso de EL +JSTL para quem utiliza JSP (Limpo e Elegante).
Consegui realizar a autenticação da seguinte maneira: Criei Index.jsp com o formulario com action para autentica-usuario.jsp onde é efetuado busca dos parametros, instancias de POJO (JavaBean) e do DAO.
Qual a minha duvida? Como faço para usar EL+JSTL no meu Index.jsp
Segue abaixo codigos do que fiz.
Index.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ 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 Login</title>
</head>
<body>
<center>
<img src="imagens/login2.jpg" alt="" />
<p> </p>
<p> </p>
</center>
<form action="autentica-usuario.jsp" method="post">
<p align="center">
Usuario.:<input type="text" name="tf_usuario"/>
Senha.:<input type="password"" name="tf_senha"/>
</p>
<p align="center">
<input type="submit" value="Enviar"/>
<input type="reset" value="Limpar"/>
</p>
</form>
</body>
</html>
autentica-usuario.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@page import="br.com.neri.modelo.Login" %>
<%@page import="br.com.neri.dao.LoginDAO"%>
<%@ 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>Autentica Usuario</title>
</head>
<body>
<%
String status = "false";
String usuario = request.getParameter("tf_usuario");
String senha = request.getParameter("tf_senha");
Login lgUsuario = new Login();
lgUsuario.setLog_usuario(usuario);
lgUsuario.setLog_senha(senha);
LoginDAO lgDao = new LoginDAO();
status = lgDao.verificaLogin(lgUsuario);
if(status == "true")
{
response.sendRedirect("bemvindo.jsp");
}else{
response.sendRedirect("index.jsp");
}
%>
</body>
</html>
loginDao.java
package br.com.neri.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.neri.jdbc.ConnectionFactory;
import br.com.neri.modelo.Login;
public class LoginDAO {
private Connection connection;
public LoginDAO(){
this.connection = new ConnectionFactory().getConnection();
}
public void adiciona(Login login) throws SQLException{
String sqlAdiciona = "insert into login (log_usuario,log_senha,log_nivelacesso) values(?,?,?)";
try {
//prepara inserção
PreparedStatement stmt = connection.prepareStatement(sqlAdiciona);
//seta valores
stmt.setString(1, login.getLog_usuario());
stmt.setString(2, login.getLog_senha());
stmt.setString(3, login.getLog_nivelAcesso());
//executa
stmt.execute();
stmt.close();
} catch (SQLException erroSql) {
throw new RuntimeException(erroSql);
}
}
public String verificaLogin(Login login){
String sqlVerifica = "select * from login where log_usuario=? and log_senha=?";
String status = "";
try {
PreparedStatement stmt = connection.prepareStatement(sqlVerifica);
stmt.setString(1, login.getLog_usuario());
stmt.setString(2, login.getLog_senha());
ResultSet rs = stmt.executeQuery();
if(rs.next()){
status = "true";
}
} catch (SQLException erroSql) {
throw new RuntimeException(erroSql);
}
return status;
}
public List<Login> getLista(){
try{
List<Login> loginList = new ArrayList<Login>();
PreparedStatement stmt = this.connection.prepareStatement("select * from login");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
//cria objeto login
Login login = new Login();
login.setLog_usuario(rs.getString("log_usuario"));
login.setLog_senha(rs.getString("log_senha"));
login.setLog_nivelAcesso(rs.getString("log_nivelacesso"));
//adiciona o objeto a lista
loginList.add(login);
}
rs.close();
stmt.close();
return loginList;
}catch (SQLException erroSql) {
throw new RuntimeException(erroSql);
}
}
/*public void verifica(String u, String s){
String sqlVerifica = "select from login where log_usuario=? and log_senha=?";
try{
PreparedStatement stmt = connection.prepareStatement(sqlVerifica);
ResultSet rs = stmt.executeQuery();
rs.close();
stmt.close();
}catch(SQLException erroSql){
throw new RuntimeException(erroSql);
}
}*/
public void altera(Login login){
String sqlAltera = "update login set log_usuario=?,log_senha=?,log_nivelacesso=? where log_codigo=?";
try {
PreparedStatement stmt = connection.prepareStatement(sqlAltera);
stmt.setString(1, login.getLog_usuario());
stmt.setString(2, login.getLog_senha());
stmt.setString(3, login.getLog_nivelAcesso());
stmt.setLong(4, login.getLog_codigo());
stmt.execute();
stmt.close();
} catch (SQLException erroSql) {
throw new RuntimeException(erroSql);
}
}
public void remove(Login login){
String sqlRemove = "delete from login where log_codigo=?";
try {
PreparedStatement stmt = connection.prepareStatement(sqlRemove);
stmt.setLong(1, login.getLog_codigo());
stmt.execute();
stmt.close();
} catch (SQLException erroSql) {
throw new RuntimeException(erroSql);
}
}
}
[size=18]O QUE FAÇO?[/size]
