projets:2018:stagejava
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteDernière révisionLes deux révisions suivantes | ||
projets:2018:stagejava [2018/10/24 10:02] – christian.jacolot | projets:2018:stagejava [2019/03/03 21:30] – christian.jacolot | ||
---|---|---|---|
Ligne 49: | Ligne 49: | ||
< | < | ||
- | package bzh.lph.services; | + | package bzh.lph.stage; |
import java.io.BufferedReader; | import java.io.BufferedReader; | ||
Ligne 89: | Ligne 89: | ||
===== Java Interface Graphique ===== | ===== Java Interface Graphique ===== | ||
+ | |||
+ | Présentation: | ||
+ | |||
+ | Code de l' | ||
+ | |||
+ | < | ||
+ | package bzh.lph.services; | ||
+ | |||
+ | import java.awt.FlowLayout; | ||
+ | import java.awt.event.ActionEvent; | ||
+ | import java.awt.event.ActionListener; | ||
+ | |||
+ | import javax.swing.ImageIcon; | ||
+ | import javax.swing.JButton; | ||
+ | import javax.swing.JFrame; | ||
+ | import javax.swing.JLabel; | ||
+ | import javax.swing.JOptionPane; | ||
+ | import javax.swing.JPanel; | ||
+ | import javax.swing.JTextField; | ||
+ | |||
+ | public class DevineGui { | ||
+ | |||
+ | private final static int max = 100; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | // jeu | ||
+ | int nombre = (int) (Math.random() * max) + 1; // à deviner | ||
+ | int nbCoup = 0; | ||
+ | |||
+ | // fenêtre | ||
+ | JFrame f = new JFrame(" | ||
+ | f.setSize(640, | ||
+ | |||
+ | // conteneur | ||
+ | JPanel panel = new JPanel(); | ||
+ | |||
+ | // configure le layout du conteneur | ||
+ | panel.setLayout(new FlowLayout()); | ||
+ | |||
+ | // créer les widgets | ||
+ | JLabel msgLbl = new JLabel(" | ||
+ | JLabel msgEntreLbl = new JLabel(" | ||
+ | JTextField nombreTF = new JTextField(); | ||
+ | nombreTF.setText(" | ||
+ | JButton validationBtn = new JButton(" | ||
+ | |||
+ | // quand le bouton validation est cliqué, vérifie le nombre | ||
+ | validationBtn.addActionListener(new ActionListener() { | ||
+ | public void actionPerformed(ActionEvent e) { | ||
+ | String textNombre = nombreTF.getText(); | ||
+ | int i = 0; | ||
+ | try { | ||
+ | i = Integer.parseInt(textNombre); | ||
+ | if (i < nombre) { | ||
+ | JOptionPane.showConfirmDialog(f, | ||
+ | " | ||
+ | JOptionPane.OK_CANCEL_OPTION); | ||
+ | } else if (i > nombre) { | ||
+ | JOptionPane.showMessageDialog(f, | ||
+ | " | ||
+ | JOptionPane.OK_CANCEL_OPTION); | ||
+ | } else { | ||
+ | JOptionPane.showMessageDialog(f, | ||
+ | " | ||
+ | JOptionPane.OK_CANCEL_OPTION); | ||
+ | } | ||
+ | } catch (NumberFormatException nfe) { | ||
+ | JOptionPane.showMessageDialog(f, | ||
+ | " | ||
+ | JOptionPane.OK_CANCEL_OPTION); | ||
+ | } | ||
+ | // nbCoup += 1; // nbCoup = nbCoup +1; | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | // ajoute les widgets au conteneur | ||
+ | panel.add(msgLbl); | ||
+ | panel.add(msgEntreLbl); | ||
+ | panel.add(nombreTF); | ||
+ | panel.add(validationBtn); | ||
+ | |||
+ | // ajoute le conteneur à la fenêtre | ||
+ | f.getContentPane().add(panel); | ||
+ | // la fenêtre est visible et active | ||
+ | f.setVisible(true); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Java Stockage: Base de données ===== | ||
+ | |||
+ | Présentation: | ||
+ | |||
+ | Code de l' | ||
+ | |||
+ | < | ||
+ | package bzh.lph.stage; | ||
+ | |||
+ | import java.sql.Connection; | ||
+ | import java.sql.DriverManager; | ||
+ | import java.sql.PreparedStatement; | ||
+ | import java.sql.ResultSet; | ||
+ | import java.sql.SQLException; | ||
+ | import java.sql.Statement; | ||
+ | |||
+ | public class GestionContact { | ||
+ | |||
+ | private static Connection conn = null; | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | connect(); | ||
+ | // lireData(); | ||
+ | // ecrireData(); | ||
+ | // effacerData(); | ||
+ | disconnect(); | ||
+ | } | ||
+ | |||
+ | // connection à la base de contact | ||
+ | public static void connect() { | ||
+ | conn = null; | ||
+ | try { | ||
+ | // les paramêtres de la connexion à la base de données | ||
+ | String url = " | ||
+ | // ouvrir la base de données | ||
+ | conn = DriverManager.getConnection(url); | ||
+ | System.out.println(" | ||
+ | } catch (SQLException e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // fermer la connexion à la base de données | ||
+ | private static void disconnect() { | ||
+ | try { | ||
+ | if (conn != null) { | ||
+ | conn.close(); | ||
+ | } | ||
+ | } catch (SQLException ex) { | ||
+ | System.out.println(ex.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void lireData() { | ||
+ | if (conn != null) { | ||
+ | try { | ||
+ | // créer à partir de la connexion la requête | ||
+ | Statement stmt = conn.createStatement(); | ||
+ | // exécuter la requête | ||
+ | ResultSet rs | ||
+ | = stmt.executeQuery(" | ||
+ | // parcourir le résultat | ||
+ | while (rs.next()) { | ||
+ | int numeroContact = rs.getInt(" | ||
+ | String nomContact = rs.getString(" | ||
+ | String prenomContact = rs.getString(" | ||
+ | System.out.println( | ||
+ | " | ||
+ | + ", Nom: " + nomContact + ", Prénom: " + prenomContact); | ||
+ | } | ||
+ | } catch (SQLException e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void ecrireData() { | ||
+ | if (conn != null) { | ||
+ | try { | ||
+ | // préparer la requête | ||
+ | String nomContact = " | ||
+ | String prenomContact = " | ||
+ | String insertionContact = " | ||
+ | + prenomContact + "' | ||
+ | // créer à partir de la connexion la requête | ||
+ | Statement stmt = conn.createStatement(); | ||
+ | // exécuter la requête | ||
+ | stmt.executeUpdate(insertionContact); | ||
+ | System.out.println(" | ||
+ | } catch (SQLException e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private static void effacerData() { | ||
+ | if (conn != null) { | ||
+ | try { | ||
+ | // préparer la requête | ||
+ | int numeroJacolot = 1; | ||
+ | String effacterRqt = " | ||
+ | // créer une requête à partir de la connexion | ||
+ | PreparedStatement deleteStmt = conn.prepareStatement(effacterRqt); | ||
+ | // positionner le paramètre | ||
+ | deleteStmt.setInt(1, | ||
+ | // exécuter la requête | ||
+ | deleteStmt.executeUpdate(); | ||
+ | System.out.println(" | ||
+ | } catch (SQLException e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | < | ||
+ | mvn archetype: | ||
+ | |||
+ | </ | ||
+ | |||
+ | pom.xml | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | </ | ||
+ | |||
+ | |||
+ | Class EntryPoint.java | ||
+ | |||
+ | < | ||
+ | package net.mdl29.test; | ||
+ | |||
+ | import javax.ws.rs.GET; | ||
+ | import javax.ws.rs.Path; | ||
+ | import javax.ws.rs.Produces; | ||
+ | import javax.ws.rs.core.MediaType; | ||
+ | |||
+ | @Path("/ | ||
+ | public class EntryPoint { | ||
+ | |||
+ | @GET | ||
+ | @Path(" | ||
+ | @Produces(MediaType.TEXT_PLAIN) | ||
+ | public String test() { | ||
+ | return " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Class App.java | ||
+ | |||
+ | < | ||
+ | package net.mdl29.test; | ||
+ | |||
+ | import org.eclipse.jetty.server.Server; | ||
+ | import org.eclipse.jetty.servlet.ServletContextHandler; | ||
+ | import org.eclipse.jetty.servlet.ServletHolder; | ||
+ | |||
+ | public class App { | ||
+ | public static void main(String[] args) throws Exception { | ||
+ | ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); | ||
+ | context.setContextPath("/" | ||
+ | |||
+ | Server jettyServer = new Server(8080); | ||
+ | jettyServer.setHandler(context); | ||
+ | |||
+ | ServletHolder jerseyServlet = context.addServlet( | ||
+ | | ||
+ | jerseyServlet.setInitOrder(0); | ||
+ | |||
+ | // Tells the Jersey Servlet which REST service/ | ||
+ | jerseyServlet.setInitParameter( | ||
+ | " | ||
+ | | ||
+ | |||
+ | try { | ||
+ | jettyServer.start(); | ||
+ | jettyServer.join(); | ||
+ | } finally { | ||
+ | jettyServer.destroy(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | Ajouter la gestion du manifeste dans pom.xml | ||
+ | |||
+ | < | ||
+ | < | ||
+ | ... | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | implementation=" | ||
+ | < | ||
+ | implementation=" | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | ... | ||
+ | |||
+ | </ | ||
+ | |||
+ | </ | ||
+ | |||
+ | pom.xml | ||
+ | < | ||
+ | <project xmlns=" | ||
+ | |||
+ | xmlns: | ||
+ | |||
+ | xsi: | ||
+ | http:// | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | <!-- Dependency needed by the Web-socket --> | ||
+ | <!-- Tomcat has it, so no need to package into the war file --> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | <!-- Used to serialize the message from the browser --> | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | | ||
+ | <!-- Sevlet jars for compilation, | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | |||
+ | class ChatWebsocket.java | ||
+ | < | ||
+ | /* | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | * | ||
+ | | ||
+ | * | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | */ | ||
+ | package net.mdl29.websocket; | ||
+ | |||
+ | import java.io.IOException; | ||
+ | import java.util.Set; | ||
+ | import java.util.concurrent.CopyOnWriteArraySet; | ||
+ | import java.util.concurrent.atomic.AtomicInteger; | ||
+ | |||
+ | import javax.websocket.OnClose; | ||
+ | import javax.websocket.OnError; | ||
+ | import javax.websocket.OnMessage; | ||
+ | import javax.websocket.OnOpen; | ||
+ | import javax.websocket.Session; | ||
+ | import javax.websocket.server.ServerEndpoint; | ||
+ | |||
+ | @ServerEndpoint(value = "/ | ||
+ | public class ChatWebsocket { | ||
+ | |||
+ | private static final String GUEST_PREFIX = " | ||
+ | private static final AtomicInteger connectionIds = new AtomicInteger(0); | ||
+ | private static final Set< | ||
+ | new CopyOnWriteArraySet<> | ||
+ | |||
+ | private final String nickname; | ||
+ | private Session session; | ||
+ | |||
+ | public ChatWebsocket() { | ||
+ | nickname = GUEST_PREFIX + connectionIds.getAndIncrement(); | ||
+ | } | ||
+ | |||
+ | |||
+ | @OnOpen | ||
+ | public void start(Session session) { | ||
+ | this.session = session; | ||
+ | connections.add(this); | ||
+ | String message = String.format(" | ||
+ | broadcast(message); | ||
+ | } | ||
+ | |||
+ | |||
+ | @OnClose | ||
+ | public void end() { | ||
+ | connections.remove(this); | ||
+ | String message = String.format(" | ||
+ | nickname, "has disconnected." | ||
+ | broadcast(message); | ||
+ | } | ||
+ | |||
+ | |||
+ | @OnMessage | ||
+ | public void incoming(String message) { | ||
+ | // Never trust the client | ||
+ | String filteredMessage = String.format(" | ||
+ | nickname, filter(message.toString())); | ||
+ | broadcast(filteredMessage); | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | @OnError | ||
+ | public void onError(Throwable t) throws Throwable { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | |||
+ | private static void broadcast(String msg) { | ||
+ | for (ChatWebsocket client : connections) { | ||
+ | try { | ||
+ | synchronized (client) { | ||
+ | client.session.getBasicRemote().sendText(msg); | ||
+ | } | ||
+ | } catch (IOException e) { | ||
+ | System.out.println(" | ||
+ | connections.remove(client); | ||
+ | try { | ||
+ | client.session.close(); | ||
+ | } catch (IOException e1) { | ||
+ | // Ignore | ||
+ | } | ||
+ | String message = String.format(" | ||
+ | client.nickname, | ||
+ | broadcast(message); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | public static String filter(String message) { | ||
+ | |||
+ | if (message == null) | ||
+ | return null; | ||
+ | |||
+ | char content[] = new char[message.length()]; | ||
+ | message.getChars(0, | ||
+ | StringBuilder result = new StringBuilder(content.length + 50); | ||
+ | for (int i = 0; i < content.length; | ||
+ | switch (content[i]) { | ||
+ | case '<': | ||
+ | result.append("& | ||
+ | break; | ||
+ | case '>': | ||
+ | result.append("& | ||
+ | break; | ||
+ | case '&': | ||
+ | result.append("& | ||
+ | break; | ||
+ | case '"': | ||
+ | result.append("& | ||
+ | break; | ||
+ | default: | ||
+ | result.append(content[i]); | ||
+ | } | ||
+ | } | ||
+ | return result.toString(); | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Web client chat.xhtml: | ||
+ | < | ||
+ | <?xml version=" | ||
+ | <!-- | ||
+ | Licensed to the Apache Software Foundation (ASF) under one or more | ||
+ | contributor license agreements. | ||
+ | this work for additional information regarding copyright ownership. | ||
+ | The ASF licenses this file to You under the Apache License, Version 2.0 | ||
+ | (the " | ||
+ | the License. | ||
+ | |||
+ | http:// | ||
+ | |||
+ | Unless required by applicable law or agreed to in writing, software | ||
+ | distributed under the License is distributed on an "AS IS" BASIS, | ||
+ | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
+ | See the License for the specific language governing permissions and | ||
+ | limitations under the License. | ||
+ | --> | ||
+ | <html xmlns=" | ||
+ | < | ||
+ | < | ||
+ | <style type=" | ||
+ | input#chat { | ||
+ | width: 410px | ||
+ | } | ||
+ | |||
+ | # | ||
+ | width: 400px; | ||
+ | } | ||
+ | |||
+ | #console { | ||
+ | border: 1px solid #CCCCCC; | ||
+ | border-right-color: | ||
+ | border-bottom-color: | ||
+ | height: 170px; | ||
+ | overflow-y: scroll; | ||
+ | padding: 5px; | ||
+ | width: 100%; | ||
+ | } | ||
+ | |||
+ | #console p { | ||
+ | padding: 0; | ||
+ | margin: 0; | ||
+ | } | ||
+ | ]]></ | ||
+ | <script type=" | ||
+ | "use strict"; | ||
+ | |||
+ | var Chat = {}; | ||
+ | |||
+ | Chat.socket = null; | ||
+ | |||
+ | Chat.connect = (function(host) { | ||
+ | if (' | ||
+ | Chat.socket = new WebSocket(host); | ||
+ | } else if (' | ||
+ | Chat.socket = new MozWebSocket(host); | ||
+ | } else { | ||
+ | Console.log(' | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | Chat.socket.onopen = function () { | ||
+ | Console.log(' | ||
+ | document.getElementById(' | ||
+ | if (event.keyCode == 13) { | ||
+ | Chat.sendMessage(); | ||
+ | } | ||
+ | }; | ||
+ | }; | ||
+ | |||
+ | Chat.socket.onclose = function () { | ||
+ | document.getElementById(' | ||
+ | Console.log(' | ||
+ | }; | ||
+ | |||
+ | Chat.socket.onmessage = function (message) { | ||
+ | Console.log(message.data); | ||
+ | }; | ||
+ | }); | ||
+ | |||
+ | Chat.initialize = function() { | ||
+ | if (window.location.protocol == ' | ||
+ | Chat.connect(' | ||
+ | } else { | ||
+ | Chat.connect(' | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | Chat.sendMessage = (function() { | ||
+ | var message = document.getElementById(' | ||
+ | if (message != '' | ||
+ | Chat.socket.send(message); | ||
+ | document.getElementById(' | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | var Console = {}; | ||
+ | |||
+ | Console.log = (function(message) { | ||
+ | var console = document.getElementById(' | ||
+ | var p = document.createElement(' | ||
+ | p.style.wordWrap = ' | ||
+ | p.innerHTML = message; | ||
+ | console.appendChild(p); | ||
+ | while (console.childNodes.length > 25) { | ||
+ | console.removeChild(console.firstChild); | ||
+ | } | ||
+ | console.scrollTop = console.scrollHeight; | ||
+ | }); | ||
+ | |||
+ | Chat.initialize(); | ||
+ | |||
+ | |||
+ | document.addEventListener(" | ||
+ | // Remove elements with " | ||
+ | var noscripts = document.getElementsByClassName(" | ||
+ | for (var i = 0; i < noscripts.length; | ||
+ | noscripts[i].parentNode.removeChild(noscripts[i]); | ||
+ | } | ||
+ | }, false); | ||
+ | |||
+ | ]]></ | ||
+ | </ | ||
+ | < | ||
+ | <div class=" | ||
+ | Javascript and reload this page!</ | ||
+ | <div> | ||
+ | <p> | ||
+ | <input type=" | ||
+ | </p> | ||
+ | <div id=" | ||
+ | <div id=" | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | Les sources complets avec pom, html, java: {{ : | ||
projets/2018/stagejava.txt · Dernière modification : 2024/04/16 22:26 de 127.0.0.1