COMMUNICATION APPLET SERVLET avec dopsot( )

Résolu
Java2386 Messages postés 27 Date d'inscription lundi 17 septembre 2007 Statut Membre Dernière intervention 10 avril 2009 - 25 nov. 2008 à 10:02
dvoraky Messages postés 744 Date d'inscription dimanche 1 avril 2007 Statut Membre Dernière intervention 9 mai 2010 - 26 nov. 2008 à 09:02
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//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)
);
}// </editor-fold>//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
*/

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* 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())




</editor-fold>

3 réponses

dvoraky Messages postés 744 Date d'inscription dimanche 1 avril 2007 Statut Membre Dernière intervention 9 mai 2010 8
25 nov. 2008 à 10:13
Salut,

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

<hr width="100%" size="2" />
 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...
3
dvoraky Messages postés 744 Date d'inscription dimanche 1 avril 2007 Statut Membre Dernière intervention 9 mai 2010 8
26 nov. 2008 à 09:02
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???

<hr width="100%" size="2" />
 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...
3
Java2386 Messages postés 27 Date d'inscription lundi 17 septembre 2007 Statut Membre Dernière intervention 10 avril 2009
26 nov. 2008 à 06:52
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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//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)
);
}// </editor-fold>//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.
0
Rejoignez-nous