View Javadoc
1   package ejava.examples.webtier.web;
2   
3   import java.io.IOException;
4   import java.io.PrintWriter;
5   
6   import javax.naming.InitialContext;
7   import javax.persistence.EntityManager;
8   import javax.persistence.PersistenceContext;
9   import javax.servlet.ServletException;
10  import javax.servlet.http.HttpServlet;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  import ejava.examples.webtier.dao.DAOFactory;
18  import ejava.examples.webtier.dao.DAOTypeFactory;
19  import ejava.util.jndi.JNDIUtil;
20  
21  @SuppressWarnings("serial")
22  public class JPADAOInit extends HttpServlet {
23      Log log = LogFactory.getLog(JPADAOInit.class);
24      
25      @PersistenceContext(unitName="webtier")
26      private EntityManager em;
27      
28      public void init() {
29          try {            
30              log.debug("initializing JPA DAOs");
31              DAOTypeFactory daoType = DAOFactory.getDAOTypeFactory();
32              log.debug("servlet got typeFactory:" + daoType);
33          }
34          catch (Throwable th) {
35              log.fatal("error initializing JPA",th);
36              super.getServletContext().log("error initializing JPA", th);
37          }
38      }
39  
40      protected void doGet(HttpServletRequest request, 
41                           HttpServletResponse response) 
42          throws ServletException, IOException {
43          
44          StringBuilder text = new StringBuilder();
45          try {
46              InitialContext jndi = new InitialContext();
47              log.debug(new JNDIUtil().dump(jndi,""));
48              log.debug(new JNDIUtil().dump(jndi,"java:comp/env"));
49          }
50          catch (Exception ex) {
51              text.append(ex.toString());            
52          }
53          
54          response.setContentType("text/html");
55          PrintWriter pw = response.getWriter();
56          pw.print("<html>");
57          pw.print("<body>");
58          pw.println("em=" + em);
59          pw.println(text);
60          pw.print("</body>");
61          pw.print("</html>");
62      }
63  
64      protected void doPost(HttpServletRequest request, 
65                            HttpServletResponse response) 
66           throws ServletException, IOException {
67          doGet(request, response);
68      }
69      
70      
71  }