6 Replies Latest reply on Apr 26, 2007 5:38 AM by Pete Muir

    jndi - tomcat - jsp file

    Piotr Lizurek Newbie

      Hi!
      I have a little problem: in my jsp file I need to get instance of EntityManager. Could somebody tell me how to do this? In POJO classes I simply use @In annotation, but it doesn't work in jsp files.
      Please, help!

        • 1. Re: jndi - tomcat - jsp file
          Piotr Lizurek Newbie

           

          "zielonyplot" wrote:
          Hi!
          I have a little problem: in my jsp file I need to get instance of EntityManager. Could somebody tell me how to do this? In POJO classes I simply use @In annotation, but it doesn't work in jsp files.
          Please, help!


          I forget - if it is impossible in jsp files, maybe it can be done in servlets? Please, help me how to get EntityManager instance in jsp or servlets.

          • 2. Re: jndi - tomcat - jsp file
            Jacek Laskowski Newbie

            Witaj,

            What Tomcat version are you using? If it's lower than Tomcat 6, you should forget about expecting @PersistenceContext to work - servlets are managed components, but only for a servlet container that understands the notion of annotation processing that's introduced in Java EE 5. It should work with Tomcat 6 fine.

            Jacek

            • 3. Re: jndi - tomcat - jsp file
              Piotr Lizurek Newbie

              So, in my servlet, I simply need to write something like this:

              import java.io.*;
              import javax.servlet.*;
              import javax.servlet.http.*;
              
              public class SomeServlet extends HttpServlet
              {
              
               @PersistenceContext
               private EntityManager em;
              
               public void doGet(HttpServletRequest request,
               HttpServletResponse response)
               throws ServletException, IOException
               {
              
               List lista = em.createQuery("from Topologia").getResultList();
               ........
               ........
               ........
              
               }
              }
              


              And It will work, right?

              • 4. Re: jndi - tomcat - jsp file
                Pete Muir Master

                You can't get the EntityManager in a jsp page. If you want it in a servlet and are using a SMPC called entityManager you can do Component.getInstance("entityManager"), but you will need to transaction demarcation yourself. In general Seam/JSF without you needing to mix your presentation and business logic like that (there are still some cases where you have to use a servlet).

                • 5. Re: jndi - tomcat - jsp file
                  Piotr Lizurek Newbie

                  So I have one question more: should I write exactly Component.getInstance("entityManager") or it can be Component.getInstance("EntityManager")? Where this String is declared?

                  • 6. Re: jndi - tomcat - jsp file
                    Pete Muir Master

                     

                    "petemuir" wrote:
                    using a SMPC called entityManager