Ami Hollander
Ami Hollander

Reputation: 2535

Headers and Getters in HttpServletRequest

I am working with HttpServletRequest, and I must output all headers and information from that object.

for the headers I am using getHeadersNames()

Enumeration<String> headerEnums = servletRequest.getHeaderNames();
while (headerEnums.hasMoreElements()) {  
    String elementName = headerEnums.nextElement();
    String elementValue = servletRequest.getHeader(elementName);
    sb.append("Header.").append(elementName).append("=").append(elementValue).append(", ");
}

and afterwards I am retrieving all parameters using getters, for example:

sb.append("getAuthType").append("=").append(servletRequest.getAuthType());

I am getting duplicate arguments for example Header.content-type and ContentType from getContentType()

my questions:

  1. There is a nice way to output all servletRequest parameters without iterate over headers, attributes and getters? like toString()?
  2. How can i avoid retrieving duplicate arguments without having a temporal set?
  3. Is it possible to have an header inside Headers where its getter is empty? for example: content-type exist in Headers but getContentType() is null?

Upvotes: 0

Views: 2133

Answers (1)

Svetlin Zarev
Svetlin Zarev

Reputation: 15663

My answer is in the context of Apache Tomcat (8.5).

Is it possible to have an header inside Headers where its getter is empty? for example: content-type exist in Headers but getContentType() is null?

It's not possible, unless there is a bug. Those methods query the same internal data structure that contains the headers.

How can i avoid retrieving duplicate arguments without having a temporal set?

You are querying the same data structure twice - so it's pretty simple - do not ask twice for the same thing. Either use the headers, or use the methods from HttpServletRequest. The only difference is, that when using the methods, you'll get a default value (like -1, if the Content-Length is unknown), while you'll get NULL if you ask for a missing header.

There is a nice way to output all ServletRequest parameters without iterate over headers, attributes and getters? like toString()

At least I'm not aware of such standard method.

Upvotes: 1

Related Questions