Today, someone asked me how to communicate between Applet and servlet, dumbful, did not pay attention before, I didn't study Applet when I learned Java, I only knew that applet was running on the Client side, and servlet was in Servetr. When running, if the two want to communicate, you must let the applets to build to the Server connection, and then request the corresponding servlet. The main thinking is this, but how to do it, you can have several ways.
First, connect directly to Socket
Because this semester is learning Computer Networking, I want to build a network connection. It is Socket, of course, this kind of hair is in theory, but I have to complete HTTP interactions with Server, there will be some The problem, such as safe, and to consider whether the write server is not fully implemented, since the application's perspective, this method is not very feasible. But this method also has its advantages, it is a little one The Socket connection can be used to recycle, and the general HTTP1.0 will support this technology.
Second, use HTTP text flow.
Java's URL and URLConnection types make it easy to read data from a URL, you don't have to worry about Socket and other common complex issues related to network work. What we need is just a component of a servet, which should be able to issue information through the URL. The code example is, for example, the implementation in the applet: public void function () throws ioException {url url = new url (getcodebase (), "/ servlet / myservlet"); urlConNetion Con = URL.OpenConnection (); conountusecaches (False) ); InputStream in = con.getInputStream (); DataInputStream textStream = new DataInputStream (in); String line1 = textStream.readLine (): String line2 = textStream.readLine (); // do something about the Strings}
Of course, the Applet read two String is made past the Servlet, Servlet code is as follows: public void doGet (HttpServletRequest request, HttpServletResponse reponse) throws IOexception, ServletException {response.setContentType ( "text / plain"); PrintWriter out = Response.Getwriter (); out.println ('' this the first line. '); out.println (' 'this the second line.');}
This method can cope with some simple situations, and its limit is to convert all data to be transmitted to string. The following method is more suitable for object-oriented requirements.
Third, use the HTTP object stream.
It is the binary code that directly transmits the object directly. The technology to use is also the Java's serializable. This method and method are similar, just encapsulate the inputStream to the ObjectInputStream to go .applet end code: public void function () { URL url = new URL (getCodeBase (), "/ servlet / myServlet"); URLConnetion con = url.openConnection (); con.setUseCaches (false); InputStream in = con.getInputStream (); ObjectInputStream objStream = new ObjectInputStream (in ); MyObject mo = (MyObject) obj.readObject ();} Servlet code is public void doGet (HttpServletRequest request, HttpServletResponse reponse) throws IOexception, ServletException {OutputStream out; ObjectOutputStream objStream; out = res.getOutputStream (); objStream = New ObjectOutputStream (OUT); MyObject Mo = New MyObject (); ObjStream.writeObject (Mo);} PS: Reference: http://www.linuxaid.com.cn/Articles/5/4/54051516.SHTML