1

I have to pass a List from a .jsp into my servlet and I want to do it through a URL. Can I do something like -

<a href="SellSelectedStockServlet?value=content" target="_self">

where 'content' is a List. I want to get the elements of this list in my servlet.

Or I can only pass the individual parameters with a separation of '&'?

Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57
  • 1
    Having list elements in url is not good approach. Like JB Nizet said, the length of a query string is limited. You said that you need to pass a List from a .jsp into your servlet. Use ModelAttributes for transferring objects between jsp and servlets, which is more clear. Why do you prefer having it in url? – Erik Kaju Sep 26 '12 at 12:11

2 Answers2

14

You need one parameter per element of the list. And all these parameters should have the same name:

SellSelectedStockServlet?values=elem1&values=elem2&values=elem3

In the servlet, you'll get all your list elements like this:

String[] values = request.getParameterValues("values");
// contains elem1, elem2 and elem3.

Beware: the length of a query string is limited. Don't pass a large number of values this way.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hey everytime I pass evean a single value through this proceedure my eclipse crashes and in order to run tom cat again I have to change the port numbers, any fix? what should I do? - Thanks –  Feb 16 '14 at 11:41
-1

To get the object to the other JSP is to add it to the HttpServletRequest objects attribute field using a scriplet:

JSP with the List:

 <%
 request.setAttribute("theList", ListObject);
 %>

The other JSP:

<%
List myList = (List) request.getAttribute("theList"); 
%>
Raj Adroit
  • 3,828
  • 5
  • 31
  • 44
  • 2
    Wrong. Each HTTP request gives you (obviously) a diffferent `request` instance. Learn here how it works: http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading/3106909#3106909 – BalusC Sep 26 '12 at 12:01