请对我的ConnectLogin和ServletValidLogin有所怀疑: 我的ConnectLogin
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);
}
}
我不知道出了什么问题,为什么在我的控制台中返回:连接数据库... 在我的网页上返回用户和密码invalids!我认为问题在于我的ConnectLogin但是什么?
答案 0 :(得分:0)
我会调查Spring,Spring Security,Spring提供了经过良好测试的系统来保护网站和管理数据库连接。
目前它支持由DB支持的JNDI支持的安全性,或者您只需对用户和角色进行硬编码即可。使用spring,您可以只保护站点或方法的各个部分
以下是有关安全性http://static.springsource.org/spring-security/site/tutorial.html
的更多信息答案 1 :(得分:0)
您的错误"可能是两件事之一。可能是您甚至没有连接到数据库。在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
声明
if
将评估为ServletValidaLogin
,您将看到错误消息。附注:您正在捕获很多异常而不输出/记录任何异常。您应该考虑在所有这些语句中添加if ( user == null )
语句。它将使您更容易调试。
如果你能够连接,那么执行查询后你的true
可能没有指向任何东西。如果情况println
将评估为ResultSet
中的if ( rs.next() )
,则您的方法将返回false
而不是用户。可能是你有一个错误的查询(不确定你是否应该收到错误信息,我很久没有使用普通的JDBC)。在任何情况下,直接访问mysql并添加假用户并尝试对该虚拟用户进行查询。如果你能找到他而不是你的问题可能在你的插入中(好的,所以3个可能的原因)。
<强>更新强>
您的实际问题似乎是您在ConnectLogin.java
方法中引用了错误的名称以获取密码。我们确定null
属性getRequest
实际上是name
并进行了必要的更改。
注意:强> 而不是
<input>
应该是
senha
(虽然由于你的user.setEmail(email);
user.setSenha(senha);
条件不是完全必要的,但将它们设置为查询返回的内容更有意义...至少对我而言。
另外,
user.setEmail(rs.getString("pf_email"));
user.setSenha(rs.getString("pf_senha"));
甚至不应该在这里,因为它不在你的select语句中。
最后(与您的主要问题无关),您应该认真考虑同步 if
。它不是线程安全的。如果您想知道原因,可以阅读以下答案开始。网上还有更多关于此的文章。
Is HttpSession thread safe, are set/get Attribute thread safe operations?