*
The moose likes JSP and the fly likes How to pass java script variable to JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass java script variable to JSP?" Watch "How to pass java script variable to JSP?" New topic
Author

How to pass java script variable to JSP?

san krish
Ranch Hand

Joined: Aug 11, 2010
Posts: 41
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30067
    
149

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().


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass java script variable to JSP?
 
Similar Threads
passing javascript array from jsp to servlet
Javascript array to server
JS ---> JSP variables
in jsp i want touse java script variable value
Array lists syntax in jsp