PDA

View Full Version : [java]Comunicazione servlet-applet


sukoy27k
06-12-2005, 17:08
Ecco il metodo che invoco per sendare e ricevere dall'applet:

private void Send(String args[]){
try {
System.out.print("invio in corso");
URL a = new URL("http://127.0.0.1:8080/UserCheck");
HttpURLConnection connection = (HttpURLConnection) a.openConnection();
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
connection.setRequestMethod("POST");
connection.setDoOutput(true);

PrintWriter out = new PrintWriter(connection.getOutputStream());

ObjectInputStream is = new ObjectInputStream (connection.getInputStream());

String param = "name="+URLEncoder.encode(UserNameTextField.getText(), "UTF-8");
out.println(param);
Object ret=is.readObject();
out.close();
is.close();
UserNameTextField.setText((String)ret);
}catch(IOException e){
System.err.println("Errore nell'invio-ricezione dati con post");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

la servlet a cui deve connettersi l'ho testata attraverso un form html e risponde bene,però se al posto del browser cerco di inoltrare la richiesta dall'applet per l'appunto non succede nulla.
Sono alle prime armi e non avendo la possibilità di debbugare nulla,non so esattamente dove si perde la connessione.
ecco il codice del metodo dopost overrides:


protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
String name=(String)request.getParameter("name");
//String password=(String)request.getParameter("password");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0); // Dice di scadere il January 1, 1970, 00:00:00 GMT
response.setHeader("Pragma","No-cache");

String s=name+"OK";
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(s);
oos.flush();
oos.close();


per quanto ne sò ho castato Object a string in quanto entrambi serializzabili (insieme a vector,giusto? ).
non ho postato il resto del codice,in quanto è solo roba di interfaccia e conessioni jdbc.

il servlet engine che utilizzo e ovviamente tomcat,il server http è apache.quando inoltro una richiesta alla servlet via html,è perfettamente in grado di trovare il .class all'interno di webbapps-root-web-inf-classes,per cui ho pensato che l'url alla servlet corretto fosse http://127.0.0.1:8080/UserCheck in quanto nella form html il path relativo che assegno è /UserCheck equivalente al primo assoluto.

bho non sò,che mi dite?

sukoy27k
06-12-2005, 17:13
ho appena testao il path assoluto http://127.0.0.1:8080/UserCheck inserendolo nella forma html e funge per bene,quindi a questo punto sono certo che apche è in grado di raccogliere la richiesta dell'applet,rendirizzandola alla servlet,quindi il problema è da cercare nella comunicazione.

sukoy27k
06-12-2005, 20:46
mettiamola così avevo capito che il problema era troppo complesso per cercare risposte altrove mi sono fatto un bel refactoring (termine molto da programmatore :D consumato)di tutto inserendo una quantità di debug spaventosa.
posto il codice in aiuto ai posteri che come me sbatteranno la testa,non avendo una cazzo di shell a disposizione per debugare applet e servlet.

applet

private void Send(String args[]){
panel.add(prova);
try {
URL a=null;
try {
a = new URL("http://127.0.0.1:8080/UserCheck");
} catch (MalformedURLException e1) {
System.err.println("Errore aperura url");
e1.printStackTrace();
}
HttpURLConnection connection=null;
try {
connection = (HttpURLConnection) a.openConnection();
} catch (IOException e1) {
System.err.println("Errore aperura connessione");
e1.printStackTrace();
}
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
connection.setRequestMethod("POST");
connection.setDoOutput(true);

ObjectOutputStream out=null;
try{
out =new ObjectOutputStream(new BufferedOutputStream(connection.getOutputStream()));
out.writeObject(UserNameTextField.getText());
out.flush();
}catch(IOException e){
System.err.println("Errore invio dati");
}

prova.setText("oggetto inviato");
panel.updateUI();
System.out.println("invio in corso");

ObjectInputStream in=null;
try{
in =new ObjectInputStream(new BufferedInputStream(connection.getInputStream()));
UserNameTextField.setText((String)in.readObject());
}catch(IOException e){
System.err.println("Errore ricezione dati");
}

prova.setText("oggetto ricevuto:"+UserNameTextField.getText());
panel.updateUI();
System.out.println(UserNameTextField.getText());

try {
out.close();
} catch (IOException e) {
System.err.println("Impossibile chiudere outputstream");
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
System.err.println("Impossibile chiudere inputstream");
e.printStackTrace();
}

}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

dove prova è un Jtextfield che aggiorno ogni volta per aver uno step by step.

applet

protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

//String password=(String)request.getParameter("password");
response.setContentType("application/x-java-serialized-object");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0); // Dice di scadere il January 1, 1970, 00:00:00 GMT
response.setHeader("Pragma","No-cache");

ObjectInputStream in=null;
try{
in=new ObjectInputStream(new BufferedInputStream(request.getInputStream()));
}catch(IOException e){
System.err.println("Impossibile aprire inputstream");
}

String name=null;
try{
name=(String)in.readObject();
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

ObjectOutputStream out=null;
try{
out=new ObjectOutputStream(new BufferedOutputStream(response.getOutputStream()));
}catch(IOException e){
System.err.println("Impossibile aprire outputstream");
}

String s=name+"OK";
out.writeObject(s);
out.flush();

in.close();
out.close();


il mio dubbio resta,dovendo per forza di cose,far partire l'applet nella cartella webapps (e non sotto eclipse) e altretanto la servlet,dove cavolo la piglio la shell per leggere lo stderr?

pinok
07-12-2005, 12:44
Abbi pazienza, ci sto capendo poco perché sono "flash" di codice...
Per l'applet, gli errori li leggi sulla "java console" del browser o del plugin.
Per la servlet, trattandosi di Tomcat, dovresti trovarli in Catalina.out

Per il motivo per cui non ti funziona, la pagina HTML dove c'è l'applet, la carichi tramite il web server? Qual'è l'url da cui carichi la pagina? E' sulla stessa porta 8080?