Introduction Principe Création et déploiement API des servlets Cookies / HttpSession
Technologie java web
Cours Java - F. Michel
8 / 35
Introduction Principe Création et déploiement API des servlets Cookies / HttpSession
Cycle de vie d’une application java web
Cycle de vie d’une application java web
1 Développer le code du composant web
2 Développer le web application deployment descriptor.
3 Compiler le composant web et les classes annexes qu’il utilise.
4 Optionellement, packager l’application dans une unité déployable.
5 Déployer l’application dans un conteneur web.
6 Tester l’URL qui référence l’application web.
Cours Java - F. Michel
9 / 35
Introduction Principe Création et déploiement API des servlets Cookies / HttpSession
Une Servlet hérite de
[Link]
HttpServlet est une classe abstraite définissant :
doGet : pour les HTTP GET requests
doPost, pour les HTTP POST requests
doPut, pour les HTTP PUT requests
doDelete, pour les HTTP DELETE requests
init et destroy : gestion du cycle de vie de la servlet (e.g. des
ressources)
getServletInfo, informations à propos de la Servlet
Cours Java - F. Michel
11 / 35
Introduction Principe Création et déploiement API des servlets Cookies / HttpSession
[Link]
i m p o r t j a v a . i o . IOException ;
import java . io . P r i n t W r i t e r ;
import javax . servlet . ServletException ;
import javax . servlet . http . HttpServlet ;
import javax . servlet . h t t p . HttpServletRequest ;
import javax . servlet . h t t p . HttpServletResponse ;
p u b l i c c l a s s H e l l o extends H t t p S e r v l e t {
p r o t e c t e d v o i d doGet ( H t t p S e r v l e t R e q u e s t request ,
HttpServletResponse response )
throws S e r v l e t E x c e p t i o n , IOException {
response . setContentType ( " t e x t / h t m l " ) ;
P r i n t W r i t e r o u t = response . g e t W r i t e r ( ) ;
o u t . p r i n t l n ( " <HTML><HEAD><TITLE> H e l l o C l i e n t ! < / TITLE> " +
" </HEAD><BODY> H e l l o C l i e n t ! < /BODY> </HTML> " ) ;
out . close ( ) ;
}
}
Cours Java - F. Michel
12 / 35
Introduction Principe Création et déploiement API des servlets Cookies / HttpSession
Hello dans le détail :
[Link] : import nécessaires
/ / E x c e p t i on IO
i m p o r t j a v a . i o . IOException ;
/ / pour l e f l u x d ’ e c r i t u r e
import java . io . P r i n t W r i t e r ;
/ / E x c e p t i on l i e e au c y c l e de v i e de l a S e r v l e t
import javax . s e r v l e t . ServletException ;
/ / classes o b l i g a t o i r e s
import javax . s e r v l e t . h t t p . HttpServlet ;
import javax . s e r v l e t . h t t p . HttpServletRequest ;
i m p o r t j a v a x . s e r v l e t . h t t p . HttpServletResponse ;
Cours Java - F. Michel
13 / 35