applications et services web: introduction à javaee · 2016-09-13 · introduction javaee java...

49
Introduction Applications VS Services JavaEE Applications et Services WEB: Introduction à JavaEE Erick Stattner Maître de conférences en Informatique Université des Antilles France [email protected] Erick Stattner Applications et Services WEB: Introduction à JavaEE 1 / 49

Upload: others

Post on 13-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Applications et Services WEB:Introduction à JavaEE

Erick Stattner

Maître de conférences en InformatiqueUniversité des Antilles

[email protected]

Erick Stattner Applications et Services WEB: Introduction à JavaEE 1 / 49

Page 2: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Description du cours

Objectif:

Se familiariser avec la notion d’applications et de services WEB

Être capables:I Savoir ce qu’est un service WEBI Développer et déployer un serviceI Concevoir une application qui exploite plusieurs services

Erick Stattner Applications et Services WEB: Introduction à JavaEE 2 / 49

Page 3: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Description du cours

Organisation de l’enseignement:CI: 15h

I JavaEEI Service REST

TD/TP: 15H

Modalités d’évaluation de l’UE:

Contrôle continu

Projet global

Erick Stattner Applications et Services WEB: Introduction à JavaEE 3 / 49

Page 4: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

Outline

1 IntroductionLe web en chiffreContexteSolution

2 Applications VS Services

3 JavaEE

Erick Stattner Applications et Services WEB: Introduction à JavaEE 4 / 49

Page 5: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

IntroductionLe web en chiffre

Figure: source: internetlivestats , internetworldstats

Erick Stattner Applications et Services WEB: Introduction à JavaEE 5 / 49

Page 6: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

IntroductionLe web en chiffre

Vers une escalade des périphériques connectésOrdinateur/ServeurTéléphone/TabletteMontreLunettesTéléVoitureMaisonElectro-ménager...

Erick Stattner Applications et Services WEB: Introduction à JavaEE 6 / 49

Page 7: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

IntroductionLe web en chiffre

Vers une escalade des périphériques connectés

Figure: Source: Le Figaro 11/04/2013

Erick Stattner Applications et Services WEB: Introduction à JavaEE 7 / 49

Page 8: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

IntroductionContexte

ContexteL’informatique d’entreprise est de plus en plus répartie

I ex. classique: Agence de voyage (réserver billet, hôtel, voiture).

Hétérogénéité des périphériques oblige a proposer des solutionsadaptatives ("Responsive design").

I ex. Consultation de son compte (pc, tel, tablette)

Décentraliser les calculsI ex. problème consommation (tablette, tel, montre, lunettes, etc.)

Différents besoinsI ex. Affichage, statistiques, simulation, ...

Erick Stattner Applications et Services WEB: Introduction à JavaEE 8 / 49

Page 9: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

IntroductionContexte

Contexte

Erick Stattner Applications et Services WEB: Introduction à JavaEE 9 / 49

Page 10: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Le web en chiffreContexteSolution

IntroductionSolution

Solution idéale:Architecture adaptative

I ex. Appli bancaire accessible de différents clients

Système de communication ouvertI ex. Information lisible et interprétable par tous les clients

Client va traiter et afficher les données selon ses besoins

Solution

Services WEB !

Erick Stattner Applications et Services WEB: Introduction à JavaEE 10 / 49

Page 11: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Outline

1 Introduction

2 Applications VS ServicesApplication VS Service WEBDéfinitionsExemples de service WEB

3 JavaEE

Erick Stattner Applications et Services WEB: Introduction à JavaEE 11 / 49

Page 12: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesApplication VS Service WEB

ApplicationVS

Service WEB ?

Erick Stattner Applications et Services WEB: Introduction à JavaEE 12 / 49

Page 13: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesApplication VS Service WEB

Application

Exploite un ou plusieurs services

Offre plusieurs fonctionnalités

Propose généralement un rendu graphique

Service

Limité à une opération élémentaire

Fournit des données brutes ou formatées

