如果我将 UsuarioDAO 中的代码放入 UsuarioServlet , JSP 效果很好。但我正在尝试使用 MVC , OO ,我无法理解我的代码有什么问题,因为我从 UsuarioDAO <调用创建的对象< / strong>在 UsuarioServlet 中,理论上应该很好用,但事实并非如此。如果有人能告诉我我做错了什么,我很感激。
Usuario.java
package modelo;
public class Usuario {
private String nome;
private String login;
private String senha;
private int tipo;
private String cpf;
public void setNome(String nome) {
this.nome = nome;
}
public void setLogin(String login) {
this.login = login;
}
public void setSenha(String senha) {
this.senha = senha;
}
public void setTipo(int tipo) {
this.tipo = tipo;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getNome() {
return nome;
}
public String getLogin() {
return login;
}
public String getSenha() {
return senha;
}
public int getTipo() {
return tipo;
}
public String getCpf() {
return cpf;
}
}
UsuarioDAO.java
package controle;
import modelo.Usuario;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
public class UsuarioDAO {
private Connection con;
public void init(ServletConfig config) throws ServletException {
final String url = "jdbc:oracle:thin:@oracle.inf.poa.ifrs.edu.br:1521:XE";
final String us = "kar";
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(ex1.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con = DriverManager.getConnection(url, us, us);
} catch (SQLException ex) {
throw new ServletException(ex);
}
}
public List<Usuario> list() throws SQLException {
List<Usuario> lista = new ArrayList<>();
try (
Connection connection = con;
PreparedStatement statement = connection.prepareStatement("SELECT * FROM JDBC_USUARIO");
ResultSet rs = statement.executeQuery();) {
while (rs.next()) {
Usuario usuario = new Usuario();
usuario.setNome(rs.getString(1));
usuario.setLogin(rs.getString(2));
usuario.setSenha(rs.getString(3));
usuario.setTipo(rs.getInt(4));
usuario.setCpf(rs.getString(5));
lista.add(usuario);
}
}
return lista;
}
}
UsuarioServlet.java
package controle;
import modelo.Usuario;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "UsuarioServlet", urlPatterns = {"/UsuarioServlet"})
public class UsuarioServlet extends HttpServlet {
UsuarioDAO usrDAO = new UsuarioDAO();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<Usuario> retorno = usrDAO.list();
request.setAttribute("listaJSP", retorno);
request.getRequestDispatcher("/WEB-INF/listarUsuarios.jsp").forward(request, response);
} catch (SQLException e) {
throw new ServletException("Erro ao obter os dados", e);
}
}
}
listarUsuarios.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Servlet Exibe Resultado</title>
</head>
<body>
<TABLE BORDER=“1”>
<th>ID</th>
<th>Nome</th>
<th>Login</th>
<th>Senha</th>
<th>Tipo</th>
<th>CPF</th>
<c:forEach items="${listaJSP}" var="show" >
<c:set var="i" value="${i+1}" />
<tr>
<td><c:out value="${i}" /></td>
<td><c:out value="${show.nome}" /></td>
<td><c:out value="${show.login}" /></td>
<td><c:out value="${show.senha}" /></td>
<td><c:out value="${show.tipo}" /></td>
<td><c:out value="${show.cpf}" /></td>
</tr>
</c:forEach>
</table>
</body>
</html>
答案 0 :(得分:2)
您收到此错误是因为init()
上的UsuarioDAO
方法从未被调用过,而且此行
List<Usuario> retorno = usrDAO.list();
返回null
。
在调用列表之前尝试usrDAO.init()
。
原因可能是UsuarioDAO
不是httpServlet
,因此init()
方法未在创建时运行