Développement Web
Transcription
Développement Web
Développement Web - Servlet Développement Web - Servlet Jean-Michel Richer [email protected] http://www.info.univ-angers.fr/pub/richer L3 Pro Informatique - 2010-2011 1 / 31 Développement Web - Servlet Plan Plan 1 Introduction 2 Le modèle MVC 3 Structure d’un site 4 Mise en application 5 Bibliographie 2 / 31 Développement Web - Servlet Introduction Introduction Introduction 3 / 31 Développement Web - Servlet Introduction Programmation Web avec Java Web + Java ◮ basée sur l’utilisation de Servlet ◮ généralement fondée sur une architecture de type MVC ◮ utilisation de JSP (Java Server Pages) ◮ utilisation de JSF (Java Server Faces - Framework web) ◮ utilisation de frameworks (Spring, Struts, Hibernate, ...) 4 / 31 Développement Web - Servlet Introduction Les Java Server Pages Qu’est ce que les JSP ◮ équivalent d’une page PHP mais avec code en Java ◮ utilisation de balises : <% %> pour insérer du code Java ◮ utilisation de balises : <%= %> pour évaluer une expression ◮ les JSP sont traduites en Servlet par le Serveur Tomcat (code compilé, non interprété) ◮ utilisation de variables globales (Session, Application) ou temporaires (Request, page) 5 / 31 Développement Web - Servlet Introduction Les Java Server Pages Attention ! ◮ la traduction JSP en Servlet n’est réalisée qu’une seule fois ◮ le premier chargement de la page est donc plus long 6 / 31 Développement Web - Servlet Introduction Balises JSP Balises ◮ <% %> : scripts ◮ <%= %> : expressions ◮ <%! %> : déclaration de variable ◮ <%@ %> : directives 7 / 31 Développement Web - Servlet Introduction Directives JSP directives ◮ <%@ page %> : définition de la page • • • • language contentType pageEncoding import ◮ <%@ include file=’’ %> : inclusion de fichier dans la page 8 / 31 Développement Web - Servlet Introduction Exemple de page JSP partie head 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.openbook.view.*" import="com.openbook.persistence.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>...</title> ... </head> 9 / 31 Développement Web - Servlet Introduction Exemple de page JSP partie body 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <body> <jsp:useBean id="user" class="com.project.model.User" scope="session" /> <jsp:useBean id="person" class="com.project.model.Person" scope="request" /> <% if (Page.allowed(out,user)==1) { %> <h1><% out.println("add new person"); %></h1> <% AuthorView.to html table(out,jsp author list,jsp author page); AuthorView.new and back panel(out); Page.page end(out); } %> </body> </html> 10 / 31 Développement Web - Servlet Introduction Les variables des pages JSP Variables prédéfinies ◮ request (HttpServletRequest) ◮ response (HttpServletResponse) ◮ out (PrintWriter) ◮ session (HttpSession) ◮ application (ServletContext) ◮ page (Object) 11 / 31 Développement Web - Servlet Introduction Actions standard Variables prédéfinies ◮ jsp:useBean création/importation d’un Bean ◮ jsp:setProperty initialisation d’un attribut d’un Bean ◮ jsp:getProperty afficher un attribut d’un Bean ◮ jsp:forward transfert de contrôle à une autre page 12 / 31 Développement Web - Servlet Introduction Le concept de Java Bean Définition Composant réutilisable manipulable visuellement Java Bean (simplifié) il s’agit d’une classe qui obéit aux règles suivantes : ◮ constructeur sans arguments ◮ méthodes get et set (getters, setters) 13 / 31 Développement Web - Servlet Introduction Exemple de Bean Bean 1 class Personne { 2 protected String nom; 3 protected String prenom; 4 5 public Personne() {} 6 public void setNom(String nom) { this.nom=nom; } 7 public void setPrenom(String nom) { this.prenom=prenom; } 8 public String getNom() { return this.nom; } 9 public String getPrenom() { return this.prenom; } 10 } 14 / 31 Développement Web - Servlet Introduction jsp:useBean Utilisation de jsp:useBean Il faut définir les attributs suivants : ◮ id : nom de l’instance ◮ class : type ◮ scope : portée (page, request, session, application) 15 / 31 Développement Web - Servlet Introduction Exemple d’utilisation de jsp :useBean jsp :useBean 1 2 3 4 5 <jsp:useBean id="MadMax" class="Personne" score="request" /> <% out.println("Hello "+MadMax.getPrenom()+" "+MadMax.getNom()); %> 16 / 31 Développement Web - Servlet Introduction ServletContext ServletCintext ◮ permet de communiquer avec le container de Servlets ◮ unique et accessible par tous les composants de l’application web ◮ paramètres définissable depuis le servlet ou depuis web.xml 17 / 31 Développement Web - Servlet Introduction ServletContext depuis web.xml <context-param> <param-name>webmasterEmail</param-name> <param-value>[email protected]</param-value> </context-param> 18 / 31 Développement Web - Servlet Introduction ServletContext depuis un servlet 1 2 3 4 5 6 7 8 9 10 ServletContext context = this.getServletContext(); out.println(context.getInitParameter("webmasterEmail"); Person person = (Person) context.getAttribute("sessionUser"); if (person == null) { person = new Person("default", "user"); context.setAttribute("sessionUser", person); } 19 / 31 Développement Web - Servlet Le modèle MVC Le modèle MVC Le modèle MVC 20 / 31 Développement Web - Servlet Le modèle MVC Le modèle M(PD)-V-C(E) Persistence Storage Logic Controller Execution Model Application Flow Process Flow Business Logic View Distributed Presentation Logic Spreading Logic 21 / 31 Développement Web - Servlet Le modèle MVC MVC en action 22 / 31 Développement Web - Servlet Structure d’un site Structure d’un site Structure d’un site 23 / 31 Développement Web - Servlet Structure d’un site Structure des répertoires d’un site Répertoire ◮ plusieurs structures possibles ! ◮ séparation source Java / affichage JSP ◮ gestion des versions ◮ manuel (ant) ou automatique (Eclipse) 24 / 31 Développement Web - Servlet Structure d’un site Exemple sous Eclipse Eclipse 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |-| | | | | | |-| | | | | | | ‘-- WebContent |-- META-INF | ‘-- MANIFEST.MF |-- WEB-INF | |-- lib | ‘-- web.xml ‘-- index.jsp build ‘-- classes ‘-- com ‘-- project |-- controller |-- model |-- persistence ‘-- view src ‘-- com ‘-- library |-- controller |-- model |-- persistence ‘-- view 25 / 31 Développement Web - Servlet Structure d’un site Autre exemple Sans Eclipse 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |-| | | | | | | | |-| | | | | | | | | | | | | | | |-|-| WebContent |-- META-INF | ‘-- MANIFEST.MF |-- WEB-INF | |-- lib | | |-- *.jar | |-- web.xml |-- *.jsp |-- img build |-- 3.0 | |-- META-INF | | ‘-- MANIFEST.MF | |-- WEB-INF | | |-- classes | | | ‘-- com | | | ‘-- project / MVCP | | |-- lib | | | |-- *.jar | | ‘-- web.xml | |-- img | ‘-- *.jsp ‘-- classes |-- com | ‘-- project / MVCP build.xml db ‘-- project.sql |-- dist | ‘-- 1.0 | ‘-- project.war ‘-- src |-- com | ‘-- project / MVCP 26 / 31 Développement Web - Servlet Mise en application Application Application 27 / 31 Développement Web - Servlet Mise en application Application Application • construction d’un site web en Java avec utilisation de l’IDE Eclipse • gestion d’une liste de personnes • projet Web Dynamique sous Eclipse 28 / 31 Développement Web - Servlet Mise en application Application Organisation index.jsp list lst_person.jsp add add_person.jsp validate mod mod_person.jsp validate del del_person.jsp 29 / 31 Développement Web - Servlet Bibliographie Bibliographie Bibliographie 30 / 31 Développement Web - Servlet Bibliographie Bibliographie • Agile Java Development with Spring, Hibernate and Eclipse, Anil Hemrajani, Developer’s Library, 2006, ISBN 9780672328961 • Java Power Tools, John Ferguson Smart, O’Reilly, 2008, ISBN 9780596527938 • Java Servlets and JSP, Joel Murach, Andrea Steelman, Murach, 2nd Edition, 2008, ISBN 9781890774448 31 / 31