0

I want to have a login page that uses hibernate to retrieve the userName and password from mySQL database.

my code is as below For my HTML page i have a form that uses the method get.

form action="PlaylistServlet" method="get">
<input type = "text" name ="userId" placeholder="userName">
<input type="password" name="password" placeholder="Password">
<input type="submit" value="submit">
</form>

Below is my servlet doGet method that passes data from the HTML page to service class and redirects back to login page if username and password pair is correct.

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    Users user1 = new Users();
    String userName = request.getParameter("userName");
    String password = request.getParameter("password");
    SetData sd = new SetData();
    Users us = sd.get(userName); //get method in service class. Line 38
    String passwordDB = us.getPassword();// line 39
    if (user1.checkPassword(password, passwordDB)) {
        response.sendRedirect("UserLogin.html");
    }
}

below is my service class get method that uses hibernate session to retrieve data from mySQL database. I pass in the username from the HTML page as the primary key for the session.get() method .

public Users get(String userName) {

    Users us = null;
    SessionFactory sf = null;
    Session session1 = null;
    try {
        sf = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
        session1 = sf.openSession();
        Transaction tx = session1.beginTransaction();

        us = (Users) session1.get(Users.class,userName); // line 64
        tx.commit();
        session1.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sf.close();
    }
    return us;

}

at the end of it all after running the code i get the error mesages below

java.lang.IllegalArgumentException: id to load is required for loading
at com.marv.service.SetData.get(SetData.java:64)
at com.marv.service.PlaylistServlet.doGet(PlaylistServlet.java:38)

java.lang.NullPointerException
at com.marv.service.PlaylistServlet.doGet(PlaylistServlet.java:39)

I believe that my problem is in the hibernate session get(Class clazz, Serializable id) throws HibernateException method, but i dont know what i am doing wrong.

Msinzore
  • 1
  • 1
  • 2
  • 3
  • i think you should see this:http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – soorapadman Oct 21 '15 at 03:42
  • And _never_ store plain passwords in a database - use a [secure hash](https://security.stackexchange.com/questions/211/how-to-securely-hash-passwords) instead! – Tobias Liefke Oct 21 '15 at 06:56

3 Answers3

0
session1.get(Users.class,userName);

is looking for the id (primary key) as the userName argument. Either you can change the table so the username is the primary key or create a dao that does a select to find the id by username.

hermitmaster
  • 155
  • 1
  • 10
0

session.get needs a pk. Also check your parameters\form variables, they seem inconsistent. Try this syntax for hibernate:

user = session.createCriteria(Users.class,"user") .add(Restriction.eq("user.username",username)).uniqueResult();

Forkmohit
  • 733
  • 3
  • 12
  • 31
0

There is one mistake in your HTML file. You have to post the method as method="post"

The method request.getParameter() only work for String data type.

In Hibernate data can be fetch from DB by session.get(Employee.class,Id) method and this method only works if we give second argument as Interger.

Instead this you can use my abstract code:

HTML code

form action="login.jsp" method="post">
<input type="text" name="empId" required>
<input type = "text" name ="userName" placeholder="userName">
<input type="password" name="passWord" placeholder="Password">
<input type="submit" value="submit">
</form>

Abstract code of login.jsp. Here login.jsp page used for store the bean objects to map with your html file and your java pojo class.

<% String path1 = "/LoginServlet";%>

    <jsp:useBean id="login" class="com.bean.Employee" scope="session">
        <jsp:setProperty name="login" param="empId" property="empId" />
        <jsp:setProperty name="login" param="userName" property="userName" />
        <jsp:setProperty name="login" param="passWord" property="passWord" />
    </jsp:useBean>

    <jsp:forward page="<%=path1 %>" />

Login Servlet Code:

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            PrintWriter out = response.getWriter();
            HttpSession session = request.getSession();

            Employee emp = (Employee)session.getAttribute("login");

            LoginDAO login = new LoginDAO();

            boolean flag = login.login(emp);

            if(flag == true)
            {
                out.print("Employee Login Successfully !!!");
            }
            else
            {
                out.print("Incorrect Username or Password !!!");
            }

    }

}

Login DAO code:

    public boolean login(Employee emp)
    {

        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();

        Transaction tx = session.beginTransaction();

        Employee employeeDatafromDB = (Employee)session.get(Employee.class,emp.getEmpId());


        boolean key = false;

        if(emp.getUserName().equals(employeeDatafromDB.getUserName()) && emp.getPassWord().equals(employeeDatafromDB.getPassWord()))
        {
            System.out.println("Employee Login Successfully !!!");
            return key = true;
        }
        else
        {
            System.out.println("Incorrect Username or Password !!!");
            return key = false;
        }

    }