java.lang.NullPointerException- login servlet [duplicate]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我最近开始编程java。我在servlet中得到了java.lang.NullPointerException。代码如下。有人可以帮我吗?

我正在使用mysql-用户名,密码等是正确的。我可以看到表中的值。

控制台中的消息 -

显示java.lang.NullPointerException

在j2ee.Authenticate.processRequest(Authenticate.java:49)

在j2ee.Authenticate.doGet(Authenticate.java:93)

代码 -

package j2ee;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
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 = "Authenticate", urlPatterns = {"/Authenticate"})
public class Authenticate extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String user = request.getParameter("userId");
    String pwd = request.getParameter("passwd");
    String dbuser = null;
    String dbpwd = null;

    try
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/j2ee","root","password");

    String query = "select username, password from user_info where username=? && password=?";

     PreparedStatement p = con.prepareStatement(query);
     p.setString(1,dbuser);  
     p.setString(2,dbpwd);  

     ResultSet rs=p.executeQuery();





     if(dbuser.equals(user) && dbpwd.equals(pwd))
    {
            String s=request.getParameter(user);
            out.println("Welcome" +s); 
            RequestDispatcher rd = request.getRequestDispatcher("Home");
            rd.include(request, response);

    }   
    else
    {
            out.println("Please enter a valid Username and Password!"); 
            RequestDispatcher rd = request.getRequestDispatcher("Login");
            rd.include(request, response);

    }

}
    catch (IOException e)
    {
    System.out.println(e);
    } catch (ClassNotFoundException e) {
        System.out.println(e);
    } catch (SQLException e) {
        System.out.println(e);
    } catch (ServletException e) {
        System.out.println(e);
    }


}
java mysql servlets
3个回答
0
投票

dbuser和dbpwd初始化为null,并且从未分配过值。看起来您正在尝试针对表验证登录,但您永远不会从ResultSet对象中读取结果。我想你想这样做:

String query = "select username, password from user_info where username=? && password=?";

     PreparedStatement p = con.prepareStatement(query);
     p.setString(1,user);  
     p.setString(2,pwd);  

     ResultSet rs=p.executeQuery()

     if ( rs.next() ) {
        dbuser = rs.getString(1);
        dbpwd  = rs.getString(2);
      }
     // etc ...

也许阅读如何在IDE和how to use result sets中调试


0
投票

请您滥用prepareStatement函数

String query = "select username, password from user_info where username=? && password=?";

PreparedStatement p = con.prepareStatement(query);
p.setString(1,dbuser);   //wrong, you are replacing ? with null
p.setString(2,dbpwd);    //wrong, you are replacing ? with null

你应该计算行数。

String query = "select count(*) from user_info where username=? && password=?";
p.setString(1,user);
p.setString(2,pwd);  
ResultSet rs=p.executeQuery()

int result = rs.getInt(1);
if(result >= 1){ 
   ..authen OK
}
else{
   ..authen failed
}

0
投票

您从未设置dbuser和dbpwd的值,您已使用null初始化它们,但您在PreparedStatement中使用了它。

© www.soinside.com 2019 - 2024. All rights reserved.