Sending arraylist from jsp to servlet on a button click -


this jsp page.

code of jsp page :-

<img src='image.jpg' height=200px width=200px> <form action="buyserv"> <% arraylist al=new arraylist(); al.add("naman"); al.add("gupta"); request.setattribute("allproducts", al); requestdispatcher rd = request.getrequestdispatcher("/buyserv"); rd.forward(request, response);  %> <input type="submit" value="buy"></form>  <a href="showallproducts.jsp"><input type="button" value="continue"></a>  

i want on clicking buy button,the arraylist (al) should passed buyserv(servlet). list getting passed buyserv problem jsp page not getting displayed.but want display jsp page pass arraylist on button click. can tell me how can ?

code of buyserv :-

protected void doget(httpservletrequest request, httpservletresponse response) throws    servletexception, ioexception  { arraylist al=(arraylist)request.getattribute("allproducts"); printwriter out=response.getwriter(); out.print(al.get(0)); } 

you can pass list string[] in following way:

<form action="buyserv"> <input type='text' name='list'  value='a' /> <input type='text' name='list'  value='b' /> <input type='text' name='list'  value='c' /> <input type="submit" value="buy" /> </form>  

you use type='hidden' if don't want values visible on form.

in servlet:

string[] values = request.getparametervalues("list"); 

if need values generated list, use loop:

<form action="buyserv"> <%   string[] array = { "a", "b", "c" };   for(int i=0; i<array.length; i++)   {     out.print("<input type='text' name='list'  value='" + array[i] + "' />");   } %> <input type="submit" value="buy" /> </form>  

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -