Exercice 1: J2EE Produit - Java:: Libelle Nom Prix Qte Libelle Libelle Nom Nom Prix Prix Qte Qte
Exercice 1: J2EE Produit - Java:: Libelle Nom Prix Qte Libelle Libelle Nom Nom Prix Prix Qte Qte
Exercice 1: J2EE Produit - Java:: Libelle Nom Prix Qte Libelle Libelle Nom Nom Prix Prix Qte Qte
: J2EE
Produit.java :
package model;
Operation.java :
package model;
import java.util.ArrayList;
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"));
//Ajout d'objet
op.ajouter(p);
op.setListe(op.getListe());
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>
<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();
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>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Gestion des Produits;
<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 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 ArticleDAO() {
super();
// TODO Auto-generated constructor stub
emf= Persistence.createEntityManagerFactory("articleJPA");
}
public EntityManager GetEntityManager() {
return emf.createEntityManager();
}
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"));
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;
<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();
%>
<tr>
<td><%= a.getNom() %></td>
<td><%= a.getDesignation() %></td>
<td><%= a.getPrix() %></td>
<td><%= a.getQte() %></td>
</tr>
<%} %>
</table>
</body>
</html>