Results 1 to 2 of 2
  1. #1
    davis123877 is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default ArrayList cannot be resolved to a type in my JSP

    I am trying to pull an ArrayList request parameter in my JSP. The code for my servlet is:

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;

    public class HobbyPage extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

    ArrayList<String> myList = new ArrayList<String>();
    myList.add("Fred");
    myList.add("Pradeep");
    myList.add("Phillipe");
    request.setAttribute("names", myList);
    RequestDispatcher view = request.getRequestDispatcher("hobby.jsp");
    view.forward(request, response);
    }
    }

    and the code in my JSP is:

    <@ page import="java.util.ArrayList" %>
    <html><body>
    The friends who share your hobby of
    <%= request.getParameter("hobby") %>
    are: <br />
    <% ArrayList<String> al = (ArrayList<String>)request.getAttribute("names"); %>
    <% Iterator it = al.iterator();
    while (it.hasNext()) { %>
    <%= it.next() %>
    <br />
    <% } %>
    </body></html>


    When I run the app and get to the JSP I get the error:


    An error occurred at line: 6 in the jsp file: /hobby.jsp
    ArrayList cannot be resolved to a type
    3: The friends who share your hobby of
    4: <%= request.getParameter("hobby") %>
    5: are: <br />
    6: <% ArrayList<String> al = (ArrayList<String>)request.getAttribute("names"); %>
    7: <% Iterator it = al.iterator();
    8: while (it.hasNext()) { %>
    9: <%= it.next() %>

    Thanks!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: ArrayList cannot be resolved to a type in my JSP

    You shouldn't be using Java code in your JSP page.
    You should be using EL expressions (${your variables}) and JSTL tags.
    Java on a JSP page has been bad practice for almost 15 years.

    That will solve your issue,
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2015, 03:48 PM
  2. cannot be resolved to a type
    By johnmerlino in forum New To Java
    Replies: 4
    Last Post: 06-05-2014, 12:22 AM
  3. HttpServlet Cannot be resolved to a type
    By chizbox in forum Eclipse
    Replies: 3
    Last Post: 07-01-2012, 05:09 PM
  4. Cannot be resolved to a type
    By Naxix in forum AWT / Swing
    Replies: 1
    Last Post: 03-18-2012, 12:16 PM
  5. Filter cannot be resolved to a type??
    By Harrie_KalaChakra in forum Eclipse
    Replies: 1
    Last Post: 01-15-2010, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •