Correction Examen Jee 2019-2020
Correction Examen Jee 2019-2020
Correction Examen Jee 2019-2020
Module : JEE
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="CONTACTS")
public class Contact implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID_Contact")
private Long idContact;
@Column(name="Nom_Contact")
private String nom;
@Column(name="Prenom_Contact")
private String prenom;
@Column(name="Num_Portable_Contact")
private String numPortable;
@Column(name="Num_Fixe_Contact")
private String numFixe;
@Column(name="Adresse_Contact")
private String adresse;
public Contact(){
super();
}
@Override
public String toString() {
return "Contact [nom=" + nom + ", prenom=" + prenom + ",
numPortable="
+ numPortable + ", numFixe=" + numFixe + ",
adresse=" + adresse
+ "]";
}
}
Bean session sans état
package examen.session;
import java.util.List;
import javax.ejb.Remote;
import examen.entity.Contact;
@Remote
public interface GestionContact {
public void ajouterContact(Contact p);
public List<Contact> getAllContacts();
public List<Contact> getContactParAdresse(String a);
public Contact getContact(String nom,String prenom);
public void removeContact(String nom,String prenom);
public void updateContact(String nom,String prenom, String
numPort,String numFix,String adresse);
}
package examen.session;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import examen.entity.Contact;
@Stateless(name="CONT")
public class GestionContactBean implements GestionContact {
@PersistenceContext(name="UP_CONT")
private EntityManager em;
@Override
public void ajouterContact(Contact p) {
// TODO Auto-generated method stub
em.persist(p);
}
@Override
public List<Contact> getAllContacts() {
// TODO Auto-generated method stub
Query req =em.createQuery("select p from Contact p");
return req.getResultList();
}
@Override
public List<Contact> getContactParAdresse(String a) {
// TODO Auto-generated method stub
Query req =em.createQuery("select p from Contact p where
p.adresse like :x");
req.setParameter("x", "%"+a+"%");
return req.getResultList();
}
@Override
public Contact getContact(String nom, String prenom) {
// TODO Auto-generated method stub
Query req =em.createQuery("select p from Contact p where
p.nom like :x and p.prenom like :y");
req.setParameter("x", "%"+nom+"%");
req.setParameter("y", "%"+prenom+"%");
Contact c=null;
try{
c=(Contact) req.getSingleResult();}
catch (NoResultException nre){
//Ignore this because as per your logic this is ok!
}
if(c==null)throw new RuntimeException("Contact non
existant dans le carnet");
return c;
}
@Override
public void removeContact(String nom, String prenom) {
// TODO Auto-generated method stub
Contact c=getContact(nom,prenom);
em.remove(c);
}
@Override
public void updateContact(String nom, String prenom, String
numPort,
String numFix, String adresse) {
// TODO Auto-generated method stub
Contact c=getContact(nom,prenom);
if(numPort !=null)c.setNumPortable(numPort);
if(numFix !=null)c.setNumFixe(numFix);
if(adresse !=null) c.setAdresse(adresse);
em.persist(c);
}
}
Partie II :
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import examen.entity.Contact;
import examen.session.GestionContact;
Partie III :
package web;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import examen.entity.Contact;
import examen.session.GestionContact;
@WebServlet(name="servletContact",urlPatterns={"/controleur"})
public class ContactServlet extends HttpServlet {
@EJB
private GestionContact metier;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String action=request.getParameter("action");
if(action.equalsIgnoreCase("Ajouter"))
{String nom= request.getParameter("nom");
String prenom= request.getParameter("prenom");
String numPor= request.getParameter("numPor");
String numFixe= request.getParameter("numFixe");
String adresse= request.getParameter("adresse");
metier.ajouterContact(new
Contact(nom,prenom,numPor,numFixe,adresse));
}
}
}