KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > enterprise > web_jpa_war > servlet > ListPersonServlet


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package enterprise.web_jpa_war.servlet;
24
25 import java.io.*;
26 import java.util.List JavaDoc;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import javax.persistence.PersistenceUnit;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityManager;
33
34 /**
35  * The servlet class to list Persons from database
36  */

37 public class ListPersonServlet extends HttpServlet {
38     
39     @PersistenceUnit
40     private EntityManagerFactory emf;
41     
42     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
43      * @param request servlet request
44      * @param response servlet response
45      */

46     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
47     throws ServletException, IOException {
48         assert emf != null; //Make sure injection went through correctly.
49
EntityManager em = null;
50         try {
51             em = emf.createEntityManager();
52
53             //query for all the persons in database
54
List JavaDoc persons = em.createQuery("select p from Person p").getResultList();
55             request.setAttribute("personList",persons);
56             
57             //Forward to the jsp page for rendering
58
request.getRequestDispatcher("ListPerson.jsp").forward(request, response);
59         } catch (Exception JavaDoc ex) {
60             throw new ServletException(ex);
61         } finally {
62             //close the em to release any resources held up by the persistebce provider
63
em.close();
64         }
65       
66     }
67     
68     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
69
/** Handles the HTTP <code>GET</code> method.
70      * @param request servlet request
71      * @param response servlet response
72      */

73     protected void doGet(HttpServletRequest request, HttpServletResponse response)
74     throws ServletException, IOException {
75         processRequest(request, response);
76     }
77     
78     /** Handles the HTTP <code>POST</code> method.
79      * @param request servlet request
80      * @param response servlet response
81      */

82     protected void doPost(HttpServletRequest request, HttpServletResponse response)
83     throws ServletException, IOException {
84         processRequest(request, response);
85     }
86     
87     /** Returns a short description of the servlet.
88      */

89     public String JavaDoc getServletInfo() {
90         return "ListPerson servlet";
91     }
92     // </editor-fold>
93
}
94
Popular Tags