Généralement pas de rendu graphique

Utilisés dans plusieurs applications

Peut faire appel à d’autres services

Erick Stattner Applications et Services WEB: Introduction à JavaEE 13 / 49

Page 14: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesApplication VS Service WEB

Application VS Service WEB

Erick Stattner Applications et Services WEB: Introduction à JavaEE 14 / 49

Page 15: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Définitions

Selon le Wikipedia:Une application Web (aussi appelée WebApp) est une applicationmanipulable grâce à un navigateur Web.Elle est généralement placée sur un serveur et se manipule à traversune Interface en actionnant des widgets à l’aide d’un navigateur Web,via un réseau informatique (Internet, intranet, réseau local, etc.).

Exemples ?

Erick Stattner Applications et Services WEB: Introduction à JavaEE 15 / 49

Page 16: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Exemple Application WEB

Erick Stattner Applications et Services WEB: Introduction à JavaEE 16 / 49

Page 17: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Exemple Application WEB

Erick Stattner Applications et Services WEB: Introduction à JavaEE 17 / 49

Page 18: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Exemple Application WEB

Erick Stattner Applications et Services WEB: Introduction à JavaEE 18 / 49

Page 19: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Définitions

Selon le Wikipedia:Un service web est un programme informatique de la famille destechnologies web permettant la communication et l’échange dedonnées entre applications et systèmes hétérogènes dans desenvironnements distribués.Il s’agit donc d’un ensemble de fonctionnalités exposées sur internetou sur un intranet, par et pour des applications ou machines, sansintervention humaine, de manière synchrone ou asynchrone.

Erick Stattner Applications et Services WEB: Introduction à JavaEE 19 / 49

Page 20: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Définitions

Selon le W3C (World Wide Web Consortium)A web service is a system designed to support machine-to-machineinteraction over a network.It has an interface that can be discovered dynamically and which isdescribed in a machine-processable format.

Erick Stattner Applications et Services WEB: Introduction à JavaEE 20 / 49

Page 21: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Définitions

Selon le Dico du netUn service web est une technologie permettant à des applications dedialoguer à distance via Internet, indépendamment desplates-formes et des langages sur lesquelles elles reposent.

Erick Stattner Applications et Services WEB: Introduction à JavaEE 21 / 49

Page 22: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Définitions

Pus généralement:Un service Web est un programme sollicité via Internet par différentstype de clients, permettant l’échange de données afin quel’application appelante puisse intégrer le résultat de cet échange à sespropres analyses.Les requêtes et les réponses s’effectuent dans des formats ouverts(HTML, XML, JSON ou text) et transitent par Internet.

Exemples ?

Erick Stattner Applications et Services WEB: Introduction à JavaEE 22 / 49

Page 23: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Principales caractéristiques:

Fonctionnalité utilisable via Internet

Inter-opérables: Interface publique décrite d’une manière interprétablepar tous

Systèmes faiblement couplés, client ne connaît pas forcément lefournisseur

Le transport des données repose sur des "protocoles du WEB":HTTP, FTP, SMTP, ...

Standard ouvert: Échange de données s’effectue dans un formatstandardXML, JSON, HTML, Text, ...

Le client est chargé d’analyser, traiter et/ou afficher les donnéesreçues

Indépendante des plates-formes et des langages

Erick Stattner Applications et Services WEB: Introduction à JavaEE 23 / 49

Page 24: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Principe de base

Erick Stattner Applications et Services WEB: Introduction à JavaEE 24 / 49

Page 25: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Client1 Prend connaissance des interfaces publiques2 Construit la requête et la normalise en respectant les interfaces

(XML, JSON, Text)3 Envoie la requête (protocole du WEB, ex. HTTP)4 Reçoit les données, les interprète (XML)5 Traite localement (affichage, calculs, etc.)

Erick Stattner Applications et Services WEB: Introduction à JavaEE 25 / 49

Page 26: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesDéfinitions

