envoie du get/post
This commit is contained in:
parent
726f2d8cba
commit
dac29b05e8
7 changed files with 126 additions and 96 deletions
|
@ -1,9 +1,11 @@
|
||||||
package defaut;
|
package defaut;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import bdd.GestionnaireHistorique;
|
import bdd.GestionnaireHistorique;
|
||||||
import liste.GestionnaireListeUtilisateur;
|
import liste.GestionnaireListeUtilisateur;
|
||||||
import ui.Login_RegisterUI;
|
import ui.Login_RegisterUI;
|
||||||
|
import servlet.Get;
|
||||||
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
@ -20,6 +22,11 @@ public class Main {
|
||||||
//on met à jour notre liste
|
//on met à jour notre liste
|
||||||
GestionnaireListeUtilisateur.instance().majListe2(); //enlever le 2 //TODO
|
GestionnaireListeUtilisateur.instance().majListe2(); //enlever le 2 //TODO
|
||||||
|
|
||||||
|
try {
|
||||||
|
Get.sendGET();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
};
|
||||||
|
|
||||||
//on lance le UI
|
//on lance le UI
|
||||||
Thread t = new Thread(new Login_RegisterUI());
|
Thread t = new Thread(new Login_RegisterUI());
|
||||||
|
|
|
@ -12,6 +12,7 @@ public class GestionnaireNom{
|
||||||
private String nomUtilisateur;
|
private String nomUtilisateur;
|
||||||
static private String ipUtilisateur;
|
static private String ipUtilisateur;
|
||||||
private boolean dansReseau;
|
private boolean dansReseau;
|
||||||
|
private String statut = "online";
|
||||||
|
|
||||||
|
|
||||||
//Instance du gestionnaire de nom
|
//Instance du gestionnaire de nom
|
||||||
|
@ -58,12 +59,18 @@ public class GestionnaireNom{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//retorurne dansReseau
|
// retourne dansReseau
|
||||||
public boolean getDansReseau() {
|
public boolean getDansReseau() {
|
||||||
return dansReseau;
|
return dansReseau;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// retourne statut
|
||||||
|
public String getStatut() {
|
||||||
|
return statut;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// set le NomUtilisateur
|
// set le NomUtilisateur
|
||||||
public void setNom(String nom) {
|
public void setNom(String nom) {
|
||||||
|
@ -91,6 +98,13 @@ public class GestionnaireNom{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//set dansReseau
|
||||||
|
public void setStatut(String statut) {
|
||||||
|
this.statut = statut;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// renvoie l'ID d'un utilisateur ou "null" s'il n'existe pas (ou non en ligne)
|
// renvoie l'ID d'un utilisateur ou "null" s'il n'existe pas (ou non en ligne)
|
||||||
public String idFromNom(String nom) {
|
public String idFromNom(String nom) {
|
||||||
for (int i=0; i<GestionnaireListeUtilisateur.instance().getListeUtilisateur().size(); i++) {
|
for (int i=0; i<GestionnaireListeUtilisateur.instance().getListeUtilisateur().size(); i++) {
|
||||||
|
|
31
Projet_POO/src/servlet/Get.java
Normal file
31
Projet_POO/src/servlet/Get.java
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
package servlet;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class Get {
|
||||||
|
|
||||||
|
//private static final String USER_AGENT = "Mozilla/5.0";
|
||||||
|
|
||||||
|
private static final String GET_URL = "http://localhost:8080/Servlet_MBP/test";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static void sendGET() throws IOException { // SUbscribe
|
||||||
|
URL obj = new URL(GET_URL);
|
||||||
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
||||||
|
con.setRequestMethod("GET");
|
||||||
|
//con.setRequestProperty("User-Agent", USER_AGENT);
|
||||||
|
int responseCode = con.getResponseCode();
|
||||||
|
System.out.println("GET Response Code :: " + responseCode);
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) { // success
|
||||||
|
System.out.println("Get success");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
System.out.println("GET request not worked");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
37
Projet_POO/src/servlet/Post.java
Normal file
37
Projet_POO/src/servlet/Post.java
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package servlet;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
|
public class Post {
|
||||||
|
|
||||||
|
private static final String POST_URL = "http://localhost:8080/Servlet_MBP/test";
|
||||||
|
|
||||||
|
public static void sendPOST(String msg) throws IOException { // Publish cmd : change$$$id$$$nom$$$ip$$$dedans$$$statut
|
||||||
|
URL obj = new URL(POST_URL);
|
||||||
|
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
||||||
|
con.setRequestMethod("POST");
|
||||||
|
|
||||||
|
// Output Stream
|
||||||
|
con.setDoOutput(true);
|
||||||
|
DataOutputStream os = new DataOutputStream(con.getOutputStream());
|
||||||
|
//os.write(POST_PARAMS.getBytes());
|
||||||
|
os.writeBytes(msg);
|
||||||
|
os.flush();
|
||||||
|
os.close();
|
||||||
|
// For POST only - END
|
||||||
|
|
||||||
|
int responseCode = con.getResponseCode();
|
||||||
|
System.out.println("POST Response Code :: " + responseCode);
|
||||||
|
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) { //success
|
||||||
|
System.out.println("Post success");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
System.out.println("POST request not worked");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,91 +0,0 @@
|
||||||
package servlet;
|
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
public class Url {
|
|
||||||
|
|
||||||
//private static final String USER_AGENT = "Mozilla/5.0";
|
|
||||||
|
|
||||||
private static final String GET_URL = "http://localhost:8080/Servlet_MBP/test";
|
|
||||||
|
|
||||||
private static final String POST_URL = "http://localhost:8080/Servlet_MBP/test";
|
|
||||||
|
|
||||||
private static final String POST_PARAMS = "userName=Pankaj";
|
|
||||||
|
|
||||||
public static void main(String[] args) throws IOException {
|
|
||||||
|
|
||||||
sendGET();
|
|
||||||
System.out.println("GET DONE");
|
|
||||||
sendPOST();
|
|
||||||
System.out.println("POST DONE");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void sendGET() throws IOException {
|
|
||||||
URL obj = new URL(GET_URL);
|
|
||||||
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
||||||
con.setRequestMethod("GET");
|
|
||||||
//con.setRequestProperty("User-Agent", USER_AGENT);
|
|
||||||
int responseCode = con.getResponseCode();
|
|
||||||
System.out.println("GET Response Code :: " + responseCode);
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) { // success
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(
|
|
||||||
con.getInputStream()));
|
|
||||||
String inputLine;
|
|
||||||
StringBuffer response = new StringBuffer();
|
|
||||||
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
response.append(inputLine);
|
|
||||||
}
|
|
||||||
in.close();
|
|
||||||
|
|
||||||
// print result
|
|
||||||
System.out.println(response.toString());
|
|
||||||
} else {
|
|
||||||
System.out.println("GET request not worked");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void sendPOST() throws IOException {
|
|
||||||
URL obj = new URL(POST_URL);
|
|
||||||
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
||||||
con.setRequestMethod("POST");
|
|
||||||
//con.setRequestProperty("User-Agent", USER_AGENT);
|
|
||||||
|
|
||||||
// For POST only - START
|
|
||||||
con.setDoOutput(true);
|
|
||||||
DataOutputStream os = new DataOutputStream(con.getOutputStream());
|
|
||||||
//os.write(POST_PARAMS.getBytes());
|
|
||||||
os.writeBytes("bonjour");
|
|
||||||
os.flush();
|
|
||||||
os.close();
|
|
||||||
// For POST only - END
|
|
||||||
|
|
||||||
int responseCode = con.getResponseCode();
|
|
||||||
System.out.println("POST Response Code :: " + responseCode);
|
|
||||||
|
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) { //success
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(
|
|
||||||
con.getInputStream()));
|
|
||||||
String inputLine;
|
|
||||||
StringBuffer response = new StringBuffer();
|
|
||||||
|
|
||||||
while ((inputLine = in.readLine()) != null) {
|
|
||||||
response.append(inputLine);
|
|
||||||
}
|
|
||||||
in.close();
|
|
||||||
|
|
||||||
// print result
|
|
||||||
System.out.println(response.toString());
|
|
||||||
} else {
|
|
||||||
System.out.println("POST request not worked");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -26,10 +26,12 @@ import java.util.ArrayList;
|
||||||
import clavardage.gestionnaireClavardage;
|
import clavardage.gestionnaireClavardage;
|
||||||
import liste.GestionnaireListeUtilisateur;
|
import liste.GestionnaireListeUtilisateur;
|
||||||
import nom.GestionnaireNom;
|
import nom.GestionnaireNom;
|
||||||
|
import servlet.Post;
|
||||||
import liste.TypeListeUtilisateur;
|
import liste.TypeListeUtilisateur;
|
||||||
import javax.swing.JComboBox;
|
import javax.swing.JComboBox;
|
||||||
import javax.swing.DefaultComboBoxModel;
|
import javax.swing.DefaultComboBoxModel;
|
||||||
import java.awt.event.ItemListener;
|
import java.awt.event.ItemListener;
|
||||||
|
import java.io.IOException;
|
||||||
import java.awt.event.ItemEvent;
|
import java.awt.event.ItemEvent;
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,6 +42,8 @@ public class ListUI2 extends JFrame implements Runnable{
|
||||||
private static final long serialVersionUID = 42L;
|
private static final long serialVersionUID = 42L;
|
||||||
|
|
||||||
private JPanel contentPane;
|
private JPanel contentPane;
|
||||||
|
private JComboBox<String> comboBox = new JComboBox<String>();
|
||||||
|
|
||||||
private static JList<TypeListeUtilisateur> list = new JList<TypeListeUtilisateur>();
|
private static JList<TypeListeUtilisateur> list = new JList<TypeListeUtilisateur>();
|
||||||
|
|
||||||
|
|
||||||
|
@ -183,11 +187,11 @@ public class ListUI2 extends JFrame implements Runnable{
|
||||||
contentPane.add(nameLabel);
|
contentPane.add(nameLabel);
|
||||||
|
|
||||||
// combobox
|
// combobox
|
||||||
JComboBox<String> comboBox = new JComboBox<String>();
|
|
||||||
comboBox.addItemListener(new ItemListener() {
|
comboBox.addItemListener(new ItemListener() {
|
||||||
public void itemStateChanged(ItemEvent e) {
|
public void itemStateChanged(ItemEvent e) {
|
||||||
if(e.getStateChange() == ItemEvent.SELECTED) {
|
if(e.getStateChange() == ItemEvent.SELECTED) {
|
||||||
System.out.println(comboBox.getSelectedItem());
|
actionComboBox();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -274,4 +278,22 @@ public class ListUI2 extends JFrame implements Runnable{
|
||||||
|
|
||||||
list.setModel(defaultListModel);
|
list.setModel(defaultListModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// action ComboBox
|
||||||
|
private void actionComboBox() {
|
||||||
|
String item = (String) comboBox.getSelectedItem();
|
||||||
|
System.out.println(item); // debug
|
||||||
|
GestionnaireNom.instance().setStatut(item);
|
||||||
|
// on envoie l'info à la servlet
|
||||||
|
try {
|
||||||
|
Post.sendPOST("nom$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom()+ "$$$"
|
||||||
|
+ GestionnaireNom.instance().getIp() + "$$$" + GestionnaireNom.instance().getDansReseau() + "$$$"
|
||||||
|
+ GestionnaireNom.instance().getStatut() );
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,10 @@ import java.awt.event.FocusAdapter;
|
||||||
import java.awt.event.FocusEvent;
|
import java.awt.event.FocusEvent;
|
||||||
import java.awt.event.KeyAdapter;
|
import java.awt.event.KeyAdapter;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import nom.GestionnaireNom;
|
import nom.GestionnaireNom;
|
||||||
|
import servlet.Post;
|
||||||
|
|
||||||
public class NomUI extends JFrame implements Runnable{
|
public class NomUI extends JFrame implements Runnable{
|
||||||
|
|
||||||
|
@ -134,6 +136,14 @@ public class NomUI extends JFrame implements Runnable{
|
||||||
//Celui-ci ne peut pas être "vide"
|
//Celui-ci ne peut pas être "vide"
|
||||||
if (!nom.isBlank()) {
|
if (!nom.isBlank()) {
|
||||||
GestionnaireNom.instance().nommer2(nom); //enlever le 2 à la fin //TODO
|
GestionnaireNom.instance().nommer2(nom); //enlever le 2 à la fin //TODO
|
||||||
|
// on envoie l'info à la servlet
|
||||||
|
try {
|
||||||
|
Post.sendPOST("nom$$$" + GestionnaireNom.instance().getId() + "$$$" + GestionnaireNom.instance().getNom()+ "$$$"
|
||||||
|
+ GestionnaireNom.instance().getIp() + "$$$" + GestionnaireNom.instance().getDansReseau() + "$$$"
|
||||||
|
+ GestionnaireNom.instance().getStatut() );
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
dispose();
|
dispose();
|
||||||
|
|
||||||
Thread t = new Thread(new ListUI2());
|
Thread t = new Thread(new ListUI2());
|
||||||
|
|
Loading…
Reference in a new issue