Show TOC Start of Content Area

Procedure documentation Using JDO with Web Components  Locate the document in its SAP Library structure

Use

You can work with the data store using JDO directly from a Web component – that is, JavaServer Pages (JSP) or a servlet.

Note

Although this scenario is possible, accessing the data store directly from a Web component is not considered a good style. It is therefore recommended to use a session bean, which implements the business logic. For more information, see Using JDO with Session Beans.

Procedure

JDO and Servlets

The life cycle of a servlet begins when its init() method is invoked. If you want to use JDO directly from the servlet, you should look up the PersistenceManagerFactory in this method:

Example

public class MyServlet extends HttpServlet {

   PersistenceManagerFactory pmf;

  

   public void init(){

      Context ctx = new InitialContext();

      pmf = (PersistenceManagerFactory) ctx.lookup("java:comp/env/jdo/defaultPMF");

   }

 

The PersistenceManager instance is obtained in the service method of the servlet. Here the actual work with the JDO instance is done:

Example

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

   PersistenceManager pm = pmf.getPersistenceManager();

   Transaction tx = pm.getCurrentTransaction();

   tx.begin();

 

   // work with JDO instances

  

   tx.commit();

   pm.close();

}

 

The example above employs the JDO transaction contract. However, you may also use the javax.transaction.UserTransaction interface for transaction demarcation. In this case we recommend that you first start the transaction, and then obtain the PersistenceManager reference. For more information about the UserTransaction interface, see Using Component-Managed JTA Transactions.

For more information about servlets, see Developing Servlets.

JDO and JSP

You can use the JSP technology instead of servlets to implement the same functionality. The JDO use should be implemented in the same way using the specific JSP syntax. For more information about JSPs, see Developing JSP Pages.

 

Recommendation

Since the focus in JSPs is on the Web design, they are supposed to contain as less Java code as possible. Therefore, you can create a JavaBean, in which you look up the PersistenceManagerFactory and implement the data access logic. Then you can invoke the bean from the JSP.

 

See also:

Working with Persistent Objects

 

 

End of Content Area