简体   繁体   中英

Showing error while sending arraylist from servlet to jsp

I want to send a ArrayList object from servlet page to a jsp page.But there show an error in jsp page.

Below the piece of my servlet code

Servlet

ArrayList<Customer> al = new ArrayList<Customer>();
al = abs.viewCustomerReqRequest();
//Return type of viewCustomerReqRequest() method is ArrayList<Customer>

request.setAttribute("Customer_AL",al);
RequestDispatcher rd = request.getRequestDispatcher("a_reg_request.jsp");
rd.forward(request, response);

Below piece of my jsp page code

JSP

<%
  ArrayList<Customer> al = new ArrayList<Customer>();
  al = (ArrayList<Customer>) request.getAttribute("Customer_AL");
  Iterator<Customer> it = al.iterator();
%>

But In my jsp page there're showing an error like " Type safety: Unchecked cast from Object to ArrayList<Customer> ".

How can I remove this error from my jsp page?

The second parameter to ServletRequest.setAttribute() is of type Object .

Once you pass the List as an Object parameter, it loses compile-time typing. The warning tells you about that. But it is a warning, not an error.

If you want to suppress the warning, you can use

@SuppressWarnings("unchecked")
al = (ArrayList<Customer>) request.getAttribute("Customer_AL");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM