Vous êtes sur la page 1sur 5

U NIVERSIT É DE J ENDOUBA

I NSTITUT S UP ÉRIEUR DE L’I NFORMATIQUE DU K EF

TP 4 : JSP ET S ERVLET

Matière : Technologie JEE Niveau : 2eme LSI


Responsable : Mahran Farhat A U : 2022 / 2023

Cette manip est développée en utilisant Intellij Idea, mais vous pouvez la produire en utilisant tout
IDE qui supporte Maven.
Pour le développement de cette manip, nous supposons que vous avez réussit le TP 1 que vous
avez crée, configuré et testé votre projet. Le but de ce support est de vous permettre de prendre en
main la création des solution web en utilisant les servlets et JSP. L’objectif de cette séance de TP est
de créer un module de consultation des livres. Un livre est caractérisé par un identifiant (ISBN), un
titre, un auteur, un éditeur et un prix unitaire.
Nous utilisons pour cela le modèle de données suivant :

Livre (ISBN , titre, auteur, éditeur, prix)

Étape 1 : créer la classe Livre permettant de représenter le modèle livre à traiter dans le
cadre de ce module.
1. Créer un package nommé models (click droit sur le package org.example > new > package )
2. Créer une classe nommé Livre dans le package models. Le code de la classe Livre doit être le
suivant :

package org.example.models;

import java.io.Serializable;

public class Livre implements Serializable {

public String isbn;


public String titre;
public String auteur;
public String editeur;
public double prix;

public Livre() {
}

public Livre(String isbn,String titre, String auteur, String


editeur, double prix) {
this.isbn = isbn;
this.titre = titre;
this.auteur = auteur;
this.editeur = editeur;
this.prix = prix;
}

1
public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {


this.isbn = isbn;
}

public String getTitre() {


return titre;
}

public void setTitre(String titre) {


this.titre = titre;
}

public String getAuteur() {


return auteur;
}

public void setAuteur(String auteur) {


this.auteur = auteur;
}

public String getEditeur() {


return editeur;
}

public void setEditeur(String editeur) {


this.editeur = editeur;
}

public double getPrix() {


return prix;
}

public void setPrix(double prix) {


this.prix = prix;
}

@Override
public String toString() {
return "Livre{" +
"isbn=’" + isbn + ’\’’ +
", titre=’" + titre + ’\’’ +
", auteur=’" + auteur + ’\’’ +
", editeur=’" + editeur + ’\’’ +
", prix=" + prix +
’}’;
}
}

2
Étape 2 : créer la servlet ManageBooks.java permettant de retourner la liste des livres à
afficher dans la page JSP.
1. Créer un package nommé controller (click droit sur le package org.example > new > pa-
ckage )
2. Créer une classe nommé ManageBooks.java dans le package controller. Le code de la classe
ManageBooks.java doit être le suivant :

package org.example.controller;

import org.example.models.Livre;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, IOException {
List<Livre> bookList = new ArrayList<>();
bookList.add(new Livre("LC01","Java", "John Doe", "Thomas
Edition", 100));
bookList.add(new Livre("LC02","C++", "John Doe", "Thomas
Edition", 100));
bookList.add(new Livre("LC03","Python", "John Doe", "Thomas
Edition", 100));
bookList.add(new Livre("LC04","C#", "John Doe", "Thomas
Edition", 100));
req.setAttribute("bookList", bookList);
req.getRequestDispatcher("manageBooks.jsp").forward(req,
resp);
}
}

Étape 3 : créer la page JSP manageBooks.jsp permettant de visualiser la liste des livres.
(a) Créer un package nommé webapp (click droit sur le dossier main > new > package )
(b) Créer une page JSP nommé manageBooks.jsp dans le dossier webapp. Le code par défaut
d’une page JSP est le suivant :

<%@page language="java" contentType="text/html"


pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>

3
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>First JSP</title>
</head>

<body>
</body>
</html>

(c) Modifier le code de la page manageBooks.jsp. Le code de la page doit être le suivant :

<%@page language="java" contentType="text/html"


pageEncoding="UTF-8" %>
<%@page import="org.example.models.Livre"%>
<%@page import="java.util.List"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>First JSP</title>
</head>
<h1 align="center"> Liste des livres </h1>
<br>
<table width="50%" align="center">
<tr>
<td> ISBN </td>
<td> Titre </td>
<td> Auteur </td>
<td> Editeur </td>
<td> Prix </td>
</tr>
<%
List<Livre> livres = (List<Livre>)
request.getAttribute("bookList");
for (Livre livre : livres) {
%>
<tr>
<td> <%= livre.getIsbn() %> </td>
<td> <%= livre.getTitre() %> </td>
<td> <%= livre.getAuteur() %> </td>
<td> <%= livre.getEditeur() %> </td>
<td> <%= livre.getPrix() %> </td>
</tr>
<%
}
%>

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

4
3. Créer et modifier le code de la page index.html. Le code cette page doit être le suivant :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>

<div align="center">
<h1>JAVA Entreprise Edition + Maven</h1>
<h2>Servlets + Java Server Page (JSP)</h2>
<h2>Formateur : Mahran Farhat (farhatmahran@gmail.com) </h2>
</div>

<br>
<a href="./manageBooks">Liste des livres</a>
</body>
</html>

4. Exécuter et compiler votre projet


ouvrir un terminal et taper la commande : mvn clean install
mvn tomcat7 :run
Consulter http ://localhost :8082/ManageBooks

Bonne Chance

Vous aimerez peut-être aussi