• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass java script variable to JSP?

 
san krish
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i pass Java script variable to JSP?
I have a code like this...
<script type="text/javascript">
var editArray = {50,60,70,80};
</script>

Note editArray is an array.
Now i want to pass this to my JSP.
How do i pass this as an hidden field?

<input type="hidden" value="???"
id="sample" />


Also, how to get this hidden field in my controller? because editArray is an array?

please help me out.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34225
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean how to submit the variable's value in a forum so the JSP can read it. (JSPs can't read JavaScript variables.)

You have two choices:
1) Put a string representing the array in the hidden field. (such as a comma separated value format).. If you go this route, you call request.getParameter() and parse it in Java.
2) Store each value of the array in a field like we do with multi select fields. If you go this route, you call request.getParameterValues().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic