开发者

Need to pass a value from JavaScript to a servlet page

开发者 https://www.devze.com 2023-03-24 01:38 出处:网络
How do I pa开发者_开发知识库ss a value from JavaScript code to a servlet page?As per my understanding , Create a hidden input field , set the value and submit to the action and get the hidden field va

How do I pa开发者_开发知识库ss a value from JavaScript code to a servlet page?


As per my understanding , Create a hidden input field , set the value and submit to the action and get the hidden field value from servlet.

This shows a kick-off example , you can modify it according to your needs : HTML Form :

<form name="myForm">  
<input type=hidden name="hiddenValue"/>  
<input type="submit" value="Submit" name="buttonSubmit" onclick="customSubmit(10)"/>  
</form>  

Script :

<script>  
 function customSubmit(someValue){  
 document.form1.hiddenValue.value = someValue;   
 document.form1.submit();  
 }  
</script>  

You would user Request.getParameter to get this value from servlet .


With a field as follows:

<INPUT TYPE="HIDDEN" NAME="variableName" VaLUE="">

Then in Javascript you can do:

document.form.variableName.value="value";
form.submit();

and the JSP can evaluate the param "variableName". Remember though, that a Parameter is always a String (or an array of strings if accessed through a different method) so you will need a way to transform this string to what you want, if you want something other than a String (e.g. an Integer).

Bt to tell you the truth I am not sure what you by a null value in javascript. Don't forget that anything that the JSP reads from the params, must then turn around and insert the value back into the hidden field to be read by the javascript on the client (or actually modify the javascript in the JSP before returning the html to the client).

0

精彩评论

暂无评论...
验证码 换一张
取 消