One, Javax.servlet.http.HttpSession is an abstract interface Its production: the Java EE Web program runs, will give each new visitor to create a httpsession, This session is the only representation of the user's identity. Note that the container (Tomcat,resin) is created automatically. Use: To store the user's frequently used information, such as: User name, permissions. For example, in the shopping cart program, store the goods that the user buys. Destruction: A certain time (related to the container), the user does not have any action, the session automatically destroyed. Method: HttpSession session = Request.getsession (); Common method Setattributesession.setattribute (key,value); So in another JSP or servlet, you can use Session.getattribute (key), get value like a map two, org.hibernate.session It is a handle object of the Hibernate operations database. The only resemblance to the previous session is that the name is a bit like the rest of the place. In general Hibernate programs, the session is manually acquired by the user and closed manually. In a regular project, the business layer gets sessionsession session = hibernatesessionfactory.opensession (); then passes this session to the DAO layer, persisting the data or other operations. A single business logic may invoke multiple DAO methods, such as bank transfers, which is a first-in-a-minus-increment process, so calls to 2 DAO methods (account minus, B account increase) are called. Therefore, you can use the same session generated by the business layer to do this a1 and A2 represent account entities. A1 is a, A2 is B. A1.setacount (A1.getacount () -1000), A2.setacount (A2.getacount () +1000);d ao.update (a1,session);d ao.update (A2, session); Transaction tx = Session.begintransaction (), Tx.commit (); finally in the business layer, closes the session session.close (), or calls HibernaTesessionfactory.closesession (session); better be able to add abnormal catch, such as the abnormal, instant rollback. Ensure the operation is not unexpected. try{...tx.commit ();} catch (Exception e) {tx.rollback ();} Finally{hibernatesessionfactory.closesession (session);}
HttpSession and Hibernate session