HttpServletRequest对象在设置属性时给出null

时间:2017-02-10 08:33:40

标签: java spring-mvc session servlets

我在DAO类中使用HQL从数据库获取userId。但是当它在会话中设置它时,它会给出空指针异常。这是DAO类中的代码。

@Repository("loginDAO")
public class LoginDAOImpl implements LoginDAO{

    @Resource(name="sessionFactory")
    protected SessionFactory sessionFactory;
    HttpServletRequest request;
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
public Session getSession(){
    return sessionFactory.openSession();
}
    public boolean checkLogin(String userName, String userPassword) {

        Session session=sessionFactory.openSession();
        boolean userExist=false;
        String sql_query="from Users as o where o.userName=? and o.userPassword=?" ;
        Query query=session.createQuery(sql_query);
        query.setParameter(0, userName);
        query.setParameter(1, userPassword);
        List list=query.list();

        if((list.size()>0) && (list!=null)){
            userExist=true;
            //TO get the user id to insert into orders table 
            Users user=null;
            user=(Users) query.list().get(0);
            Long id=user.getId();
            System.out.println("Id is "+ id);
            **//This is giving Null Pointer
            request.getSession().setAttribute("id", id);**


            }
        session.close();
    if(userExist==false){System.out.println("user name or password wrong");}
        return userExist;
    }

}

我确信id有价值。 提前谢谢。

0 个答案:

没有答案