14
Stéphane Frenot - Département Télécommunication - S ID - [email protected] II - Jsp 1 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected] II - Jsp 266 Les servlets

Embed Size (px)

Citation preview

Page 1: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 1

Les servlets

Page 2: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 2

Qu’est ce qu'une servlet

• Une servlet est un petit programme Java utilisé pour étendre les fonctionnalités d'un serveur Web

• C'est :– Une application côté serveur– Utilisée pour générer du contenu dynamique– Chargée dynamiquement quand elle est

demandée

Page 3: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 3

Model d’accès des servlets

client ServeurWeb

Servlet

Servlet

Page 4: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 4

Avantages / Inconvénients

• Avantages– Indépendance issue de la plateforme java– Modèle de sécurité issu du serveur Web– Support dans la plupart des serveurs Web– Exploite toute l'API Java (+ protocoles)

• Problèmes– Apprendre java– Certains serveurs Web supportent mal la charge

Page 5: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 5

L'interface Servlet

public interface Servlet {

public void destroy();

public ServletConfig getServletConfig();

public String getServletInfo();

public void init(ServletConfig) throws ServletException;

public void service(ServletRequest, ServletResponse)

throws ServletException, IOException;

}

javax.servlet.GenericServlet

javax.servlet.http.HttpServlet

Page 6: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 6

Cycle de vie de la Servlet

Classe de la servlet

Instanciationet

Chargement

?

Initialisation?

?

ActiveRequêtes client

Destruction

?

Garbage Collection

?

Page 7: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 7

La servlet de base !

import javax.servlet.*;

import javax.servlet.http.*;

public class MaServlet extends HttpServlet {

public void service (HttpServletRequest req,

HttpServletResponse res) throws

ServletException, IOException{

res.setContentType("text/html");

ServletOutputStream out=res.getOutputStream();

out.println("<html>");

out.println("<head><title>Salut le monde</title></head>");

out.println("<body><h1>Bonjour le monde</h1></body>");

out.println("<html>");

}

}

Page 8: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 8

SA est un serveur Web

• Gestion des sessions

• Accès aux BD par JDBC

• Hébergement multi IP

• Configuration et Gestion

• Accès sécurisé au services

• Logging et Rapports

Page 9: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 9

JSP : Java Server Pages

• Technologie définie pour aider à l'écriture de pages Web

• Elle– génère une page vers le client– est portable (Write Once, Run EveryWhere)– mets en avant l'approche par composants– permet la mise en œuvre facile des sites dynamiques

• Equivalents : ASP, PHP, PSP

Page 10: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 10

Les JSP

• Séparent la présentation du contenu

• Une page JSP contient– moules (squelettes) contenant le texte fixe– action contenues dans des directives et des scriplets

Client

ServletRequête

Page JSP

Réponse

Page 11: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 11

La JSP de base !

<html>

<H1>Information sur la requête</H1>

Requête <%= request.getMethod() %>

<BR>

URI Demandée : <%= request.getRequestURI() %>

<BR>

Protocol demandé : <%= request.getProtocol() %>

<BR>

</html>

Page 12: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 12

Les tags Jsp

• Expression<%= new Date().toString() %>

• Scriplet<%for (int i=0; i<10 ; i++> { %>

• Declarations<%! String retourneCinq() { return ("Cinq");}

• Action<jsp:useBean…>

<jsp:include…>

Page 13: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 13

L'utilisation d'un objet

package essai;

public class MaClasse {

String val=15;

public String getVal(){return this.val;}

public void setVal(String val){this.val=val;}

}

<html><body><h2>Un test d'objet</h2>

<jsp:usebean id="uneclasse" class="essai.MaClasse" scope="session" />

<% uneClasse.setValue("toto"); %>

<blink>Mon objet a pour valeur <%= uneclasse.getVal() %>

</body></html>

Page 14: Stéphane Frenot - Département Télécommunication - SID - stephane.frenot@insa-lyon.fr II - Jsp 266 Les servlets

Stéphane Frenot - Département Télécommunication - SID - [email protected]

II - Jsp 14

L'utilisation d'un EJB

<html><head><title> <%= pagetitle %> </title></head>

<h2><font color=#DB1260><%= pagetitle %></font></h2>

<%@ page import="edt.matiere.*"%>

<%!String pagetitle = "JSP : Fibonnacci";%>

<%try {

ctx = getInitialContext();

homeFib = (FibonnacciHome) ctx.lookup("fibonnaci");

uneSuite=homeFib.create();

out.println("fib(7)="+uneSuite.getFibonacciNumber(7));

}catch(Exception e){e.printStackTrace();

}

%>

</body></html>