Jason
Jason

Reputation: 12789

How do I parameterize getParameterNames to avoid the warning

    HttpServletRequest request;
    Enumeration params = request.getParameterNames();

How should I declare the return type for the above method?

Upvotes: 10

Views: 9806

Answers (3)

BalusC
BalusC

Reputation: 1108742

This method was parameterized since Servlet API 3.0 (Java EE 6). In older versions like Servlet API 2.5 (Java EE 5) and before this method (and many others) is not parameterized. You're apparently running a Servlet 2.5 container or older. You have basically 2 options:

  1. Upgrade to a Servlet 3.0 container (Tomcat 7, Glassfish 3, JBoss AS 6, etc) so that you can do

    Enumeration<String> params = request.getParameterNames();
    
  2. Make an unchecked cast.

    @SuppressWarnings("unchecked")
    Enumeration<String> params = (Enumeration<String>) request.getParameterNames();
    

Upvotes: 10

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Check out the ServletRequest API (since HttpServletRequest inherits this method from its super interface, ServletRequest) and it will tell you what this method returns: java.util.Enumeration<java.lang.String>, meaning you might wish to try:

HttpServletRequest request;
Enumeration<String> params = request.getParameterNames();

Edit 1:
I'm not sure why you're getting the first error. Perhaps you must cast the value returned from the method?

HttpServletRequest request;
Enumeration<String> params = (Enumeration<String>)request.getParameterNames();

But to be honest, while I"m pretty good at going through most API's, I don't do servlets and would appreciate any help from the experts here.

Upvotes: 4

Alex Gitelman
Alex Gitelman

Reputation: 24722

Not sure what method you referred to, but to suppress warnings for the method or for the statement put this in front of it:

@SuppressWarnings("unchecked")

It must be either before method declaration or before assignment statement.

Upvotes: 0

Related Questions