Entry
How to send data from servlet to javascript variables
Mar 28th, 2008 18:01
ha mo, Joseph Ess, HARI K, www.javasoft.com, Java Servlet Programming (Hunter and Crawford 1998)
Javascript can access public methods on embedded applets, so my script
invokes a method on an applet which uses the java.net.URL class to
contact the servlet (see the Java API for details on URL and
URLConnection, exception handling code has been ommitted here):
url = new URL(<servlet URL>);
conn = url.openConnection();
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.writeBytes(args);
out.flush();
out.close();
Now, the applet reads in the reply from the servlet and returns the
reply text to the Javascript caller for processing.
InputStream is = conn.getInputStream();
DataInputStream in = new DataInputStream(new BufferedInputStream(is));
String str = in.readLine();
in.close();
return str;
One caveat: when the returned String pops back up in your Javascript
code, it will be a Java, not a Javascript string. To parse using
Javascript, you will need to create a Javascript string:
str = new String(anApplet.getAppletInfo())
http://www.tantofa.com
http://www.fantofa.com
http://www.mantofa.com
http://www.tanpola.com
http://www.tampola.com
http://www.yamot.com
http://www.mozmar.com
http://www.templatestemp.com