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]
# 1

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.

jsalonen at 2007-6-29 2:27:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thank you for replying but let me know if I can pass a javascript variable to jsp as I want it in my code .
preetith at 2007-6-29 2:27:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

<script>

function func(a, b)

{

var finalvalue;

// perform function

formexample.hiddenexample.value=finalvalue;

}

</script>

Later on the page you have the form part of the html:

<form name="formexample">

<input type="hidden" name="hiddenexample" value="defaultvalue">

</form>

Obviously if there is already a form on the page you add the hidden value to that. As long as the form is submitted you can collect this like any other value.

Breakfast at 2007-6-29 2:27:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

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

NormNewman at 2007-6-29 2:27:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Have you tried request.getParameter("hide1");? Remember, when the form gets submitted to the server all you will have are the form values to work with. The JavaScript structures will all be gone.

Looking at they way you are trying to retrieve the value of hide1, it seems like you are having some problems distinguishing server and client side code. You are trying to use the lstPerson structure on the server side where it doesn't exist.

Sean

tus at 2007-6-29 2:27:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

The request.getParameter("hide1") was the initial try (see 2 and 3 paragraph after resOutP section). I tried the lstPerson structures after that failed.

I should add at this point that I did displays of request.getParameterNames and hide1 was not in the list. This was corrected by defining hide1 as an input field on the prior page and giving it an empty value of "". This now defined hide1 in the parameter list and I am now getting the empty value returned (hide1 = || using my construct not hide1 = |null|). hide1 is now a valid parameter, but it is still not being updated by the javascript function which does update properly in the javascript function.

Any ideas on this status.

NormNewman at 2007-6-29 2:27:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...