Click to See Complete Forum and Search --> : JavaScript interacting with a Java Servlet


jovialjonny
05-30-2003, 11:02 AM
Hi all,
I have been going round in circles with this problem:
I have a Java Servlet that is holding a string variable ( a URL taken from a form).

What I need is to get the servlet to EITHER open a page called 'multiviewer.html' and pass this variable into one of that pages JavaScript methods (addToViewer(theUrl)).

OR if that page is already open, the servlet should just call the addToViewer method and give it the new url.

Does anyone know how can I achieve this, using the response object of the servlet for example.

I know this isnt strictly a javascript question but Im really lost n any advice would be appreciated :)

khalidali63
05-30-2003, 11:09 AM
well a not so elegant solution,thou it will work,
is to use applet.
you need to communicat with the java applet from the servlet and then from the applet you can do exactly what you want to call js methods or pass values back to applet...

jovialjonny
05-30-2003, 11:41 AM
Ya I heard about that as an idea but I think that might be IE specific and I was hoping to find a more generic solution.

I haven't heard of any elegant way to tackle this as yet, I was thinking about using the servlet and the html page in a kind of parent/child relationship but its all a bit messy that way too, plus it causes a lot of referencing problems in the JavaScript file.

khalidali63
05-30-2003, 11:55 AM
Originally posted by jovialjonny
I think that might be IE specific and

On the contrary use of applet will work with most browsers I know for sure that the above solutionwill work with IE6+ and NS6+ browsers.

As for servlet/html paretn child relationship is concerned.
You can not do that,
Servlet runs on the server side and html runs on the client side,HUGE difference,no direct link,no direct communication other then http request/response bridge.

There are solutions where you can pass the variable value as a string litteral using the URL but calling a js function is really out of question with servlet.
There are some new classes and packages I have not worked with,might be of some help.
Go to
http://devedge.netscape.net
in the JS1.5 reference you will see a section for LiveConnect,it describes some details about it.

That "might" have some solution,other then I still believe your best bet for thsi will be using java aplet/JavaScript communication

luigiza
12-06-2007, 05:37 AM
KHALIDALI63 wrote: '...but calling a js function is really out of question with servlet...'
Not true ! For example put these instructions at the end of the doGet or doPost of the servlet's method (note: res is the servlet's response):

PrintWriter out2 = res.getWriter();
out2.println( "<FONT COLOR=red SIZE=1>" );
out2.println( "my_text" );
out2.println( "<BR><B><CENTER>more_text</CENTER></B><HR>");
out2.close();

Another example:

PrintWriter out2 = res.getWriter();
out2.println( "<SCRIPT LANGUAGE=javascript>" );
out2.println( "location.href='my_url' " );
out2.println( "</SCRIPT>");
out2.close();

It works.