daily pastebin goal
51%
SHARE
TWEET

Codice

a guest Sep 24th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //PERSISTENCE
  2.  
  3. <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
  4.  
  5.     <persistence-unit name="corsoWebPersistence" transaction-type="RESOURCE_LOCAL">
  6.  
  7.     <jta-data-source>connessioneCorsoWeb</jta-data-source>
  8.    
  9.     <properties>
  10.         <property name="openejb.jpa.auto-scan" value="true"/>
  11.     </properties>
  12.        
  13.     </persistence-unit>
  14.  
  15. </persistence>
  16.  
  17.  
  18. //PERSONA SERLVET
  19. package it.corso.java.web;
  20.  
  21. import java.io.IOException;
  22.  
  23. import javax.ejb.EJB;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.annotation.WebServlet;
  26. import javax.servlet.http.HttpServlet;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29.  
  30. import it.corso.java.business.PersonaServiceBeanLocal;
  31. import it.corso.java.data.Persona;
  32.  
  33. /**
  34.  * Servlet implementation class Persona
  35.  */
  36. @WebServlet("/persona")
  37. public class PersonaServlet extends HttpServlet {
  38.     private static final long serialVersionUID = 1L;
  39.        
  40.     //OVVIAMENTE CI SERVE UNA EJB QUINDI LA CREIAMO
  41.     @EJB
  42.     PersonaServiceBeanLocal personaService;
  43.    
  44.     public PersonaServlet() {
  45.         super();
  46.     }
  47.  
  48.     /**
  49.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  50.      */
  51.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  52.    
  53.         //NEL REQUEST GET PARAMETER POSSIAM GIA CONTROLLARE SE E NULL , SE NO ASSOCIAMI ALLA VARIABILE METHOD CHE C STA SCRITT
  54.         String method = request.getParameter("method") != null ? request.getParameter("method") : "";
  55.         if (method.equals("add")) {
  56.             Persona p = new Persona();
  57.             p.setNome("MARIO");
  58.             p.setCognome("ROSSI");
  59.             p.setAnni(34);
  60.             personaService.inserisci(p);
  61.        
  62.         } else {
  63.            
  64.         }
  65.        
  66.    
  67.     }
  68.  
  69.     /**
  70.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  71.      */
  72.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  73.         doGet(request, response);
  74.     }
  75.  
  76. }
  77.  
  78.  
  79.  
  80. //PERSONASERVICEBEAN
  81. package it.corso.java.business;
  82.  
  83. import javax.ejb.Stateless;
  84. import javax.persistence.EntityManager;
  85. import javax.persistence.PersistenceContext;
  86.  
  87. import it.corso.java.data.Persona;
  88.  
  89. /**
  90.  * Session Bean implementation class PersonaServiceBean
  91.  */
  92. @Stateless
  93. public class PersonaServiceBean implements PersonaServiceBeanRemote, PersonaServiceBeanLocal {
  94.  
  95.     //QUA DEFINIZIAMO L'ENTITY MANAGER
  96.    
  97.     //COME UNITNAME PRENDIAMO IL NOME DA DENTRO PERSISTENCE.XML , E LO METTIAMO
  98.     @PersistenceContext(unitName="corsoWebPersistence")
  99.     EntityManager manager;
  100.    
  101.    
  102.    
  103.     public PersonaServiceBean() {
  104.        
  105.     }
  106.     //METODI CRUD
  107.    
  108.     public void inserisci(Persona p) {
  109.         manager.persist(p);
  110.     }
  111.  
  112.     public void aggiorna(Persona p) {
  113.        
  114.     }
  115.    
  116.  
  117.     public void cancella(Persona p) {
  118.         manager.remove(p);
  119.     }
  120.  
  121.     public Persona getPersonaById(Long id) {
  122.         return null;
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top