我想有一页的标识,用于从我的SQL数据库检索用户Name和密码。
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>
下面是我从超文本页面向服务班级传送数据的服务器单单,如果用户名和密码正确,则将数据转回正页。
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)
我认为,我的问题在于秘密会议(Class clazz, Serializable id)扔下了HibernateException方法,但我知道什么是错的。