Java Servlets: Remove a HTTP header?

  • Thread starter Joona I Palaste
  • Start date
J

Joona I Palaste

Is there a way in the Java Servlets API (J2EE 1.4) to remove a HTTP
header from the response? response.setHeader(name, null) sets the value
of the header to the literal string "null", i.e. a four-character string
consisting of 'n' 'u' 'l' 'l'. I need to get rid of the entire header.
Is this possible?
 
W

William Brogden

Joona I Palaste said:
Is there a way in the Java Servlets API (J2EE 1.4) to remove a HTTP
header from the response? response.setHeader(name, null) sets the value
of the header to the literal string "null", i.e. a four-character string
consisting of 'n' 'u' 'l' 'l'. I need to get rid of the entire header.
Is this possible?

I wonder if you could do that with a custom HttpServletResponseWrapper that
would pass through all headers except the one you want to block.

Bill
 
J

Joona I Palaste

I wonder if you could do that with a custom HttpServletResponseWrapper that
would pass through all headers except the one you want to block.

I don't see how that could work. Sure, I could write my own subclass of
HttpServletResponseWrapper. But what would that help? The client is
still going to get the original HttpServletResponse. As you can recall,
the signature of the doGet() method in HttpServlet is:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException;
It's not returning a HttpServletResponse, it's accepting one as a
parameter. And Java methods can't change their parameters.
 
W

William Brogden

Joona I Palaste said:
I don't see how that could work. Sure, I could write my own subclass of
HttpServletResponseWrapper. But what would that help? The client is
still going to get the original HttpServletResponse. As you can recall,
the signature of the doGet() method in HttpServlet is:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException;
It's not returning a HttpServletResponse, it's accepting one as a
parameter. And Java methods can't change their parameters.

From looking at the javax.servlet.Filter interface, - there is a note
under the doFilter() method about wrapping the response object
with a custom object "to filter content or headers" for output
filtering. I assume they mean a subclass of the response wrapper,
so it sounds promising.

Bill
 
A

Adam Maass

William Brogden said:
From looking at the javax.servlet.Filter interface, - there is a note
under the doFilter() method about wrapping the response object
with a custom object "to filter content or headers" for output
filtering. I assume they mean a subclass of the response wrapper,
so it sounds promising.

A servlet Filter has the signature

doFilter(ServletRequest, ServletResponse, FilterChain)


A filter is supposed to make some (minor) changes, then call the "doFilter"
method on the passed in FilterChain object with a ServletRequest and
ServletResponse.

So, to filter out an HTTP response header, write a filter that wraps the
passed ServletResponse in an HttpServletResponseWrapper that ignores the
header that needs to be filtered out, then pass the new ServletResponse to
the FilterChain.


Filters are declared in the web.xml file much like servlets are.



-- Adam Maass
 
B

Ben_

FYI, if you can't do it in Java, Apache HTTP server can do it. Maybe other
web servers can do it as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top