English 中文(简体)
用于检索用户名和密码的标识
原标题:Login Using hibernate to retrieve Username and Password

我想有一页的标识,用于从我的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方法,但我知道什么是错的。

问题回答
session1.get(Users.class,userName);

正在寻找id(主要关键)作为<代码>用户Name的论点。 你们都可以改变表格,以便用户名称是主要的关键,或创建可选择由用户名找到的假体。

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

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

你的超文本档案中有一个错误。 你们必须把这种方法列为“员额”的方法。

方法要求.getParaile()仅用于检索数据类型。

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.

相反,你可以使用我的抽象法典:

<><>斯特凡>

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>

www.un.org/Depts/DGACM/index_spanish.htm 摘要代码。 此处为标识。 jsp page used for Store the beanbject tomap with You html file and You java pojo category.

<% 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 %>" />

www.un.org/Depts/DGACM/index_spanish.htm 《婚姻法典》:

@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 !!!");
            }

    }

}

<DAO代码:

    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;
        }

    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签