KickJava   Java API By Example, From Geeks To Geeks.

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


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 enterprise.web_jpa_war.entity.Person;
26 import java.io.*;
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 import javax.annotation.Resource;
34 import javax.transaction.UserTransaction JavaDoc;
35
36
37 /**
38  * The sevelet class to insert Person into database
39  */

40 public class CreatePersonServlet extends HttpServlet {
41     
42     @PersistenceUnit
43     //The emf corresponding to
44
private EntityManagerFactory emf;
45     
46     @Resource
47     private UserTransaction JavaDoc utx;
48
49     
50     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
51      * @param request servlet request
52      * @param response servlet response
53      */

54     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
55     throws ServletException {
56         assert emf != null; //Make sure injection went through correctly.
57
EntityManager em = null;
58         try {
59             
60             //Get the data from user's form
61
String JavaDoc id = (String JavaDoc) request.getParameter("id");
62             String JavaDoc firstName = (String JavaDoc) request.getParameter("firstName");
63             String JavaDoc lastName = (String JavaDoc) request.getParameter("lastName");
64             
65             //Create a person instance out of it
66
Person person = new Person(id, firstName, lastName);
67             
68             //begin a transaction
69
utx.begin();
70             //create an em.
71
//Since the em is created inside a transaction, it is associsated with
72
//the transaction
73
em = emf.createEntityManager();
74             //persist the person entity
75
em.persist(person);
76             //commit transaction which will trigger the em to
77
//commit newly created entity into database
78
utx.commit();
79             
80             //Forward to ListPerson servlet to list persons along with the newly
81
//created person above
82
request.getRequestDispatcher("ListPerson").forward(request, response);
83         } catch (Exception JavaDoc ex) {
84             throw new ServletException(ex);
85         } finally {
86             //close the em to release any resources held up by the persistebce provider
87
em.close();
88         }
89     }
90     
91     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
92
/** Handles the HTTP <code>GET</code> method.
93      * @param request servlet request
94      * @param response servlet response
95      */

96     protected void doGet(HttpServletRequest request, HttpServletResponse response)
97     throws ServletException, IOException {
98         processRequest(request, response);
99     }
100     
101     /** Handles the HTTP <code>POST</code> method.
102      * @param request servlet request
103      * @param response servlet response
104      */

105     protected void doPost(HttpServletRequest request, HttpServletResponse response)
106     throws ServletException, IOException {
107         processRequest(request, response);
108     }
109     
110     /** Returns a short description of the servlet.
111      */

112     public String JavaDoc getServletInfo() {
113         return "Short description";
114     }
115     // </editor-fold>
116
}
117
Popular Tags