在Tomcat Server中部署2个J2EE应用程序之间共享jsp会话

时间:2011-06-29 22:47:02

标签: hibernate jsp session tomcat java-ee

你好我正在使用hibernate处理web应用程序,当我在eclipse web服务生成的项目中尝试这个应用程序时出现错误,我无法解决它

就是这样:

hibernate class not found exception

所以我必须在两个项目中工作,

那么如何在两个动态Web项目之间共享会话属性?

例如在我的第一个项目中,我这样登录:

        String Username=request.getParameter("UserName");
    String Password=request.getParameter("passWord");

    if(Username!=null && Password!=null ){

     Session hibernateSession = HibernateUtil.currentSession(); 
     Transaction tx = hibernateSession.beginTransaction(); 
     tx.commit(); 

   Query qEtudiant = hibernateSession.createQuery("from Etudinat where UserName =    :userSID and Password=:psw");
 qEtudiant.setParameter("userSID", Username);
 qEtudiant.setParameter("psw", Password);
 Etudinat e =(Etudinat) qEtudiant.uniqueResult();

 Query qEnseignant = hibernateSession.createQuery("from Enseignant where UserName = :userSID and Password=:psw");
 qEnseignant.setParameter("userSID", Username);
 qEnseignant.setParameter("psw", Password);
 Enseignant en =(Enseignant) qEnseignant.uniqueResult();

 Query qadmin = hibernateSession.createQuery("from Administrateur where UserName = :userSID and Password=:psw");
 qadmin.setParameter("userSID", Username);
 qadmin.setParameter("psw", Password);
 Administrateur admin =(Administrateur) qadmin.uniqueResult();



 if(e!=null){

    session.setAttribute("UserName",Username);
    session.setAttribute("Nom", e.getNom());
    session.setAttribute("Prenom", e.getPrenom());
    session.setAttribute("type", "Etudiant");
    HibernateUtil.closeSession();
    RequestDispatcher rd =request.getRequestDispatcher("index.jsp");
    rd.forward(request, response);

         }
     else 
         if(en!=null){

                             session.setAttribute("UserName",Username);
                        session.setAttribute("Nom",    en.getNom());
                        session.setAttribute("Prenom", en.getPrenom());
                        session.setAttribute("type", "Enseignant");
                        HibernateUtil.closeSession();
                         RequestDispatcher rd   =request.getRequestDispatcher("index.jsp");
                         rd.forward(request, response); 
                    }
         else
                if(admin!=null){

                                    session.setAttribute("UserName",Username);
                                  session.setAttribute("Nom", admin.getNom());
                                 session.setAttribute("Prenom",   admin.getPrenom());
                                session.setAttribute("type",   "Administrateur");
                                HibernateUtil.closeSession();
                                 RequestDispatcher rd =request.getRequestDispatcher("index.jsp");
                                 rd.forward(request, response);
                                }
                else{   HibernateUtil.closeSession();
                        RequestDispatcher rd =request.getRequestDispatcher("index.jsp?err=true");
                        rd.forward(request, response);

                    }

 }

1 个答案:

答案 0 :(得分:0)

你可以尝试将你的hibernate代码粘贴到第三个项目中,并通过JNDI查找将它暴露给前两个。这篇文章描述了如何将SessionFactory绑定到Tomcat的JNDI名称空间中:

http://community.jboss.org/wiki/UsingJNDI-boundSessionFactorywithTomcat41