4 Replies Latest reply on Mar 12, 2009 10:01 AM by ts chuah

    JSF, JSP with EJB3

    ts chuah Newbie


      Can someone provide me the simple step to create a JSP page and link to the Entity Bean?

      My objective is to create a simple JSP page which allow me to save the data into the database. I have no idea how to do it?

      FYI, I know how to connect EJB3 to db and create a simple class. Thanks.

        • 1. Re: JSF, JSP with EJB3
          Peter Johnson Master

          Lookup up an entity bean within a servlet is no different than from anywhere else, use the @PersistenceContext annotation to get the EnitytManager and then use that to look up your entity bean. If you like, you can pass the entity bean off to the JSP for rendering.

          By the way, I hope you are running JBoss AS 5.0 - I don't think that the @PersistenceContext in a servlet is picked up in 4.2.x.

          • 2. Re: JSF, JSP with EJB3
            ts chuah Newbie

            Can you provide me with concrete example or provide me some useful link on how to create a simple jsp page because I am new in Java Development, may not understand what you are trying to say. Too advance for me. :(

            Yes, I am using JBoss 5.0

            • 3. Re: JSF, JSP with EJB3
              Peter Johnson Master

              Look at the books Core Servlets and JSPs and More Servlets and JSPs, both free downloads from http://books.coreservlets.com/ - they are somewhat dated, but they give many basics on servlet and JSP development.

              If you are just now getting into JSPs, I recommend that you avoid scriptlets (server-side Java script). Instead, all of your processing (looking up or saving the entity bean, in your case (actually, I would put that logic within a stateless session bean EJB)) should be done in a servlet, and then you should use JSP Expression Language (EL) and tag libraries (JSTL) to render the data.

              For an example of what I mean, download the source code for JBoss in Action (http://www.manning.com/jbossinaction), and look at these files:

              jbia-src/ch03/src/video/java/org/jbia/har/Video.java (the entity bean)
              jbia-src/ch03/src/video/java/org/jbia/har/VideoServlet.java (the servlet)
              jbia-src/ch03/src/video/webapp/index.jsp (the JSP)

              While that represents a Hibernate example, the concepts are the same. There are more example in that source code bundle.

              • 4. Re: JSF, JSP with EJB3
                ts chuah Newbie

                Hi Peter,

                Thank you very much !!!