> Dear all,
>
> Does anyone know how we can pass a JavaScript variable into JSP?
>
> For example, when I try to following simple JSP file, it will say that
> jscript_data is not found. Does anyone know how I can achieve this?
> Any help will be greatly appreciate it. Thanks.
>
> Francis
>
> <%! int id; %>
> <html>
> <head>
> </head>
>
> <SCRIPT LANGUAGE="JavaScript">
>
> function a() {
> var jscript_data = 1;
>
> <% id = jscript_data; %>
> }
>
> </SCRIPT>
> <BODY>
> </BODY>
> </HTML>
Your code attempts to mix client and server side code with predictable
results. Remember that everything inside the "<% %>" delimiters runs on the
server, which has no knowledge of your JavaScript variables. You need to
pass your JavaScipt values back to the JSP script as a request parameter
(either as GET in the URL or POST from a form input). I would do the
following:
<SCRIPT LANGUAGE="JavaScript">
function a()
{
var jscript_data = 1;
self.location="your_jsp_script.jsp?id=" + jscript_data;
}
and then get the value from your JSP script, which may of course be in the
same file as your JavaScript function.
<%
String strId = request.getParameter("id");
// convert to int if necessary
if (strId != null)
{
// convert to int if necessary
int iId = (new Integer( strId)).intValue();
// use your value......
}
%>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html