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.