faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

68 of 104 people (65%) answered Yes
Recently 3 of 10 people (30%) answered Yes

Entry

How to send data from servlet to javascript variables

Nov 12th, 2008 23:13
hindusthan jos, 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
http://www.hindusthanjobs.com