Reputation: 21
This is my 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 that on clicking Buy button,the arraylist (al) should be passed to buyserv(servlet). However the list is getting passed to buyserv but the problem is that the jsp page is not getting displayed.But I want to display the jsp page as well as pass the arraylist on button click. Can anyone tell me how can I do so ?
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));
}
Upvotes: 1
Views: 4567
Reputation: 8659
You can pass a list as String[] in the 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 could also use type='hidden'
if you don't want the values visible on the form.
In the servlet:
String[] values = request.getParameterValues("list");
If you need the values to be generated from a list, use a 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>
Upvotes: 1