Passing ArrayList from servlet to JSP

passing arraylist from servlet to jsp using jstl
how to pass object from servlet to jsp
how to pass values from java class to jsp page
how to display arraylist in jsp
request.setattribute in jsp
how to send data from servlet to jsp
how to send arraylist from servlet to jsp using ajax
how to get session attribute in jsp from servlet

I am trying to pass ArrayList which contains object from servlet to JSP. But

Servlet file:

request.setAttribute("servletName", categoryList); //categorylist is an arraylist      contains object of class category  

JSP file:

//category class    
<% Category category = new Category();
//creating arraylist object of type category class
ArrayList<Category> list = ArrayList<Category>();
//storing passed value from jsp
list = request.getAttribute("servletName");

for(int i = 0; i < list.size(); i++) {

category = list.get(i);

out.println( category.getId());


out.println(category.getMainCategoryId() );

In the servlet code, with the instruction request.setAttribute("servletName", categoryList), you save your list in the request object, and use the name "servletName" for refering it. By the way, using then name "servletName" for a list is quite confusing, maybe it's better call it "list" or something similar: request.setAttribute("list", categoryList) Anyway, suppose you don't change your serlvet code, and store the list using the name "servletName". When you arrive to your JSP, it's necessary to retrieve the list from the request, and for that you just need the request.getAttribute(...) method.

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

// print the information about every category of the list
for(Category category : list) {

getAttribute() - Passing data from Server to JSP, How can we pass list values from JSP to servlet? I'm passing an arraylist from servlet by setting it in the attribute and forwarding it to the jsp Servlet: ArrayList <String> mylist = new ArrayList <String

request.getAttribute("servletName") method will return Object that you need to cast to ArrayList

ArrayList<Category> list =new ArrayList<Category>();
//storing passed value from jsp
list = (ArrayList<Category>)request.getAttribute("servletName");

Can we pass values from servlet to HTML without using JSP?, will display the data retrieved, in a tabular form. how to generate a bar chart on a JSP PAGE using the arraylist object passed form the servlet.(using jfreechart) How to update table column from the values of Arraylist in java How to pass variable from one class to another in java


        ArrayList<Actor> list = new ArrayList<Actor>();
        list = (ArrayList<Actor>) request.getAttribute("actors");
    <link rel="stylesheet" type="text/css" href="style.css">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


    <h2>This is Actor Class</h2>
                <th>First Name</th>
                <th>Last Name</th>
            <% for(int i = 0; i < list.size(); i++) {
                Actor actor = new Actor();
                actor = list.get(i);



Pass values from form to form, How pass value from one form to another in JSP? In your servlet: req.setAttribute("myList", yourArrayList); In your jsp page you can access it with el like this: ${requestScope.myList} To iterate over your ArrayList using jstl, you could use the <c:forEach> tag. Like this:

the possible errors would be... set the array list from the servelt in the session, not the in the request. 2.the array you set is null. redirect the page instead of forward it. also you should not initialize the list and the category in jsp. try this.

for(Category cx: ((ArrayList<Category>)request.getAttribute("servletName"))) {

out.println( cx.getId());


out.println(cx.getMainCategoryId() );

How to pass arraylist from servlet to jsp?, in servlet { HttpSession hs=request.getSession(); String s="tamis"; String s1="​Tamils"; ArrayList<String> al=new ArrayList<String>(); al.add(s);  The common way of passing data from servlet to jsp is through defining attributes in the http request and then forwarding it to the corresponding jsp, this is done on the server side using one of the following techniques:

public class myActorServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private String name;
    private String user;
    private String pass;
    private String given_table;
    private String tid;
    private String firstname;
    private String lastname;
    private String action;

    public void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {


        // connecting to database
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        PrintWriter out = response.getWriter();
        name = request.getParameter("screenName");
        user = request.getParameter("username");
        pass = request.getParameter("password");
        tid = request.getParameter("tid");
        firstname = request.getParameter("firstname");
        lastname = request.getParameter("lastname");
        action = request.getParameter("action");
        given_table = request.getParameter("tableName");

        out.println("<title>Servlet JDBC</title>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
        out.println("<h1>Hello, " + name + " </h1>");
        out.println("<h1>Servlet JDBC</h1>");

        // init connection object
        String sqlSelect = "SELECT * FROM `" + given_table + "`";
        String sqlInsert = "INSERT INTO `" + given_table + "`(`firstName`, `lastName`) VALUES ('" + firstname + "', '" + lastname + "')";
        String sqlUpdate = "UPDATE `" + given_table + "` SET `firstName`='" + firstname + "',`lastName`='" + lastname + "' WHERE `id`=" + tid + "";
        String sqlDelete = "DELETE FROM `" + given_table + "` WHERE `id` = '" + tid + "'";

                "<p>Reading Table Data...Pass to JSP File...Okay<p>");

        ArrayList<Actor> list = new ArrayList<Actor>();
        // connecting to database
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/javabase", user, pass);
            stmt = con.createStatement();
            rs = stmt.executeQuery(sqlSelect);
            // displaying records

            while ( {
                Actor actor = new Actor();
            request.setAttribute("actors", list);
            RequestDispatcher view = request.getRequestDispatcher("myActors_1.jsp");
            view.forward(request, response);

        } catch (SQLException e) {
            throw new ServletException("Servlet Could not display records.", e);
        } catch (ClassNotFoundException e) {
            throw new ServletException("JDBC Driver not found.", e);
        } finally {
            try {
                if (rs != null) {
                    rs = null;
                if (stmt != null) {
                    stmt = null;
                if (con != null) {
                    con = null;
            } catch (SQLException e) {



Servlet Jsp 4 : Passing ArrayList With Employee Objects To Jsp , 1) First create data at the server side and pass it to a JSP. Here a list of student objects in a servlet will be created and pass it to a JSP using setAttribute(). 2) Next, the JSP will retrieve the sent data using getAttribute(). 3) Finally, the JSP will display the data retrieved, in a tabular form. I have a servlet that passes an ArrayList<String> to JSP. I am a beginner with JSP, and really am not sure how to pass an ArrayList<String> to JSP. I have successfully tried passing a regular string with no problems. Servlet

Passing an arraylist from servlet to jsp, Storing results set into Employees object having getter & setter method and storing them into Duration: 21:58 Posted: Nov 4, 2018 1) First create data at the server side and pass it to a JSP. Here a list of student objects in a servlet will be created and pass it to a JSP using setAttribute(). 2) Next, the JSP will retrieve the sent data using getAttribute(). 3) Finally, the JSP will display the data retrieved, in a tabular form.

Problem Passing ArrayList From Servlet To JSP Page, I am creating a small screen (without database connectovity) and am unable to pass the Arraylist from Servlet to JSP page. Please advice the  The servlet will be able to access the data by pulling it from the appropriate scope using the name mapped to the array data. In JSP use a bean with a setter and getter method that takes an ArrayList. Use the jsp:useBean tag to create an instance of the bean and place it into the appropriate scope.

Pass data from servlet to jsp, Passing ArrayList. We modify the previous example so that we pass a list of Student object, again we use setAttribute() method at the server side  Hi friends, I involved in a project and recently i get up from a big mess up in MySQL. After that,i do another mess up. There i need the resultset obtained in the servlet page(A bean file) to be passed to the Invoking Servlet file and then it can be accessed by JSP file.

  • Why not use JSP-EL+JSTL to deal with the objects? Secondly, how are you getting from the Servlet to the JSP? using a redirect or a forward?
  • @DebojitSaikia the result displays nothing
  • @DaveG i am a beginner and am not aware of JSP-EL+JSTL ... i am using following code to pass value in servlet .... getServletConfig().getServletContext().getRequestDispatcher("/GetCategory.jsp").forward(request,response);
  • @user2952188 try to get the dispatcher with request as request.getRequestDispatcher('/jspFile.jsp').forward(request,response), and make sure servlet will DO NOTHING after it forwarded the request. in other word you are now allowed to use a request/response one it has redirected or forwarded.
  • @user2511414 actually it works well for passing string from servlet to jsp ..Is it needed to change to pass object ??
  • you have forgot new before constructor new ArrayList<Category>() :)
  • How does the jsp know about the class Category?