Serveur1 Définit ses interfaces (XML, WSDL, etc.)2 Reçoit les requêtes3 Les traduit et effectue le bon traitement4 Normalise la réponse et envoie le résultat au client

(HTML, XML, JSON)

Erick Stattner Applications et Services WEB: Introduction à JavaEE 26 / 49

Page 27: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesExemples de service WEB

Exemple 1: Actualités

Erick Stattner Applications et Services WEB: Introduction à JavaEE 27 / 49

Page 28: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesExemples de service WEB

Exemple 1: Actualités

Erick Stattner Applications et Services WEB: Introduction à JavaEE 28 / 49

Page 29: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesExemples de service WEB

Exemple 2: Analyse des données

Erick Stattner Applications et Services WEB: Introduction à JavaEE 29 / 49

Page 30: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesExemples de service WEB

Exemple 2: Analyse des données

Erick Stattner Applications et Services WEB: Introduction à JavaEE 30 / 49

Page 31: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesExemples de service WEB

Autres exemples:

Réduction d’URL (goo.gl, TinyURL, bit.ly, etc.)

http://www.erickstattner.com/?page_id=21⇓

goo.gl/sKV0Hf

Analyse d’adresses mail

es@es → INCORRECT

Association de codes postaux

97190 → Le Gosier

Erick Stattner Applications et Services WEB: Introduction à JavaEE 31 / 49

Page 32: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

Application VS Service WEBDéfinitionsExemples de service WEB

Applications VS ServicesExemples de service WEB

De nombreux services proposés par Googlehttps://developers.google.com/apis-explorer/#p/

Analyse de données

Traduction

Réduction d’URL

Statistique fréquentation site web

etc.

Erick Stattner Applications et Services WEB: Introduction à JavaEE 32 / 49

Page 33: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

Outline

1 Introduction

2 Applications VS Services

3 JavaEEIntroductionFonctionnement JavaEE

Erick Stattner Applications et Services WEB: Introduction à JavaEE 33 / 49

Page 34: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEIntroduction

JavaEE

Java Enterprise Edition, ou JavaEE (anciennement J2EE)

Version de JAVA pour les entreprises

Plus particulièrement à destination des appli et services WEB

Première spécification proposée en 1999

Basée sur la notion de ServletNécessite un serveur d’applications ("serveur Java")

I Apache Tomcat (Apache)I GlassFish Server (Oracle)I Google App Engine (Google)I JBoss App. Server (Red Hat)I ...

Erick Stattner Applications et Services WEB: Introduction à JavaEE 34 / 49

Page 35: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEIntroduction

ServletComposant logiciel écrit en Java fonctionnant coté serveur

I Assimilable a: PHP, ASP, etc.

Reçoit et traite les requêtes HTTP

Fournit au client une réponse HTTP

Une Servlet s’exécute dans un moteur (ou conteneur) de ServletsI Établit le lien entre la Servlet et le serveur WebI Associe à des URL virtuelles une Servlet

Tout comme un programme JAVA, la Servlet s’exécute parl’intermédiaire d’une machine virtuelle

I Nécessite que java soit installé sur le serveurI Pas nécessaire sur le client

Erick Stattner Applications et Services WEB: Introduction à JavaEE 35 / 49

Page 36: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEIntroduction

Principe

Erick Stattner Applications et Services WEB: Introduction à JavaEE 36 / 49

Page 37: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEIntroduction

IntérêtPuissance du JAVA

I Disponibilités de l’API Java et des toutes les API liéesI Traitement d’images, de sons, connexion aux bases de données,

chiffrement, graphisme, etc.I Gestion des erreurs par exceptionI Typage fort de JAVAI Technologie portable

Ajout de JavaEEI Une servlet est chargée une seule foisI Servlet peut conserver son état

Erick Stattner Applications et Services WEB: Introduction à JavaEE 37 / 49

Page 38: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEIntroduction

Structure d’une application JavaEE

Erick Stattner Applications et Services WEB: Introduction à JavaEE 38 / 49

