我对我的 ConnectLogin 和 ServletValidLogin 有一些疑问: 我的连接登录
package br.com.cad.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.cad.basica.Contato;
import br.com.cad.dao.ConnectDb;
public class ConnectLogin extends ConnectDb {
public Contato getContato( String email, String senha ){
Connection c = this.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try{
ps = c.prepareStatement("select pf_email, pf_senha from dados_cadastro where pf_email = ? and pf_senha = ?");
ps.setString(1, email);
ps.setString(2, senha);
rs = ps.executeQuery();
if ( rs.next() ){
Contato user = new Contato();
user.setEmail(email);
user.setSenha(senha);
user.setNome( rs.getString("pf_nome") );
return user;
}
}
catch (SQLException e){
e.printStackTrace();
}
finally{
if (rs != null ) {
try { rs.close(); } catch (SQLException e) { ; }
rs = null;
}
if (ps != null ) {
try { ps.close(); } catch (SQLException e) { ; }
ps = null;
}
if (c != null ) {
try { c.close(); } catch (SQLException e) { ; }
c = null;
}
}
return null;
}
}
我的Servlet:
package br.com.cad.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import br.com.cad.dao.ConnectLogin;
import br.com.cad.basica.Contato;
public class ServletValidaLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
HttpSession session = request.getSession();
Contato user = null;
String email = request.getParameter("email");
String senha = request.getParameter("password");
try {
ConnectLogin dao = new ConnectLogin();
user = dao.getContato(email, senha);
}
catch ( Exception e ){
}
if ( user == null ) {
session.invalidate();
request.setAttribute("msg", "Usuário ou senha inválidos");
request.getRequestDispatcher("login.jsp" ).forward(request, response);
}
else{
session.setAttribute("user", user);
request.getRequestDispatcher("home.jsp" ).forward(request, response);
}
}
}
还有我的 ConnectDb:
package br.com.cad.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectDb {
public Connection getConnection() {
try {
System.out.println("Connect to database...");
return DriverManager.getConnection("jdbc:mysql://localhost:3306/soa", "root", "wey123");
} catch(SQLException sqlException) {
throw new RuntimeException(sqlException);
}
}
我不知道出了什么问题以及为什么在我的控制台中返回:连接到数据库... 在我的网页中返回的用户和密码无效!我认为问题出在我的 ConnectLogin 上,但是什么?
最佳答案
您的“错误”可能是两件事之一。可能您甚至没有连接到数据库。在 ConnectDb.java
你正在扔一个RunTimeException
但在 ServletValidaLogin.java
你正在接受这个异常。添加System.out.println(e.getMessage())
在 catch
阻止以确保您确实能够连接。
try {
ConnectLogin dao = new ConnectLogin();
user = dao.getContato(email, senha);
}
catch ( Exception e ){
System.out.println(e.getMessage());
}
在这种情况下会发生的情况是,由于您未连接,因此您将转到 catch
block ,然后是你的if
ServletValidaLogin
中的声明
if ( user == null )
将评估为 true
您将看到错误消息。旁注:您捕获了很多异常,但没有输出/记录任何异常。您应该考虑添加 println
所有这些中的陈述。它将使您更容易调试。
如果您能够连接,那么可能是您的 ResultSet
执行查询后不指向任何内容。如果是这样if ( rs.next() )
将评估为 false
在ConnectLogin.java
你的方法将返回 null
而不是用户。可能是您的查询错误(不过不确定您是否应该收到错误消息,我已经很长时间没有使用纯 JDBC)了。无论如何,直接访问 mysql 并添加一个假用户,然后在该虚拟用户上尝试查询。如果您能找到他,那么您的问题可能出在您的插入中(好吧,有 3 个可能的原因)。
更新
您的实际问题似乎是您在 getRequest
中引用了错误的名称。密码的方法。我们确定name
对于属性 <input>
实际上是senha
并进行了必要的更改。
注意: 而不是
user.setEmail(email);
user.setSenha(senha);
应该是
user.setEmail(rs.getString("pf_email"));
user.setSenha(rs.getString("pf_senha"));
(虽然由于您的 if
条件而并非完全必要,但将它们设置为查询返回的内容更有意义......至少对我来说)。
另外,
user.setNome( rs.getString("pf_nome") );
甚至不应该出现在此处,因为它不在您的 select 语句中。
最后(与您的主要问题无关),您应该认真考虑同步 HttpSession
。它不是线程安全的。如果您想知道原因,可以阅读下面的答案来开始。网络上还有很多讨论此问题的文章。
Is HttpSession thread safe, are set/get Attribute thread safe operations?
关于java - 我的登录身份验证 Servlet 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17559838/