Since JavaScript, in most cases, runs on the client (Request), you cannot
extract a string generated on the client with a servlet (Response) at the
same time. However, you can extract a string and pass it to your servlet via
a url or post methods, or you can generate a value using your servlet and
pass it to the client JavaScript routine(s). There are some other cool
alternatives, too, like using hidden frames in your HTML that dynamically
load servlets, generate dynamic JavaScript data based on user events, and
then write that data to the parent frame ...
--Bill
*** Simple JS Example : ***
~~~~~~~~~~~~~~~~~~~~ HTML ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JS to Servlet</title>
</head>
<body>
<script language="javascript" type="text/javascript">
<!--
function invokeMyServlet( aString ){
location.href = 'servlet/Servlet2JavaScript?param1=' + escape(
aString );
}
//-->
</script>
<form>
Enter some stuff: <input type="text" name="stuff"> <input
type="button" value="Click Me" onclick="invokeMyServlet(
this.form.stuff.value )">
</form>
</body>
</html>
~~~~~~~~~~~~~~~~~~~~~~~~~~ Servlet ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet2JavaScript extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse
response )
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String valueToPass = request.getParameter( "param1" );
out.println( "<html>" );
out.println( "<head>" );
out.println( "<title>foolet</title>" );
out.println( "<script>" );
out.println( "var valueFromServlet='" + valueToPass.toUpperCase()
+ "!'" );
out.println( "alert( 'Value passed from servlet: ' +
valueFromServlet )" );
out.println( "</script>" );
out.println( "<body>" );
out.println( "</head>" );
out.println( "</body>" );
out.println( "</html>" );
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-----Original Message-----
From: Velmurugan, Vivek [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 24, 2000 9:29 AM
To: [EMAIL PROTECTED]
Subject: How servlet can Interact with Javascript?
Could any one give an idea of how a Servlet can get a String from Javascript
String object....
vivek.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html