Applet with Http Tunnel Servlet technology and communications (source code) Author: jdeveloper
Applet Source
Import java.io. *; import java.applet.applet; import java.aw. *;
public class TestDataStreamApplet extends Applet {String response; public void init () {try {java.net.URL url = new java.net.URL (getDocumentBase (), "servlet / DataStreamEcho"); java.net.URLConnection con = url .openConnection (); con.setUseCaches (true); con.setDoOutput (true); con.setDoInput (true); ByteArrayOutputStream byteout = new ByteArrayOutputStream (); DataOutputStream out = new DataOutputStream (byteout); out.writeUTF ( "hello world ! "); Out.flush (); byte buf [] = Byteout.TobyteArray (); con.setRequestProperty (" Content-Type "," Application / Octest-Stream "); Con.SetRequestProperty (" Content-Length ", " buf.length); DataOutputStream Data = New DataOutputStream ()); DataOut.write (); DataOut.close (); DataOut.close ();
DataInputStream in = New DataInputStream (); response = in.readutf (); system.out.println ("Read from Server:" response); in .close ();} catch (exception e)} E.PrintStackTrace ();}}
Public void Paint (Graphics G) {g.drawstring ("Read from Server:" Response, 10, 20);}}
Servlet Source
Import javax.servlet. *; import javax.servlet.http. *; import java.io. *;
public class DataStreamEcho extends HttpServlet {public void service (HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException {DataInputStream in = new DataInputStream (req.getInputStream ()); resp.setContentType ( "application / octest-stream") ; ByteArrayOutputStream byteout = new ByteArrayOutputStream (); DataOutputStream out = new DataOutputStream (byteout); String response = in.readUTF (); out.writeUTF (response); out.flush (); byte buf [] = byteout.toByteArray () Resp.setContentLength; ServletOutputStream servletout = Resp.getOutputStream (); servletout.write (buf); servletout.close ();}}
--------------------------------------------
Easy applet and servlet communication (fragment source) Join time 2004-3-1 2:01:35 This site domain name www.code365.com Browse statistics Total: 63 | Year: 63 | Month: 16 | DAY: 1
Applet and Servlet communication (source code) Author: jdeveloper
Applet Source (Partly) ...
showStatus (textField1.getText ()); URL servlet = new URL (this.getDocumentBase (), "servlet / GetParameterServlet"); URLConnection connection = servlet.openConnection (); connection.setUseCaches (false); connection.setDoOutput (true) ; ByteArrayOutputStream byteStream = new ByteArrayOutputStream (1024); PrintWriter out = new PrintWriter (byteStream, true); String postData = "Name =" URLEncoder.encode (textField1.getText ()); out.print (postData); out.flush (); String lengthString = String.valueOf (byteStream.size ()); connection.setRequestProperty ( "Content-Length", lengthString); connection.setRequestProperty ( "Content-Type", "application / x-www-form-urlencoded "); byteStream.writeTo (connection.getOutputStream ()); BufferedReader br = new BufferedReader (new InputStreamReader (connection.getInputStream ())); textArea1.setText (" "); String s; for (; (s = br. Readline ())! = null && s.length ()> 0; Textarea1.AppendText (s "/ n")); ... servlet Source
// getParameterServletImport java.io. *; Import javax.servlet. *; Import javax.servlet.http. *; Import java.util. *;
public class GetParameterServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / html"); PrintWriter out = response.getWriter (); String title = "Reading Request Parameters"; String name = Request.getParameter ("name"); out.println (title); Out.println ("Hello:" name);}