http://www.frank-buss.de/echoservlet/index.html
ho trovato questo codice interessante ...
copiando lo zip nella root di tomcat , riesco a farlo funzionare , solo che devo riuscirci a lavorare , su eclipse o netbeans ...
Solo che a creare una Applet e una servlet ... la Servlet non viene compilata ...
Se compilo separatamente la Servlet mi da questo risultatocodice:import java.io.*; import javax.servlet.ServletException; import javax.servlet.http.*; /** * Simple demonstration for an Applet <-> Servlet communication. */ public class EchoServlet extends HttpServlet { /** * Get a String-object from the applet and send it back. */ public void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { response.setContentType("application/x-java-serialized-object"); // read a String-object from applet // instead of a String-object, you can transmit any object, which // is known to the servlet and to the applet InputStream in = request.getInputStream(); ObjectInputStream inputFromApplet = new ObjectInputStream(in); String echo = (String) inputFromApplet.readObject(); // echo it to the applet OutputStream outstr = response.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(outstr); oos.writeObject(echo); oos.flush(); oos.close(); } catch (Exception e) { e.printStackTrace(); } } } --------- La parte interessante di EchoServerlet /** * Get a connection to the servlet. */ private URLConnection getServletConnection() throws MalformedURLException, IOException { // Connection zum Servlet öffnen URL urlServlet = new URL(getCodeBase(), "echo"); URLConnection con = urlServlet.openConnection(); // konfigurieren con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setRequestProperty( "Content-Type", "application/x-java-serialized-object"); // und zurückliefern return con; } /** * Send the inputField data to the servlet and show the result in the outputField. */ private void onSendData() { try { // get input data for sending String input = inputField.getText(); // send data to the servlet URLConnection con = getServletConnection(); OutputStream outstream = con.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(outstream); oos.writeObject(input); oos.flush(); oos.close(); // receive result from servlet InputStream instr = con.getInputStream(); ObjectInputStream inputFromServlet = new ObjectInputStream(instr); String result = (String) inputFromServlet.readObject(); inputFromServlet.close(); instr.close(); // show result outputField.setText(result); } catch (Exception ex) { ex.printStackTrace(); exceptionArea.setText(ex.toString()); } } }
e quando lancio la applet , la carica anche , però quando faccio per inviare la stringa con echoApplet , mi da questa eccezionecodice:messageHTTP method GET is not supported by this URL descriptionThe specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).
codice:java.net.UnknownServiceException: protocol doesn't support output at java.net.URLConnection.getOutputStream(URLConnection.java:785) at EchoApplet.onSendData(EchoApplet.java:105) at EchoApplet.access$000(EchoApplet.java:10) at EchoApplet$1.actionPerformed(EchoApplet.java:46) at java.awt.Button.processActionEvent(Button.java:388) at java.awt.Button.processEvent(Button.java:356) at java.awt.Component.dispatchEventImpl(Component.java:4050) at java.awt.Component.dispatchEvent(Component.java:3885) at java.awt.EventQueue.dispatchEvent(EventQueue.java:463) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:176) at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Qualcuno ha qualche idea ???
grazie !![]()

Rispondi quotando