0

I Have a Login page which is a JSP and a servlet where i get the username from the Login page using request.getParameter. I now want to insert that username into a mysql DB using hibernate. In my hibernate program, how i can access the username???

My servlet is - public class LoginPage extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {  
    String username= request.getParameter("username");
    System.out.println("Hi  " + username);

}

}

and Hibernate program is-

public class HibernateTest {

public static void main(String args[]){
    Login name = new Login();       
    name.setUserName("");//////This line shows error. What should be inside ""
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(name);

    session.getTransaction().commit();
    session.close();

}

}

The marked line shows error when i run.What should be inside "". How can i retrieve username from the servlet???

4

1 回答 1

0

我在您的代码中没有看到任何问题。如果两者都工作正常,您只需将所有内容移至您的 servlet 类:

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {  
    String username= request.getParameter("username");

    Login name = new Login();       
    name.setUserName(username);
    SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(name);

    session.getTransaction().commit();
    session.close();
}

当然,在一个健壮的应用程序中,我们将添加层并应用一些设计模式......

于 2013-04-12T01:39:56.167 回答