I have a situation where one of the response headers Content-Disposition
has to be removed. So I thought of writing a servlet filter to do this. But I realized that the HttpServletResponse
has only a setHeader()
method but no method to remove it.
How can I do this?
相关问题
- how to create files under /WEB-INF/
- tomcat websocket servlet listening port
- Using regular expressions for filter-mapping
- how to use the image file located in local drive,
- JSP login with declarative security - How does the
相关文章
- java.lang.NoClassDefFoundError: javax/servlet/http
- Forward request from servlet to jsp
- Intercept @RequestHeader exception for missing hea
- Integrating Jetty with RESTEasy
- How to abort Tomcat startup upon exception in Serv
- XML Parsing Error in Firefox developer console
- Proper WWW-Authenticate header for OAuth provider
- servlet request parameter character encoding
As the other responses. There is no way to remove a header after being set, at least not standard (glassfish lets clear a header setting it's value to null). So at the end of the day you would have two choices:
reset the response.
response.reset()
This effectively removes ALL headers AND ALSO ANY BUFFERED DATA, depending on you case can be a good alternative (in my case was after authentication validation errors). If the response is already committed you'll get an IllegalStateException.
This does not work for me using Spring 4. I'm trying to strip out the Expires response header. For every page. Like so:
And here is how I add the filter:
setHeader() being called for Expires and Cache-Control, but I can't override the Expires filter value, or the Cache-Control value. I can add to the Cache-Control value. It turns into an array of values if I call setHeader on Cache-Control. But I need to delete the header.
You can't delete headers afterwards by the standard Servlet API. Your best bet is to just prevent the header from being set. You can do this by creating a
Filter
which replaces theServletResponse
with a customHttpServletResponseWrapper
implementation which skips thesetHeader()
's job whenever the header name isContent-Disposition
.Basically:
Just map that filter on the URL-pattern of interest to get it to run.
This may not be Servlet API compliant, but setting the value to null works on GlassFish 4 and probably on Tomcat too as that is what is underneath GlassFish.
We really need to update the Servlet API specification to either add a method to allow removing headers or to officially support using setHeader with a null value.
An example where this is important is if you use a security constraint (SSL/TLS) on your web application then static resource caching is complicated by the fact that the container will automatically add headers to prevent caching (you can try to disable with disableProxyCaching and securePagesWithPragma on Tomcat/GlassFish). I've already got a servlet filter for cache control that works great for non-secure content so I would like to keep cache control all in one place and simply set Prama and Cache-Control to null to clear any container added headers.