JSP in javascript
Hi,
I would like to know how to pass javascript variable to jsp .
the code wud look like this :
function myfunc(name,value){
<%
session.setAttribute(name,value);
%>
}
this gives a javascript error.
We would like to do this without posting on the same form or using request.getParameter().
Thanx in advance
[408 byte] By [
preetith] at [2007-9-26 1:39:07]

The JSP code is executed on the server and it doesn't exist in the resulting HTML that is passed to the browser. JavaScript code is executed in the browser that knows nothing about JSP.
So code like
function myfunc(name,value){
<% session.setAttribute(name,value); %>
}
will look like
function myfunc(name,value){
}
when it leaves the server. Communication JSP -> JavaScript is easy because JSP can leave script code to the HTML code. The other way round requires contacting the server that runs JSP with eg. a form POST.
You can pass a javascript variable's value to JSP in a hidden field so that the user will never see it.
You were particularly helpful in solving the problem for preetith of getting a parameter value from JavaScript to JSP. I am having a similar problem and cannot resolve the issue. I am hoping that you can provide me with a resolution to the problem.
The JavaScript function is given below. The function does execute and works properly with the alert displaying the proper values for theForms[1], theForms[1].hide1.valueandhidePVal.
<SCRIPT LANGUAGE=JavaScript>
<!--
function getPersonId(theForms)
{
alert( "theForms[1].name = " + theForms[1].name );
var hidePVal = theForms[1].lstPerson.value;
alert( "hidePVal = " + hidePVal);
theForms[1].hide1.value = hidePVal;
alert( "theForms[1].hide1.value = " + theForms[1].hide1.value);
return hidePVal;
}
//-->
</SCRIPT>
The JSP code gathers the HTML in a string parameter for output:
resOutP = "<FORM Name='setPerson' method='POST'>";
resOutP += resOut1 + strDropBox;
resOutP += " <TR><TD>";
resOutP += "<INPUT TYPE=BUTTON NAME='Continue' OnClick='getPersonId(document.forms);' VALUE='Continue'>";
resOutP += "<INPUT TYPE='HIDDEN' NAME='hide1'>";
resOutP += "<A HREF='javascript:getPersonId(document.forms);'></A>";
resOutP += " <TD></TR>";
resOutP += "</FORM Name='setPerson'>";
resOut1 is html text and strDropBox is the html for a Select/Option box with name='lstPerson' that the JavaScript function is capturing. The selected value in the drop box is properly handled by the JavaScript function getPersonId. I want to capture the value obtained in the function getPersonId and bring it into the JSP form parameter hide1 (or any JSP accessible parameter).
I then output the hide1 parameter to the log using the following JSP statement:
log("hide1 = |"+ request.getParameter("hide1") + "|");
The log entry output shows hide1 to be null:hide1 = |null|.
If I create a log statement with a dummy variable, xyz (neither declared nor referenced any where else), and substitute it for hide1, i.e.
log("xyz = |"+ request.getParameter("xyz") + "|");
I get the same null output for xyz. There is no error statement that the parameter does not exist.
I therefore question if the hide1 parameter refered to in the getParameter function is in fact linked to the input parameter that is named hide1. I believe the JavaScript function is executiong for the Continue Button Input but not the Hidden Input. The alerts are only executed one time.
For the log statement I have also tried accessing hide1 using the following formats for the request.getParameter method:
request.getParameter("lstPesron.hide1")
request.getParameter("this.lstPerson.hide1")
request.getParameter("setPerson.lstPerson.hide1")
I have also tried the following INPUT statement for defining the hide1 parameter and get the same null value for hide1.
resOutP += "<INPUT TYPE=HIDDEN Name='hide1' Value='defaultvalue'>";
I would appreciate any assistance you can give me in resolving this issue. My email address is nnewman@multitech.com.
Thank you,
Norm Newman