Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Correction Examen Jee 2019-2020

Télécharger au format pdf ou txt
Télécharger au format pdf ou txt
Vous êtes sur la page 1sur 5

Institut Supérieur d’Informatique et de Multimédia de Sfax ANNEE UNIVERSITAIRE : 2020-2021

RESPONSABLE DU COURS : NADA LOUATI

Module : JEE

Corrigé Examen 2019-2020


Partie I :
package examen.entity;

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();
}

public String getNom() {


return nom;
}

public void setNom(String nom) {


this.nom = nom;
}

public String getPrenom() {


return prenom;
}

public void setPrenom(String prenom) {


this.prenom = prenom;
}

public String getNumPortable() {


return numPortable;
}

public void setNumPortable(String numPortable) {


this.numPortable = numPortable;
}

public String getNumFixe() {


return numFixe;
}

public void setNumFixe(String numFixe) {


this.numFixe = numFixe;
}

public String getAdresse() {


return adresse;
}

public void setAdresse(String adresse) {


this.adresse = adresse;
}

public Long getIdContact() {


return idContact;
}

public Contact(String nom, String prenom, String numPortable,


String numFixe, String adresse) {
super();
this.nom = nom;
this.prenom = prenom;
this.numPortable = numPortable;
this.numFixe = numFixe;
this.adresse = adresse;
}

@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;

public class Test {

public static void main(String[] args) throws


InterruptedException {
// TODO Auto-generated method stub
try{
Properties prop = new Properties();
prop.put(Context.URL_PKG_PREFIXES,
"org.jboss.ejb.client.naming");
Context ctx =new InitialContext(prop);
GestionContact stub= (GestionContact)
ctx.lookup("ejb:/ExamenTMW/CONT!examen.session.GestionContact");
System.out.println(stub);
//ajout des Contacts
//stub.ajouterContact(new Contact("Ali","Ben
Salah","94987506","74250866","Sfax"));
//stub.ajouterContact(new Contact("Zied","Ben
Salah","98987506","73250866","Sousse"));
//stub.ajouterContact(new
Contact("Mohamed","Loukil","58287506","71360666","Tunis") );
//Recherche Contact par adresse
List<Contact> prods=stub.getContactParAdresse("Tunis");
for(Contact pr:prods)
{System.out.println(pr.getNom()+"\t"+pr.getPrenom());}
//consulter contact numero 1
Contact p =stub.getContact("Zied","Ben Salah");
System.out.println(p.getNumFixe()+"\t"+p.getNumPortable());
//supprimer contact numero 2
//stub.removeContact("aaa", "b");
//consulter produit apres suppression
List<Contact> prod=stub.getAllContacts();
for(Contact pr:prod)
{System.out.println(pr.getNom());}
stub.updateContact("Zied", "Ben Salah", "92586320", null,
null);
Thread.sleep(Integer.MAX_VALUE);
}
catch(NamingException e){
e.printStackTrace();
}
}
}

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));
}
}
}

Vous aimerez peut-être aussi