Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variable from Javascript to JSP

Status
Not open for further replies.

scohan

Programmer
Dec 29, 2000
283
US
I use the onclick event for an input tag in my form. I pass it an integer. I need to set a jsp session variable with the value of this variable. How can I get the javascript integer variable into the jsp code to set the session variable? One doesn't seem to recognoze the other. I have:

<script>
function copy(row)
{
selected_row=row;
<%
session.putValue(&quot;sel_row&quot;, new Integer(selected_row));
if (session.getValue(&quot;sel_row&quot;) != null) {
jsp_sel_row = ((Integer) session.getValue(&quot;sel_row&quot;)).intValue();
}
%>
</script>
.
.
.
<INPUT name=cmd_copy type=image src=&quot;copy.gif&quot; alt=&quot;Copy Row&quot; onclick=&quot;copy(<%=i%>)&quot;>

This doesn't work - jsp_sel_row is still 0. Any ideas? Thanks again.
 
I don't believe client-side JavaScript and server-side JSP's can communicate on the fly in the manner that you suggest. By the time the code is loaded into the browser, all JSP code has been resolved into static values by the server's JVM.

If you want to pass a JavaScript variable to a JSP method, it will require a new HTTP transaction sent via a cookie or form submission. This is simply the nature of the beast.

If you really need on-the-fly communication between your web page and the server, Java applets have this capability.

Petey
 
How do I pass values directly from javascripts to javascripts. Are there any easy way?
 
Scenerio #1:
You are opening a new window with JavaScript, and you want the scripts in both windows to communicate with each other. Open the window thusly:
var win = window.open(URL);
Now you can control the new window's scriptable components in the following manner:
win.document.write(text);
win.document.bgColor = hexcode;
(etc.)
The new window can control the original window's scriptable components in the following manner:
opener.document.write(text);
opener.document.bgColor = hexcode;
(etc.)

Scenerio #2:
You would like to pass values between consecutive pages in the same window. This is called session tracking. Cookies were designed to do this. Cookies store bits of data on the hard disk that can be retrieved by other web pages, as long as those pages came from the same web server. JavaScript can be used to get and set cookies.

Scenerio #3:
You have a frameset, and you want the scripts in two frames to communicate with each other. You can control other frame's scriptable components in the following manner:
parent.frames[x].document.write(text);
parent.frames[x].document.bgColor = hexcode;
(etc.)
where &quot;x&quot; is the number of the frame to be manipulated in order of appearance in the source code starting from zero.

Petey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top