📄 listpersonservlet.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 java.io.*;import java.util.List;import javax.servlet.*;import javax.servlet.http.*;import javax.persistence.PersistenceUnit;import javax.persistence.EntityManagerFactory;import javax.persistence.EntityManager;/** * The servlet class to list Persons from database */public class ListPersonServlet extends HttpServlet { @PersistenceUnit private EntityManagerFactory emf; /** 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, IOException { assert emf != null; //Make sure injection went through correctly. EntityManager em = null; try { em = emf.createEntityManager(); //query for all the persons in database List persons = em.createQuery("select p from Person p").getResultList(); request.setAttribute("personList",persons); //Forward to the jsp page for rendering request.getRequestDispatcher("ListPerson.jsp").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 "ListPerson servlet"; } // </editor-fold>}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -