验证用户,电子邮件和密码不起作用

时间:2014-12-23 09:43:36

标签: java database validation java-ee servlets

我在验证电子邮件和密码时遇到问题,无论它是否属于注册会员。我使用NetBeans,并使用表members创建了一个数据库。我已经设置了连接池并填充了我的成员表。

在实施代码,解决错误并观看了一些关于NetBeans教程的视频之后,我仍然无法弄清楚我在哪里错过它。我4天后就被困在那里,现在有点丢失了。希望你们能帮助我,提前谢谢。

这是我的会员表数据:

 # | id |       email     | password |  name
------------------------------------------------
 1 |  1 | what@what.com   |   what   | Number 1 
 2 |  2 | fireup@fire.com |   fire   | Number 2

这是我的LoginServlet的代码:

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
    @Resource(name = "jdbc/jed")
    private DataSource dsMembers;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String strName = request.getParameter("memberName");
        String strPassword = request.getParameter("memberPassword");
        String strEmail = request.getParameter("memberEmail");
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = dsMembers.getConnection();
            String sql = "SELECT * FROM members";
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            while (rs.next()){
                if((isValidEmail(strEmail))&& (strEmail.equals(rs.getString("email")))&& (strPassword.equals(rs.getString("password")))&&(strName.equals(rs.getString("name")))){
                    System.out.println("Success");

                   HttpSession session = request.getSession();
                    session.setAttribute("name", strName);
                    session.setAttribute("email", strEmail);
                    response.sendRedirect(this.getServletContext().getContextPath() + "/account.jsp");
                }
                else {
                    request.getSession().invalidate();
                    response.sendRedirect(this.getServletContext().getContextPath());
                }
            }
        } catch (SQLException ex) {
            System.out.println("SQLException: " + ex.getMessage());
        }
    }

    private boolean isValidEmail(String strEmail){
        if(strEmail.matches("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"))
            return true;
        return false;
    }
}

这是我收到的GlassFish Server日志:

  

信息:WEB0671:在[/ Project]加载应用程序[Project]       信息:项目已在286毫秒内成功部署       信息:WEB0671:在[/ Project]加载应用程序[Project]       信息:项目已在247毫秒内成功部署       信息:无效的用户,密码或电子邮件
      信息:无效的用户,密码或电子邮件
      警告:StandardWrapperValve [org.jedlibrary.lola.loan.ValidationServlet]:PWC1406:servlet org.jedlibrary.lola.loan.ValidationServlet的Servlet.service()抛出异常       java.lang.IllegalStateException       在org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:524)
      at org.jedlibrary.lola.loan.ValidationServlet.doPost(ValidationServlet.java:46)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:688)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:770)       在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
      在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      在com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      在com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)       at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)       在com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      在com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      在com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      在com.sun.grizzly.ContextTask.run(ContextTask.java:71)       在com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)
      在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)
      在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

当然它不起作用,您正在检查输入的电子邮件是email,密码是password还是名称是name

您应该检查ResultSet项,而不是常量。例如,而不是

strEmail.equals("email")

你应该写

strEmail.equals(rs.getString("email"))

修改

你错过了其他的东西:你没有检查整个ResultSet。第一个结果与您提供的数据不匹配,因此您可以立即使会话无效。当没有任何项匹配时,您应该只使其无效。

这可以解决您的问题:

while (rs.next()) {
    if (isValidEmail(strEmail) && strEmail.equals(rs.getString("email")) && strPassword.equals(rs.getString("password")) && strName.equals(rs.getString("name"))) {
        System.out.println("Success");
        HttpSession session = request.getSession();
        session.setAttribute("name", strName);
        session.setAttribute("email", strEmail);
        response.sendRedirect(this.getServletContext().getContextPath() + "/account.jsp");
        return;
    }
}
request.getSession().invalidate();
response.sendRedirect(this.getServletContext().getContextPath());

我的最后一点:你应该从不以这种方式实施登录(即使是作业),选择完整的表格,只需一条记录就是浪费资源

你应该使用PreparedStatementselect * from members where name = ?这样的语句,检查是否有结果,密码和电子邮件是否合适(我假设这个名称在数据库中是唯一的)