Page 39: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveurMise en place d’une servlet nécessite deux étapes:

1 Routage des requêtes à l’aide du fichier web.xml2 Traitement des informations par la Servlet

Erick Stattner Applications et Services WEB: Introduction à JavaEE 39 / 49

Page 40: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 1) web.xml

OBLIGATOIREMENT situé a la racine du dossier WEBINF

ATTENTION Un seul fichier par application/projet

Regroupe l’ensemble des informations de fonctionnement del’application

Permet d’associer une chemin (URL) à une servlet

La servlet ainsi spécifiée sera chargée du traitement de la requête

Erick Stattner Applications et Services WEB: Introduction à JavaEE 40 / 49

Page 41: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 1. web.xml

Squelette classique fichier web.xml

Erick Stattner Applications et Services WEB: Introduction à JavaEE 41 / 49

Page 42: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 1. web.xmlAssociation de plusieurs cheminsDeux pointeurs vers une même servlet

Erick Stattner Applications et Services WEB: Introduction à JavaEE 42 / 49

Page 43: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 1. web.xml

Utilisation de meta-caractères

Erick Stattner Applications et Services WEB: Introduction à JavaEE 43 / 49

Page 44: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 2. Servlet

Une Servlet doit hériter de HttpServlet

Reçoit et traite les requêtesI Lit la méthode HTTP dans le paquetI Transmet à la méthode appropriée

Squelette classique:

Erick Stattner Applications et Services WEB: Introduction à JavaEE 44 / 49

Page 45: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 2. Servlet

HttpServlet fournit plusieurs méthodes de traitement des requetesNom construit sur la meme base doXXX(...)

I doGet(...): pour les requêtes de type GETI doPost(...): pour les requêtes de type POSTI doPut(...): pour les requêtes de type PUTI doDelete(...): pour les requêtes de type DELETE

Si non redéfinies, les méthodes doXXX(...) renvoie une erreur de typeHTTP 405

Erick Stattner Applications et Services WEB: Introduction à JavaEE 45 / 49

Page 46: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 2. ServletQuelle que soit la méthode, on dispose toujours de deux types d’objetspour chaque requête

I Un objet de requête: contexte de la requête (entete, parametre, url,navig, etc.) + info sur le client (navigateur, IP, etc.)

I Un objet de réponse: qui permet de renvoyer des données au client (typede contenu, code de retour, données, etc.)

Erick Stattner Applications et Services WEB: Introduction à JavaEE 46 / 49

Page 47: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 2. ServletMéthodes utiles sur l’objet requête: (http://goo.gl/eiGhgK)

I getMethod()I getParameter(String name)I getRequestURI()I getRequestURL()I getQueryString()I getParameterNames()I ...

Méthodes utiles sur l’objet reponse: (http://goo.gl/LRIIlh)I getWriter()I setStatus()I setContentType()I sendRedirect()I ...

Erick Stattner Applications et Services WEB: Introduction à JavaEE 47 / 49

Page 48: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 2. Servlet et cycle de vie

Exemple de servlet traitant les informations reçues

Erick Stattner Applications et Services WEB: Introduction à JavaEE 48 / 49

Page 49: Applications et Services WEB: Introduction à JavaEE · 2016-09-13 · Introduction JavaEE Java Enterprise Edition, ou JavaEE (anciennement J2EE) Version de JAVA pour les entreprises

IntroductionApplications VS Services

JavaEE

IntroductionFonctionnement JavaEE

JavaEEFonctionnement JavaEE

Coté serveur: 2. Servlet et cycle de vieLa servlet n’est créée qu’une fois, i.e. une seule instanceA chaque fois que le serveur est relancé, le conteneur de servlet estréinitialisé et il y a création d’une nouvelle instanceCette instance traite toutes les requetesAvantages

I Rapidité: pas cout lié à la création d’un nouvel objet à chaque requeteI Possibilité de conserver des données entre les requetes

Exemple

Erick Stattner Applications et Services WEB: Introduction à JavaEE 49 / 49