JavaScriptからJSPへ変数を渡す方法。
I need to pass the variables which are used in javascript to jsp page? Is it possible or not? If yes how? I can verywell do the same process in the opposite direction, that is sending variables from jsp to javascript.
Re: How to pass javascript variable to jsp page?
Author: mshanu Jun 2, 2005 9:59 PM (reply 1 of 5)
u can assign values to ur form fields like document.form1.field.value..i think this will work..pls try and let me know..
regards
shanu
Re: How to pass javascript variable to jsp page?
Author: evnafets Jun 2, 2005 10:03 PM (reply 2 of 5)
The only communication from javascript to java is via request parameters when you click a link/submit a form.
So you can set the javascript variables into form fields (input type="hidden" is popular) and submit the form.
Re: How to pass javascript variable to jsp page?
Author: Nilalini Jun 6, 2005 10:19 PM (reply 3 of 5)
Dear pals
I am asking this for a single .jsp file. I am putting jsp and javascript together in a file. like
var a; // javascript
a=10; //javascript
// jsp starts here in the same program
<%
int b;
//here i need to assign a to b.
%>
Re: How to pass javascript variable to jsp page?
Author: mshanu Jun 6, 2005 10:32 PM (reply 4 of 5)
I think u can do that..cuz java script is in the client side and scriplets supposed to run in the server side.u have to append the value in the java script to the request and get in the sriplet...
regards
shanu
Re: How to pass javascript variable to jsp page?
Author: Virendra Jun 6, 2005 10:46 PM (reply 5 of 5)
Dear
To pass variable value from JavaScript to JSP
to do so u have to use <form> tag.
to pass data from javascript to JSP u have to submit form and
u should get data like request.getParameter("varName");
this method will return all the data as string.
If you want script variable data in same JSP page then
u have to write form tag like <form name='form1' action='yourJspPage.jsp' >
-----------
To get JSP variables data in javaScript
it is so easy.
This is one sample example like under for your both questation.
file name : test.jsp
<@ page inport="...." %>
<%
String FirstName="";
String caption="Go";
FirstName = request.getParameter("firstname");
%>
<html>
<head>
<script language="javascript" >
function checkAll()
{
if(document.form1.firstname.value=="")
{
alert("Invalid text");
return false;
}
return true;
}
function setJSPvalueToScript()
{
var testVar;
testVar='<%=caption%>';
alert(testVar);
// here value is set when page in loaded not function is called
// you can see by viewing source of this file
}
</script>
</script>
</head>
<body>
<form name="form1" action="test.jsp" onSubmit="return checkAll()" >
<input type="text" name="firstname" value="<%=FirstName%>" >
<input type="submit" value="<%=caption%>" >
<input type="button" onClick="setJSPvalueToScript()" >
</form>
</body>
</html>