begin process at 2010 02 10 08:02:40
  Trouver un code source :
 
dans
 
Accueil > Forum > 

JAVA / J2EE / J2ME

 > 

Réseau & internet

 > 

Servlet

 > 

COMMUNICATION APPLET SERVLET avec dopsot( )


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

COMMUNICATION APPLET SERVLET avec dopsot( )

mardi 25 novembre 2008 à 10:02:12 | COMMUNICATION APPLET SERVLET avec dopsot( )

Java2386

bonjour je vien de realisé une communication entre une servlet et une applet (JAPPLET with swing). j'ai fait (en principe ) les etapes necessaire, voici mon code pour mieu comprandre. applet: import java.awt.event.ActionEvent; import java.io.DataOutputStream; import java.io.IOException; /* * ApplClient.java * * Created on 18 novembre 2008, 20:10 */ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.URL; import java.net.URLConnection; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author HEDAHDIA */ public class ApplClient extends javax.swing.JApplet { private Vector result; private static String Servlet = "http://localhost:8080/WEBMAD/Server"; private URL u; /** Initializes the applet ApplClient */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } /** This method is called from within the init() method to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); Groupe3 = new javax.swing.JRadioButton(); okGroupe3 = new javax.swing.JButton(); jComboBox1 = new javax.swing.JComboBox(); Groupe3.setText("30-Groupe3"); okGroupe3.setText("OK"); okGroupe3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { okGroupe3ActionPerformed(evt); } }); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(29, 29, 29) .addComponent(Groupe3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 154, Short.MAX_VALUE) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(78, 78, 78)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(156, 156, 156) .addComponent(okGroupe3) .addContainerGap(197, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(65, 65, 65) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(Groupe3) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 107, Short.MAX_VALUE) .addComponent(okGroupe3) .addGap(82, 82, 82)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); }// //GEN-END:initComponents private void okGroupe3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okGroupe3ActionPerformed URLConnection con = null; try { con = getConnection(); System.out.println("conniction etablié"); } catch (IOException ex) { Logger.getLogger(ApplClient.class.getName()).log(Level.SEVERE, null, ex); } try { Serializable objs = "Sys30"; sendObject(con, objs); } catch (IOException ex) { Logger.getLogger(ApplClient.class.getName()).log(Level.SEVERE, null, ex); } try { System.out.println( (String)receiveObject( con )); result = (Vector) receiveObject( con ); } catch (Exception ex) { Logger.getLogger(ApplClient.class.getName()).log(Level.SEVERE, null, ex); } System.out.println("message recu"); int i = 0; while(result.size()== i){ jComboBox1.addItem((String)result.elementAt(i)); i++; }//GEN-LAST:event_okGroupe3ActionPerformed } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JRadioButton Groupe3; private javax.swing.JComboBox jComboBox1; private javax.swing.JPanel jPanel1; private javax.swing.JButton okGroupe3; // End of variables declaration//GEN-END:variables private URLConnection getConnection() throws IOException { URL u = new URL(Servlet); URLConnection con = u.openConnection() ; con.setDoInput( true ); con.setDoOutput( true ); con.setUseCaches( false ); con.setRequestProperty("Content-type","application/octet-stream"); con.setAllowUserInteraction( false ); return con ; } private void sendObject(URLConnection con ,Serializable objs ) throws IOException { ObjectOutputStream out = new ObjectOutputStream( con.getOutputStream()); // serialiser l'obj . out.writeObject(objs); out.flush(); out.close(); } private Object receiveObject(URLConnection con ) throws Exception { ObjectInputStream in = new ObjectInputStream( con.getInputStream()) ; Object obj = in.readObject(); in.close(); return obj ; } public void actionPerformed(ActionEvent e) { throw new UnsupportedOperationException("Not supported yet."); } } coté servlet: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; import java.util.Vector; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jess.JessException; import jess.Rete; import se.liu.ida.JessTab.JessTabFunctions; import jess.*; /** * * @author HEDAHDIA */ public class ServerWEBMAD extends HttpServlet { private Rete moteur; private Vector result; private String query; /** * Processes requests for both HTTP GET and POST methods. * @param request servlet request * @param response servlet response */ // /** * Handles the HTTP GET method. * @param request servlet request * @param response servlet response */ /** * Handles the HTTP POST method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // initServer(); String Tranche = (String) receiveObject(request); sendObject(response,result); } catch (Exception ex) { Logger.getLogger(ServerWEBMAD.class.getName()).log(Level.SEVERE, null, ex); } } private Object receiveObject( HttpServletRequest req ) throws Exception { ObjectInputStream in = new ObjectInputStream( req.getInputStream() ); Object obj = in.readObject(); in.close(); return obj ; } private void sendObject( HttpServletResponse resp , Object obj ) throws Exception { ObjectOutputStream out = new ObjectOutputStream( resp.getOutputStream() ); out.writeObject( obj ); out.close(); } private void initServer() throws JessException{ moteur = new Rete(); moteur.addUserpackage(new JessTabFunctions()); moteur.executeCommand("(load-project Turb-Onto.pprj)"); } public String getServletInfo() { return "Short description"; } } je suis vriment épuisé a cause de se problem le voila : java.io.EOFException (bottonok3actionperformed())
mardi 25 novembre 2008 à 10:13:34 | Re : COMMUNICATION APPLET SERVLET avec dopsot( )

dvoraky

Réponse acceptée !
Salut,

Je sais pas si le code mis comme ça aide à mieux comprendre...


 C'est après des heures de codage que j'ai compris pourquoi les créateurs de Java ont choisi une tasse de café comme logo...
mercredi 26 novembre 2008 à 06:52:26 | Re : COMMUNICATION APPLET SERVLET avec dopsot( )

Java2386

merci pour le tasse de café: je repete ma question pour la communication entr applet et servlet a base de HTTP.(a base d'objet) mon problem c 'est au niveau de l'applet elle resoi pa la réponse de servlet(malgré que l'objet est serialisé). voici mon code: applet: import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; /* * test1.java * * Created on 20 novembre 2008, 10:42 */ import java.util.Vector; /**HEDAHDIA/ * Suite a la projet WebApplication4. * On utilise cette Applet pour testter la communication avec la servlet on * utlisant les composants SWING * */ public class test2 extends javax.swing.JApplet { private static final String servlet="http://localhost:8080/WEBMAD1.1/Serveur.PipelineServletA"; private URLConnection con; /** Initializes the applet test1 */ public void init() { try { java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { initComponents(); } }); } catch (Exception ex) { ex.printStackTrace(); } } /** This method is called from within the init() method to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jComboBox1 = new javax.swing.JComboBox(); jButton1.setText("envoyer"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(108, 108, 108) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(45, 45, 45) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(84, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(94, 94, 94) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE) .addComponent(jButton1) .addGap(64, 64, 64)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(53, 53, 53) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(42, 42, 42) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(81, 81, 81) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(58, Short.MAX_VALUE)) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); }// //GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed int i; try { URLConnection con1=getServletConnection(); // get input data for sending Vector Tab1 = new Vector(); for (i=1;i < 5;i++){ String input = jTextField1.getText() ; Tab1.addElement(input); } // send data to the servlet sendObject(con1,Tab1); // receive result from servlet Vector Res =new Vector(); Res =(Vector)receiveObject(con1); while(Res.size()== i){ jComboBox1.addItem((String)Res.elementAt(i)); i++; } } catch (Exception ex) { ex.printStackTrace(); } }//GEN-LAST:event_jButton1ActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables /** * Get a connection to the servlet. */ private URLConnection getServletConnection() throws MalformedURLException, IOException { URL urlServlet = new URL(getCodeBase(), servlet); URLConnection con = urlServlet.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty("Content-Type","application/x-java-serialized-object"); return con; } private void sendObject( URLConnection con , Object obj ) throws IOException { ObjectOutputStream out = new ObjectOutputStream( con.getOutputStream() ); if ( obj != null ) { out.writeObject( obj ); } out.close(); } private Object receiveObject( URLConnection con ) throws Exception { ObjectInputStream in = new ObjectInputStream( con.getInputStream()) ; Object obj = in.readObject(); in.close(); return obj ; } } servlet: package Serveur; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author HEDAHDIA */ import java.io.* ; import java.util.Vector; import javax.servlet.http.* ; public class PipelineServletA extends HttpServlet { private Vector Tab1,Res1; private Object receiveObject( HttpServletRequest req ) throws Exception { ObjectInputStream in = new ObjectInputStream( req.getInputStream() ); Object obj = in.readObject(); in.close(); return obj ; } private void sendObject( HttpServletResponse resp , Object obj ) throws Exception { ObjectOutputStream out = new ObjectOutputStream( resp.getOutputStream() ); out.writeObject( obj ); out.close(); } public void doPost( HttpServletRequest req , HttpServletResponse resp ) { int i = 0; try { Object obj = receiveObject( req ) ; // reverse the String. Tab1 = (Vector)obj; while(Tab1.size()== i){ String old = (String) Tab1.elementAt(i); String s =""; for( int j = old.length() - 1 ; j >= 0 ; j-- ) { s += old.charAt( j ); } Res1.add(i, s); i++; } sendObject( resp , Res1 ); } catch ( Exception e ){ System.out.println( "Clang! Thunk: " + e ); } } } et l'erreur qu'il m'affche: java.io.FileNotFoundException: http://localhost:8080/monprojet/maservlet.
mercredi 26 novembre 2008 à 09:02:26 | Re : COMMUNICATION APPLET SERVLET avec dopsot( )

dvoraky

Réponse acceptée !
Bon arrêtes de mettre ton code comme ça c'est illisible et ça donne pas envi de répondre....

As tu bien référencé ta servlet dans ton web.xml???


 C'est après des heures de codage que j'ai compris pourquoi les créateurs de Java ont choisi une tasse de café comme logo...


Cette discussion est classée dans : java, import, javax, swing, grouplayout


Répondre à ce message

Sujets en rapport avec ce message

Problème rafraichissement image avec Swing [ par ejouvin ] Bonjour.Je suis en train de faire une petite application qui contient une arborescence à gauche et une fenêtre de détail sur la droite.Dans le code su jTable/jCheckBox [ par Dellys ] Bonjour Je voudrai s'avoir commet afficher un jCheckBox sur un ma jTable dans une colonnes de type booléen et aussi lui attribuer un listner < PB dans main [ par thanatos67 ] Salut à toi lecteur. Oui toi! J'ai un petit soucis.J'ai déjà eu un premier soucis résolu grace à l'aide de TWINUTS (encore merci). J'ai une classe dan Probleme de requete [ par thanatos67 ] Salut à tous.Je suis en BTS IRIS et pour mon projet je dois créer une base de donnée mysql qui va recevoir des données d'une station météorologique et Un ptit conseil/aide serait le bienvenue (Java graphique) [ par Al3x38 ] Bonjour tout le monde ;)Voila, dans le cadre d'un projet d'informatique, je dois réaliser un petit programme en java avec une interface agéable ! Alor JTable (aider moi SVP) please !! [ par amine1234Z ] Bonsoir voila j'ai ajouter juste le code, j'ai pas pu ajouter la base de donnée est ce que qlq un peut m'aider SVP . - c'est a vous de la créé Lien entre deux classe [ par Bouboukick ] Bonjour, je suis occupé de faire un programme sous NetBeans et j'ai 2 fichier l'un pour mon interface graphique l'autre pour me connecter à mon port c Faire une grille de Jlabel dans une JFrame [ par ABF ] Bonjour, Je voudrais : 0) demander de sélectionner une map 1) charger un fichier texte, le parser en fonction des | et remplir un tableau 2) generer interface [ par gigi00 ] bonjour tout le monde j'ai besoin de votre aide j'ai  un projet de gestion de caisse a realise avec netbeans  , et je trouve un probleme au niveau la probléme avec les interfaces graphiques en java [ par Rody88 ] Bonsoir tout le monde jai un gros brobléme lorsque jessaye de lié mon IGavec mon code aauquel j'ai implémenté les classes bibliotéque,abonne et lesepr


Nos sponsors


Sondage...

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,655 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales