Commit 84125073ec514a1e2ab645c9f98fddb0de726057
1 parent
56d4d606
projet illustrant les concepts JEE du cours AL
Showing
13 changed files
with
223 additions
and
9 deletions
Show diff stats
demoCoursAL/.classpath
... | ... | @@ -4,5 +4,7 @@ |
4 | 4 | <classpathentry kind="lib" path="/home/opt/wildfly-10.1.0.Final/modules/system/layers/base/javax/persistence/api/main/hibernate-jpa-2.1-api-1.0.0.Final.jar"/> |
5 | 5 | <classpathentry kind="lib" path="/home/opt/wildfly-10.1.0.Final/modules/system/layers/base/javax/ejb/api/main/jboss-ejb-api_3.2_spec-1.0.0.Final.jar"/> |
6 | 6 | <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> |
7 | + <classpathentry kind="lib" path="/home/opt/wildfly-10.1.0.Final/modules/system/layers/base/javax/servlet/api/main/jboss-servlet-api_3.1_spec-1.0.0.Final.jar"/> | |
8 | + <classpathentry kind="lib" path="/home/opt/wildfly-10.1.0.Final/modules/system/layers/base/javax/servlet/jstl/api/main/jboss-jstl-api_1.2_spec-1.1.3.Final.jar"/> | |
7 | 9 | <classpathentry kind="output" path="bin"/> |
8 | 10 | </classpath> | ... | ... |
demoCoursAL/build.xml
... | ... | @@ -23,6 +23,9 @@ |
23 | 23 | <fileset dir="${jboss.dir}/modules/system/layers/base/javax/ejb/api/main"> |
24 | 24 | <include name="jboss-ejb-api_3.2_spec-1.0.0.Final.jar"/> |
25 | 25 | </fileset> |
26 | + <fileset dir="${jboss.dir}/modules/system/layers/base/javax/servlet/api/main"> | |
27 | + <include name="**/*.jar"/> | |
28 | + </fileset> | |
26 | 29 | <pathelement location="${build.dir}/sessions"/> |
27 | 30 | <pathelement location="${build.dir}/entites" /> |
28 | 31 | <pathelement location="${build.dir}/classes"/> |
... | ... | @@ -37,6 +40,7 @@ |
37 | 40 | <mkdir dir="${build.dir}/jars" /> |
38 | 41 | <mkdir dir="${build.dir}/web" /> |
39 | 42 | <mkdir dir="${build.dir}/web/WEB-INF" /> |
43 | + <mkdir dir="${build.dir}/web/WEB-INF/classes" /> | |
40 | 44 | </target> |
41 | 45 | |
42 | 46 | <!-- Compile sessions--> |
... | ... | @@ -72,17 +76,23 @@ |
72 | 76 | </jar> |
73 | 77 | </target> |
74 | 78 | |
75 | - <target name="war" depends="prepare"> | |
76 | - <!-- <xmlvalidate file="${src.dir}/web/web.xml"/> --> | |
77 | - <copy file="${src.dir}/web/web.xml" todir="${build.dir}/web/WEB-INF" /> | |
79 | + <target name="compile-controleurs" depends="prepare"> | |
80 | + <javac destdir="${build.dir}/web/WEB-INF/classes" | |
81 | + classpathref="build.classpath" > | |
82 | + <src path="${src.dir}/web/controleurs" /> | |
83 | + </javac> | |
84 | + </target> | |
85 | + | |
86 | + <target name="war" depends="compile-controleurs"> | |
78 | 87 | <copy todir="${build.dir}/web"> |
79 | - <fileset dir="${src.dir}/web" includes="*" excludes="*.xml" /> | |
88 | + <fileset dir="${src.dir}/web/vues" includes="**" /> | |
80 | 89 | </copy> |
81 | 90 | <jar |
82 | 91 | basedir="${build.dir}/web/" |
83 | 92 | destfile="${build.dir}/jars/${name}.war" > |
84 | 93 | </jar> |
85 | 94 | </target> |
95 | + | |
86 | 96 | |
87 | 97 | <target name="earSession" depends="package-sessions"> |
88 | 98 | <schemavalidate file="${src.dir}/application.xml"/> | ... | ... |
demoCoursAL/src/application.xml
... | ... | @@ -11,5 +11,11 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns |
11 | 11 | <module> |
12 | 12 | <ejb>appliBanqueEntites.jar</ejb> |
13 | 13 | </module> |
14 | + <module> | |
15 | + <web> | |
16 | + <web-uri>appliBanque.war</web-uri> | |
17 | + <context-root>appBanque</context-root> | |
18 | + </web> | |
19 | + </module> | |
14 | 20 | </application> |
15 | 21 | ... | ... |
demoCoursAL/src/client/Main.java
... | ... | @@ -14,9 +14,10 @@ public class Main { |
14 | 14 | try { |
15 | 15 | InitialContext ctx = new InitialContext(); |
16 | 16 | System.out.println("Accès au service distant") ; |
17 | - Object obj = ctx.lookup("ejb:appliBanque/appliBanqueSessions//ServiceBanqueBean!ejb.sessions.ServiceBanque"); | |
17 | + Object obj = ctx.lookup("ejb:appliBanque/appliBanqueSessions//ServiceBanqueBean"+ | |
18 | + "!ejb.sessions.ServiceBanqueRemote"); | |
18 | 19 | |
19 | - ServiceBanque service = (ServiceBanque) obj ; | |
20 | + ServiceBanqueRemote service = (ServiceBanqueRemote) obj ; | |
20 | 21 | service.addCompte(1, "Olivier", 2000.0); |
21 | 22 | service.addCompte(2, "Paul", 300.0) ; |
22 | 23 | service.addAction("nintendo",46.5) ; | ... | ... |
demoCoursAL/src/ejb/sessions/ServiceBanque.java
1 | 1 | package ejb.sessions; |
2 | 2 | |
3 | 3 | import ejb.entites.LigneAction ; |
4 | +import ejb.entites.Compte ; | |
4 | 5 | |
5 | -@javax.ejb.Remote | |
6 | 6 | public interface ServiceBanque { |
7 | 7 | public void addCompte(int numeroCompte, String nomTitulaire, double soldeDepart) |
8 | 8 | throws CompteDejaExistantException ; |
9 | + public Compte getCompte(int numeroCompte) throws CompteInconnuException ; | |
9 | 10 | public void addAction(String nomAction, double taux) |
10 | 11 | throws ActionDejaExistanteException ; |
11 | 12 | public void crediterCompte(int numeroCompte, double montant) | ... | ... |
demoCoursAL/src/ejb/sessions/ServiceBanqueBean.java
1 | 1 | package ejb.sessions; |
2 | 2 | |
3 | +import java.util.Collection; | |
3 | 4 | import java.util.Set; |
4 | 5 | |
5 | 6 | import javax.persistence.EntityManager; |
... | ... | @@ -10,7 +11,8 @@ import ejb.entites.Compte; |
10 | 11 | import ejb.entites.LigneAction; |
11 | 12 | |
12 | 13 | @javax.ejb.Stateless |
13 | -public class ServiceBanqueBean implements ServiceBanque { | |
14 | +public class ServiceBanqueBean | |
15 | + implements ServiceBanqueLocal, ServiceBanqueRemote { | |
14 | 16 | @PersistenceContext(unitName="appliBanque") protected EntityManager em ; |
15 | 17 | @Override |
16 | 18 | public void addCompte(int numeroCompte, String nomTitulaire, double soldeDepart) |
... | ... | @@ -119,7 +121,7 @@ public class ServiceBanqueBean implements ServiceBanque { |
119 | 121 | return c.getLignesactions() ; |
120 | 122 | } |
121 | 123 | |
122 | - private Compte getCompte(int numeroCompte) throws CompteInconnuException { | |
124 | + @Override public Compte getCompte(int numeroCompte) throws CompteInconnuException { | |
123 | 125 | Compte c= (Compte) em.find(Compte.class, numeroCompte) ; |
124 | 126 | if (c==null) throw new CompteInconnuException() ; |
125 | 127 | return c; | ... | ... |
demoCoursAL/src/ejb/sessions/ServiceBanqueLocal.java
0 → 100644
demoCoursAL/src/ejb/sessions/ServiceBanqueRemote.java
0 → 100644
... | ... | @@ -0,0 +1,46 @@ |
1 | +package web.controleurs; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.PrintWriter; | |
5 | + | |
6 | +import javax.ejb.EJB; | |
7 | +import javax.servlet.ServletException; | |
8 | +import javax.servlet.annotation.WebServlet; | |
9 | +import javax.servlet.http.HttpServlet; | |
10 | +import javax.servlet.http.HttpServletRequest; | |
11 | +import javax.servlet.http.HttpServletResponse; | |
12 | + | |
13 | +import ejb.entites.Compte; | |
14 | +import ejb.sessions.CompteInconnuException; | |
15 | +import ejb.sessions.ServiceBanqueLocal; | |
16 | + | |
17 | +@WebServlet(value={"badDisplayCompte"}) | |
18 | +public class BadServlet extends HttpServlet { | |
19 | + @EJB ServiceBanqueLocal service ; | |
20 | + | |
21 | + public BadServlet() {} | |
22 | + | |
23 | + public void doGet(HttpServletRequest req,HttpServletResponse res) | |
24 | + throws ServletException, IOException { | |
25 | + int numeroCompte= | |
26 | + Integer.parseInt(req.getParameter("numeroCompte")) ; | |
27 | + Compte c=null ; | |
28 | + try { | |
29 | + c=service.getCompte(numeroCompte) ; | |
30 | + } catch (CompteInconnuException e) { | |
31 | + } | |
32 | + res.setContentType("text/html"); | |
33 | + PrintWriter out = res.getWriter(); | |
34 | + out.println("<html><head><title>demo servlet"+ | |
35 | + "</title></head>"); | |
36 | + out.println("<body>"); | |
37 | + if (c==null) | |
38 | + out.println("<h2>Compte "+numeroCompte+" inconnu</h2>") ; | |
39 | + else { | |
40 | + out.println("<h2>Compte "+numeroCompte+"</h2>") ; | |
41 | + out.println("<ul><li>Titulaire:<b>"+c.getTitulaire()+"</b></li>"); | |
42 | + out.println("<li>Solde:<b>"+c.getSolde()+"</b></li></ul>"); | |
43 | + } | |
44 | + out.println("</body></html>"); | |
45 | + } | |
46 | +} | ... | ... |
... | ... | @@ -0,0 +1,47 @@ |
1 | +package web.controleurs; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | + | |
5 | +import ejb.sessions.* ; | |
6 | +import ejb.entites.* ; | |
7 | + | |
8 | +import javax.servlet.RequestDispatcher; | |
9 | +import javax.servlet.ServletException; | |
10 | +import javax.servlet.annotation.WebServlet; | |
11 | +import javax.servlet.http.HttpServlet; | |
12 | +import javax.servlet.http.HttpServletRequest; | |
13 | +import javax.servlet.http.HttpServletResponse; | |
14 | + | |
15 | + | |
16 | +@WebServlet(value={"rechCompte","afficherCompte"}) | |
17 | +public class Controleur extends HttpServlet { | |
18 | + private static final long serialVersionUID = 1L; | |
19 | + | |
20 | + @javax.ejb.EJB private ServiceBanqueLocal service ; | |
21 | + | |
22 | + public Controleur() {} | |
23 | + | |
24 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
25 | + throws ServletException, IOException { | |
26 | + String url = request.getRequestURL().toString(); | |
27 | + String maVue ="/rechCompte.html"; // vue par défaut | |
28 | + if (url.endsWith("/rechCompte")) { | |
29 | + maVue ="/rechCompte.html"; // rien d'autre à faire | |
30 | + } else if (url.endsWith("/afficherCompte")) { | |
31 | + maVue = "/afficherCompte.jsp"; | |
32 | + int numeroCompte=Integer.parseInt(request.getParameter("numeroCompte")) ; | |
33 | + Compte compte ; | |
34 | + try { | |
35 | + compte= service.getCompte(numeroCompte); | |
36 | + java.util.Collection<LigneAction> l_la = service.getActionsAchetees(numeroCompte); | |
37 | + request.setAttribute("listeLA", l_la); | |
38 | + } catch (CompteInconnuException e) { | |
39 | + compte=null ; | |
40 | + } | |
41 | + request.setAttribute("compte",compte) ; | |
42 | + | |
43 | + } | |
44 | + RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(maVue); | |
45 | + dispatcher.forward(request,response); | |
46 | + } | |
47 | +} | ... | ... |
... | ... | @@ -0,0 +1,32 @@ |
1 | +<%@page contentType="text/html" pageEncoding="UTF-8"%> | |
2 | +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | |
3 | +<!DOCTYPE html> | |
4 | +<html> | |
5 | + <head> | |
6 | + <title>Application banque</title> | |
7 | + <meta charset="UTF-8" /> | |
8 | + </head> | |
9 | +<body> | |
10 | + <header><h1>Application banque</h1></header> | |
11 | + | |
12 | +<h2>Information compte:</h2> | |
13 | + <c:if test = "${not empty requestScope.compte}"> | |
14 | + <h3>Compte n° ${requestScope.compte.numero}</h3> | |
15 | + Titulaire : <b>${requestScope.compte.titulaire}</b><br /> | |
16 | + Solde <b>${requestScope.compte.solde}</b> | |
17 | + | |
18 | + <ul> | |
19 | + <c:forEach items="${requestScope.listeLA}" var="la"> | |
20 | + <li>${la.nombre} action(s) ${la.action.nom} | |
21 | + au taux de ${la.action.taux} | |
22 | + </li> | |
23 | + </c:forEach> | |
24 | + </ul> | |
25 | +</c:if> | |
26 | + | |
27 | +<c:if test = "${empty requestScope.compte}"> | |
28 | + <h3>Compte Inconnu</h3> | |
29 | +</c:if> | |
30 | + | |
31 | +</body> | |
32 | +</html> | |
0 | 33 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,37 @@ |
1 | +<%@ page language="java" contentType="text/html; charset=UTF-8" | |
2 | + pageEncoding="UTF-8"%> | |
3 | +<!DOCTYPE html> | |
4 | +<html> | |
5 | +<head> | |
6 | +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
7 | +<title>demo JSP</title> | |
8 | +</head> | |
9 | + | |
10 | +<%@ page import="javax.ejb.*" %> | |
11 | +<%@ page import="javax.naming.*" %> | |
12 | +<%@ page import="ejb.sessions.ServiceBanqueLocal" %> | |
13 | +<%@ page import="ejb.entites.Compte" %> | |
14 | +<%@ page import="ejb.sessions.CompteInconnuException" %> | |
15 | +<%! | |
16 | + Compte c ; | |
17 | + int numeroCompte ; | |
18 | +%> | |
19 | +<body> | |
20 | +<% | |
21 | + numeroCompte=Integer.parseInt(request.getParameter("numeroCompte")) ; | |
22 | + Compte c=null ; | |
23 | + InitialContext ic = new InitialContext(); | |
24 | + Object obj = ic.lookup("java:app/appliBanqueSessions/ServiceBanqueBean!ejb.sessions.ServiceBanqueLocal"); | |
25 | + ServiceBanqueLocal service= (ServiceBanqueLocal) obj ; | |
26 | + try { | |
27 | + c=service.getCompte(numeroCompte) ; | |
28 | +%> | |
29 | + <h2>Compte <%= numeroCompte%></h2> | |
30 | + <ul><li>Titulaire:<b><%= c.getTitulaire() %></b></li> | |
31 | + <li>Solde:<b><%= c.getSolde() %></b></li> | |
32 | + </ul> | |
33 | +<% } catch (CompteInconnuException e) { %> | |
34 | + <h2>Compte <%= numeroCompte %> inconnu</h2> ; | |
35 | +<% } // fin du try catch %> | |
36 | +</body> | |
37 | +</html> | |
0 | 38 | \ No newline at end of file | ... | ... |
... | ... | @@ -0,0 +1,18 @@ |
1 | +<!DOCTYPE HTML> | |
2 | +<html> | |
3 | + <head> | |
4 | + <title>Application Banque</title> <meta charset="UTF-8" /> | |
5 | + </head> | |
6 | + | |
7 | + <body> | |
8 | + <header><h1>Application banque</h1></header> | |
9 | + | |
10 | + <h2>Rechercher un compte</h2> | |
11 | + <form action="afficherCompte"> | |
12 | + Numéro du compte : | |
13 | + <input type="text" name="numeroCompte" | |
14 | + required placeholder="numéro du compte..." /> | |
15 | + <input type="submit" value="Rechercher" > | |
16 | + </form> | |
17 | +</body> | |
18 | +</html> | ... | ... |