I want to loop through an enumeration containing all of the header names inside a java servlet. My code is as follows:
public class ServletParameterServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
ServletConfig c = this.getServletConfig();
PrintWriter writer = response.getWriter();
writer.append("database: ").append(c.getInitParameter("database"))
.append(", server: ").append(c.getInitParameter("server"));
Enumeration<String> headerNames = ((HttpServletRequest) c).getHeaderNames();
}
}
Is this the correct syntax? How does one actually iterate over an enums values in Java? And specifically in this instance?
Thanks for your help, Marc
It's just an iteration like normal Java:
Note that for some setups this is a bit dangerous in that HTTP headers can be duplicated. In that case, the
headerValuewill be only the first HTTP header with that name. UsegetHeadersto get all of them.And throw away whatever Eclipse was suggesting - it's garbage.