1

I want to send an ArrayList from jsp to Servlet and retrive that ArrayList in Servlet. I am using form action=myServlet and sending ArrayList as an hidden parameter. But it is not working.

How to send arrayList from JSP to Servlet. Is it possiple

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
user2500442
  • 35
  • 1
  • 2
  • 7
  • How are you using arrayList as hidden parameter? can you show code? You need to pass a request attribute, not the parameter. Parameters can only be strings – Prasad Kharkar Aug 01 '13 at 08:24
  • Thank You Prasad Kharkar. Will you please tell me how to pass request attribute from jsp to Servlet. – user2500442 Aug 01 '13 at 09:02
  • I need. I am showing my reports in html doc. then on that same page i am giving another button pdf. on clicking that button a servlet is called which has code to show report in pdf format. the data to display should be passed to srvlet. – user2500442 Aug 01 '13 at 09:13
  • If you need to send those data to servlet for PDF report, then better set that arraylist into session and use that session in that servlet.Why you are sending the data from servlet to jsp then from jsp to servlet. – JDGuide Aug 01 '13 at 10:19

1 Answers1

1

How to send arrayList from JSP to Servlet. Is it possiple

Yes definitely it is possible, you need to

  • Populate the arrayList object in the request object.
  • Retrieve the list in servlet.

As you have not provided information whether you are using scriptlets or jstl, I'm assuming the scriptlets.

In your JSP,

 request.setAttribute("arrayList", yourListObject);
 RequestDispatcher rd = request.getRequestDispatcher("servletUrlPattern");
 rd.forward(request, response);

In your servlet, you can access the list using

 List yourList = (List)request.getAttribute("arrayList");

To call the servlet from your JSP file,

 <form action = "yourServletUrl" method = "POST">
    //everything in the form here.
     <input type = "submit" name = "submit">
 </form>

After pressing submit button, you will be able to get your jsp data into it.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56