📄 createpersonservlet.java
字号:
/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the License). You may not use this file except in * compliance with the License. * * You can obtain a copy of the license at * https://glassfish.dev.java.net/public/CDDLv1.0.html or * glassfish/bootstrap/legal/CDDLv1.0.txt. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * Header Notice in each file and include the License file * at glassfish/bootstrap/legal/CDDLv1.0.txt. * If applicable, add the following below the CDDL Header, * with the fields enclosed by brackets [] replaced by * you own identifying information: * "Portions Copyrighted [year] [name of copyright owner]" * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. */package enterprise.web_jpa_war.servlet;import enterprise.web_jpa_war.entity.Person;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import javax.persistence.PersistenceUnit;import javax.persistence.EntityManagerFactory;import javax.persistence.EntityManager;import javax.annotation.Resource;import javax.transaction.UserTransaction;/** * The sevelet class to insert Person into database */public class CreatePersonServlet extends HttpServlet { @PersistenceUnit //The emf corresponding to private EntityManagerFactory emf; @Resource private UserTransaction utx; /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException { assert emf != null; //Make sure injection went through correctly. EntityManager em = null; try { //Get the data from user's form String id = (String) request.getParameter("id"); String firstName = (String) request.getParameter("firstName"); String lastName = (String) request.getParameter("lastName"); //Create a person instance out of it Person person = new Person(id, firstName, lastName); //begin a transaction utx.begin(); //create an em. //Since the em is created inside a transaction, it is associsated with //the transaction em = emf.createEntityManager(); //persist the person entity em.persist(person); //commit transaction which will trigger the em to //commit newly created entity into database utx.commit(); //Forward to ListPerson servlet to list persons along with the newly //created person above request.getRequestDispatcher("ListPerson").forward(request, response); } catch (Exception ex) { throw new ServletException(ex); } finally { //close the em to release any resources held up by the persistebce provider em.close(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold>}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -