Possible Duplicate:
passing value to JSP via javaScript
i want to pass value of a javascript varible to jsp variable is this possible?
This will require two things:
- Your Javascript to somehow post the variable's value, either normally (e.g. in a HTTP POST field) or with AJAX to the web server.
- The controller needs to fish it out of the request (there are numerous ways of doing that) and supply it to the JSP.
Well, you can't. Javascript is executed on the browser & what comes out there is only HTML. So you can use javascript to set form fields or send them as request variables using AJAX. You can use your jsp scriplets inside of java script which gets parsed on your server & the values can then be used. for ex:
var i = <%=initialValue%>
When the jsp is processed on the server the scriplets are going to be parsed. What comes out to your browser will be
var i = 10; // In case your initialValue was 10 in your jsp.
精彩评论