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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
| package com.aster;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Salut extends MIDlet implements CommandListener{
private final Form DonneesForm; //Formulaire d'envoi de données
//Champs du formulaire
private final TextField NOM,ECOLE;
//Bouton Exit
private static final Command exitCommand = new Command("Exit", Command.EXIT,1);
//Bouton d'envoi par méthode GET
private static final Command parGet = new Command("ParGet", Command.ITEM, 1);
//Bouton d'envoi par méthode POST
private static final Command parPost = new Command("ParPost", Command.ITEM, 1);
private Display display; //Display Object
//Affiche le résultat envoyé par le serveur.
private StringItem msgResultat;
public Salut()
{
DonneesForm= new Form("EnvoiServlet");
display = Display.getDisplay(this);
//On initialise les champs à vide
NOM = new TextField("Nom ","",30, TextField.ANY);
ECOLE= new TextField("Ecole","",30, TextField.ANY);
//emailCorps = new TextField("EMail", "Ici texte de mail",150, TextField.ANY);
msgResultat = new StringItem("Resultat : ", "");
//On attache les champs, boutons et item au formulaire
DonneesForm.append(NOM);
DonneesForm.append(ECOLE);
DonneesForm.append(ECOLE.getString());
//MailForm.append(emailCorps);
DonneesForm.append(msgResultat);
DonneesForm.addCommand(parPost);
DonneesForm.addCommand(parGet);
DonneesForm.addCommand(exitCommand);
DonneesForm.setCommandListener(this);
}
protected void destroyApp(boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() {
//Au démarrage de l'application on affiche le formulaire
display.setCurrent(DonneesForm);
}
public void commandAction(Command comd, Displayable disp) {
if(comd==parPost)
{
String url= "http://localhost:8080/MomPro/essai" ;
try {
new Thread(new Runnable(){
public void run()
{
try{
String url= "http://localhost:8080/MomPro/essai" ;
envoiMailPost(url);
}catch(Exception e){}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(comd==parGet){
DonneesForm.append(NOM.getString());
DonneesForm.append(ECOLE.getString());
//MailForm.append("http://localhost:8080/MomPro/essai" + "?");
String url= "http://localhost:8080/MomPro/essai" + "?";
try {
new Thread(new Runnable(){
public void run()
{
try{
String url= "http://localhost:8080/MomPro/essai"+"?" ;
envoiMailGet(url);
}catch(Exception e){}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
//Bouton Exit est cliqué
else if (comd==exitCommand)
{
//On ferme l'application
destroyApp(false);
notifyDestroyed();
}
}
//Cette méthode gére l'envoi d'informations via la méthode POST
public void envoiMailPost(String url) throws Exception
{
HttpConnection connection = null;
DataOutputStream dos = null;
DataInputStream dis = null;
try {
connection = (HttpConnection)Connector.open("http://localhost:8080/MomPro/essai");
connection.setRequestMethod(HttpConnection.POST);
int rc = connection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
dos = connection.openDataOutputStream();
byte[] data =("NOM="+NOM.getString()).getBytes();
System.out.println("before");
dos.writeUTF("ndella");
dos.write(data);
data = ("&ECOLE="+ECOLE.getString()).getBytes();
dos.write(data);
System.out.println("after");
dos.flush();
dis = connection.openDataInputStream();
// byte[] data = new byte[10];
dis.readFully(data);
String message = new String(data);
System.out.println(message);
DonneesForm.append(message);
} catch(Exception e) { }
finally {
try {
if(dis != null) { dis.close();}
if(connection != null) {connection.close();
dos.close();}
}
catch(Exception e) {}
}
}
public void envoiMailGet(String url) throws Exception
{
HttpConnection connection = null; //Pour se connecter au serveur
InputStream inStrm = null; //Pour recevoir la réponse du serveur
try
{
//On concatène l'url avec les informations du formulaire.
url = url + "NOM=" + NOM.getString();
url = url + "&ECOLE=" + ECOLE.getString();
//Ouverture de la connexion et choix de la méthode GEt
connection = (HttpConnection) Connector.open(url);
connection.setRequestMethod(HttpConnection.GET);
inStrm = connection.openInputStream();
//Appel de la méthode respServeur pour afficher
//les résultats renvoyées par le serveur
respServeur(connection, inStrm);
}
finally
{
// On ferme la connexion et le flux d'entrée
if (inStrm != null)
inStrm.close();
if (connection != null)
connection.close();
}
}
//Cette méthode affiche le résultat du serveur
//(Voir la réponse de la Servlet ServletWebMail.java)
public void respServeur(HttpConnection connection,InputStream inStrm) throws IOException
{
//Si le serveur a renvoyé une information ?
if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{
//Longueur du flux
int length = (int) connection.getLength();
String stringRes="";
if (length != -1)
{
//valable que si le flux d'entrée est relativement petit
byte servletData[] = new byte[length];
inStrm.read(servletData);
stringRes = new String(servletData);
}
//On affiche le résultat sur le formulaire, en modifiant
//la avleur de msgResultat
msgResultat.setText(stringRes);
//String adc = msgResultat.getText();
//MailForm.append(adc);
}
}
}
et celui de ma servlet :
package com.estm;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Essai extends HttpServlet {
public void doPost( HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{
res.setContentType("text/html");
PrintWriter out= res.getWriter();
String nom= req.getParameter("NOM");
String ecole= req.getParameter("ECOLE");
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello, "+ nom+ " </TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, "+nom+" BIENVENUE A L'"+ecole);
out.println("</BODY></HTML>");
}
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter writer = res.getWriter();
String nom = req.getParameter ("NOM");
String ecole = req.getParameter ("ECOLE");
writer.println("Hello World !!!"+nom+" "+ecole);
}
} |
Partager