You are on page 1of 9

Exercice 1 

: J2EE
Produit.java :
package model;

public class Produit {


private String libelle, nom;
private float prix;
private int qte;
public String getLibelle() {
return libelle;
}
public void setLibelle(String libelle) {
this.libelle = libelle;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public float getPrix() {
return prix;
}
public void setPrix(float prix) {
this.prix = prix;
}
public int getQte() {
return qte;
}
public void setQte(int qte) {
this.qte = qte;
}
public Produit(String libelle, String nom, float prix, int qte) {
super();
this.libelle = libelle;
this.nom = nom;
this.prix = prix;
this.qte = qte;
}
}

Operation.java :
package model;

import java.util.ArrayList;

public class Operation {


private ArrayList<Produit> liste= new ArrayList<>();

public ArrayList<Produit> getListe() {


return liste;
}

public void setListe(ArrayList<Produit> liste) {


this.liste = liste;
}

public void ajouter(Produit p) {


liste.add(p);
}

Servlet.java :
package controller;

import java.io.IOException;
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 model.Operation;
import model.Produit;

/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
Operation op;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
super();
// TODO Auto-generated constructor stub
}

@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
op= new Operation();
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String libelle = request.getParameter("libelle");
String nom = request.getParameter("nom");
float prix = Float.parseFloat(request.getParameter("prix"));
int qte = Integer.parseInt(request.getParameter("qte"));

//creation/instanciation des objets


Produit p = new Produit(libelle, nom, prix, qte);

//Ajout d'objet
op.ajouter(p);
op.setListe(op.getListe());

//envoi des objets


request.setAttribute("result", op);
request.getRequestDispatcher("Produit.jsp").forward(request,
response);
}

Produit.jsp :
<%@page import="model.Produit"%>
<%@page import="java.util.Iterator"%>
<%@page import="model.Operation"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Gestion des Produits;

<form action="Servlet" method="post">


Libelle:
<input type="text" name="libelle"></br>
Nom:
<input type="text" name="nom"></br>
Prix:
<input type="text" name="prix"></br>
Quantité:
<input type="text" name="qte"></br>
<input type="submit" value="valider">
</form>
<table border= "1" width= "30%">

<tr>
<th>Nom</th>
<th>Description</th>
<th>Prix</th>
<th>Quantite</th>
</tr>
<%
Operation produits;
if(request.getAttribute("result")!= null){
produits=(Operation) request.getAttribute("result");
}
else
produits=new Operation();

Iterator<Produit> list1 = produits.getListe().iterator();


while(list1.hasNext()){
//Produit p = list1.next();
Produit p =list1.next();

int i=0 ;
System.out.println(produits.getListe().size());
%>
<tr>
<td><%= p.getLibelle() %></td>
<td><%= p.getNom() %></td>
<td><%= p.getPrix() %></td>
<td><%= p.getQte() %></td>
</tr>

<%} %>

</table>
</body>
</html>

Produit.jsp avec JSTL:


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Gestion des Produits;

<form action="Servlet" method="post">


Libelle:
<input type="text" name="libelle"></br>
Nom:
<input type="text" name="nom"></br>
Prix:
<input type="text" name="prix"></br>
Quantité:
<input type="text" name="qte"></br>
<input type="submit" value="valider">
</form>
<table border= "1" >

<tr>
<th>Nom</th>
<th>Description</th>
<th>Prix</th>
<th>Quantite</th>
</tr>
<c:forEach items="${result.liste}" var="rep1">
<tr>

<td><c:out value="${rep1.getNom()}"></c:out></td>
<td><c:out value="${rep1.getDesc()}"></c:out></td>
<td><c:out value="${rep1.getPrix()}"></c:out></td>
<td><c:out value="${rep1.getQte()}"></c:out></td>
</tr>
</c:forEach>

</table>

</body>
</html>

Exercice 2 :JPA
package modele;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Article implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
int id ;

String nom,desig;

double prix;

int qte;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

public String getNom() {


return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getDesignation() {
return desig;
}
public void setDesignation(String desig) {
this.desig = desig;
}
public double getPrix() {
return prix;
}
public void setPrix(double prix) {
this.prix = prix;
}
public int getQte() {
return qte;
}
public void setQte(int qte) {
this.qte = qte;
}

public Article() {
super();
// TODO Auto-generated constructor stub
}
public Article(String nom, String desig, double prix, int qte) {
super();
this.nom = nom;
this.desig = desig;
this.prix = prix;
this.qte = qte;
}

ArticleDAO.java :
package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import modele.Article;

public class ArticleDAO {

private EntityManagerFactory emf;

public ArticleDAO() {
super();
// TODO Auto-generated constructor stub
emf= Persistence.createEntityManagerFactory("articleJPA");
}
public EntityManager GetEntityManager() {
return emf.createEntityManager();
}

public Article AjoutArticle(Article art) {


EntityManager em= GetEntityManager();
em.getTransaction().begin();
em.persist(art);
em.getTransaction().commit();
em.close();
return art;
}

public List<Article> Affichearticle(){


return GetEntityManager().createQuery("SELECT A FROM article
A").getResultList();
}

public void Delete(int id) {


EntityManager em = GetEntityManager();
em .getTransaction().begin();
Article a = em.find(Article.class, id);
em.remove(a);
em.getTransaction().commit();
em.close();
}

Servlet.java :
package web;

import java.io.IOException;
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 dao.ArticleDAO;
import modele.Article;

/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name= request.getParameter("name");
String description = request.getParameter("description");
double prix = Double.parseDouble(request.getParameter("prix"));
int qte = Integer.parseInt(request.getParameter("qte"));

Article art= new Article(name, description, prix, qte);


ArticleDAO artdao= new ArticleDAO();

artdao.AjoutArticle(art);

request.setAttribute("model", artdao.Affichearticle());
request.getRequestDispatcher("Article.jsp").forward(request,
response);
}

Article.jsp :

<%@page import="modele.Article"%>
<%@page import="java.util.Iterator"%>
<%@page import="dao.ArticleDAO"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Gestion des Articles;

<form action="Servlet" method="post">


Libelle:
<input type="text" name="name"></br>
Description:
<input type="text" name="description"></br>
Prix:
<input type="text" name="prix"></br>
Quantité:
<input type="text" name="qte"></br>
<input type="submit" value="valider">
</form>
<table border= "1" width= "30%">

<tr>
<th>Nom</th>
<th>Description</th>
<th>Prix</th>
<th>Quantite</th>
</tr>
<%
ArticleDAO articles;
if(request.getAttribute("model")!= null){
articles=(ArticleDAO) request.getAttribute("model");
}
else
articles=new ArticleDAO();

Iterator<Article> list1 = articles.Affichearticle().iterator();


while(list1.hasNext()){
//Produit p = list1.next();
Article a =list1.next();

%>
<tr>
<td><%= a.getNom() %></td>
<td><%= a.getDesignation() %></td>
<td><%= a.getPrix() %></td>
<td><%= a.getQte() %></td>
</tr>

<%} %>

</table>

</body>
</html>

You might also like