-4
**=========================== MY JAVA FUNCTION RETURNS AN ARRAYLIST============  
   public ArrayList<Class1> getDetails(String id, String year) {
            ArrayList<Class1> arraylist1 = new ArrayList<Class1>();
           return arraylist1;
    }
============================== SERVLET CODE =================================    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            try{
                String operation=request.getParameter("operation");
                log.debug("Operation : "+operation);
                Class1 obj1 = new Class1();
                response.setContentType("text/plain");
                PrintWriter out = response.getWriter();
                if(operation.equals("getDetails")){
                    ArrayList<Class1> record1 = new ArrayList<Class1>();
                    String id = request.getParameter("id_code");
                    String year = request.getParameter("fin_yr");
                    if(id != null) {

                        record1 = obj.geDetails(id, year);
                    } 
                        out.print(record1);
                }
            }  catch(Exception e){ 
                log.error("Exception : "+ e.toString());
            }
        }
======================JSP CODE=====================================
if($('idCode').val() != ""){
            $('#IdCode').focus(function(){
                var fYear = $('#txtYear :selected').attr('label');

                htmlObj = $.ajax({
                    type: "GET",
                    url: "Servlet1",
                    data: "operation=getDetails&id_code="+  $('#IdCode').val() + "&fin_yr="+ fYear,
                    async: false,
                    contentType:"text/html; charset=utf-8",
                    dataType:"html",
                    success: function(result){

                        }
                    }
                    });
            }); 
        }** 

In this above code i added dummy function that will return an arrayList after servlet calls that function. Now my question is how do i get arraylist into may jsp page. I got arraylist properly upto servlet i have no idea how do i get it into my jsp page and designs controls as per the size of servlet returned by sevlet.

spuri
  • 3
  • 3
  • **urgent help is required** ... I had 2 options in my mind **first** one is, i call directly java method from my scriptlet, but for that i am facing an issue that how would i pass my text field values as parameter to my java function. second thing i get an arraylist into servlet(the way i did in sample code) but i do not know how do i pass that arraylist to my jsp – spuri Apr 26 '17 at 11:17
  • 1
    re "Urgent/ASAP" (source: [link](http://stackoverflow.com/questions/32140476/need-to-store-a-2d-list-but-not-using-array#comment52170418_32140476)): "You would be wise not to state this in your posts. Even if it is urgent to you, realize that it is not urgent to us. Many here take offense to this as it implies to them that a) the poster thinks that his post is more important than everyone else's (and it isn't since **all** questions here are equally important), and b) that the poster wants to put pressure on the volunteers who come here to help on their own free time." – Pshemo Apr 26 '17 at 11:31
  • Sorry....I didn't that on that point. – spuri Apr 27 '17 at 04:44

1 Answers1

0

You did not add elements to your list. So even if you iterate over the list, there will be no elements inside. Basically you can iterate over the list on the jsp using java code between these: <% ... %> But this is not best practice.

You can include your list to the response:

request.setAttribute("list", categoryList);

And at the jsp you can get it, and iterate over it:

    <%  
// retrieve your list from the request, with casting 
ArrayList<Category> list = (ArrayList<Category>) request.getAttribute("list");

// print the information about every category of the list
for(Category category : list) {
    out.println(category.getId());
    out.println(category.getName());
    out.println(category.getMainCategoryId());
}
%>

Please look at this answer: Passing ArrayList from servlet to JSP

Community
  • 1
  • 1
Stimpson Cat
  • 1,444
  • 19
  • 44
  • Thannks for reply. I am just taking an example that if my java function is returning an arraylist, how would that is accessible to my jsp page. – spuri Apr 27 '17 at 04:43
  • in servlet's logs its printing outpiut correct but it won't b carrying forward to jsp , their its giving null – spuri Apr 27 '17 at 08:43