How to pass java script array to jsp page?

Questions by vinaygundu   answers by vinaygundu

Showing Answers 1 - 2 of 2 Answers

Farook

  • Feb 7th, 2006
 

hi, all.
I just started to learn java script. working on practice project to create shopping cart. The jsp page will getting the information from database, after user key in quantity in text box, it will submit to servlet.
Code:
<input type=text name="<%= newit.getItem() %>Qty" value = "0">

now I wanna use javascript to validate if the input is positive integer, so I change the name to "Qty", and added a hidden field right after this.
Code:
<input type=text name="Qty" value = "0"><input type="hidden" name="<%= newit.getItem() %>Qty" value = "<%=keyinQty%>">
the next step will be pass the array Qty from javascript back to keyinQty and sent to servlet, but I couldn't figure out how to do it.
Could anybody help? or is this a right method to do? anyone has good link that i could find helpful info? thanks!
Code:
<SCRIPT LANGUAGE=JAVASCRIPT> function validate() { var y = new Array(x.length) for ( i=0; i<x.length; i++) { if (parseInt(x[i].value) < 0 ) { alert("Please enter positive number:"); return false; } } return true; }</SCRIPT>

  Was this answer useful?  Yes

lhariPrasad

  • Sep 27th, 2006
 

Hi,

one thing, just remember,  a JSP is nothing but a compiled servlet, once the JSP is compiled into a servlet and sends the response to the client, there is no way to recognize that it is a JSP page at client side. You will get the whole data onto the client. From there if you want to pass any thing (javascript array)back to the JSP(SERVLET), just create a hiddne variable, iterate through the javascript array, put the value as some comma seperated into the hidden variable and submit to the servlet.

There is no way u can reference a JSP variable from your JavaScript once the page is processed and the response send.

Hope this helps.

Regards,

Hari

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions