1

In my JSP I get a warning for this cast

<%
  Collection<Server> svr = (Collection<Server>)request.getAttribute("serverCollection");
%>

instanceOf doesn't seem to work here

Giann
  • 3,142
  • 3
  • 23
  • 33
stackoverflow
  • 18,348
  • 50
  • 129
  • 196

4 Answers4

3
<%
    @SupressWarnings("unchecked")
    Collection<Server> svr = (Collection<Server>)request.getAttribute("serverCollection");
%>

BTW, using scriplets is not very good thing, read this thread about avoiding scriplets.

Community
  • 1
  • 1
Roman
  • 64,384
  • 92
  • 238
  • 332
1

You can't "satisfy" that warning. It is an unchecked cast, and you can't really do anything about it since the method returns an Object.

If you're absolutely positive the attribute will always contain a Collection<Server> you can add a @SuppressWarnings("unchecked") annotation.

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

You could use JSTL instead of scriptlets. It would look like:

<c:set var="svr" value="${requestScope['serverCollection']}"/>
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

You cannot avoid this warning (except by suppressing it). The problem is that Java at runtime is only able to check that the object is of type Collection at runtime when casting. It cannot check that it is of type Collection<Server>. That is what the error means.

Mathias Schwarz
  • 7,099
  • 23
  • 28