开发者

What is the difference between Servlet response methods addHeader and setHeader?

开发者 https://www.devze.com 2023-02-24 08:00 出处:网络
Can I use setHeader to set an new header? Or Do I need to addHeader first, then use s开发者_如何学PythonetHeader method?The documentation says that you can add multiple values to a particular header u

Can I use setHeader to set an new header?

Or Do I need to addHeader first, then use s开发者_如何学PythonetHeader method?


The documentation says that you can add multiple values to a particular header using the addHeader method, whereas an initial value would be overwritten if you use the setHeader method.

In both cases a non-existent header would be created.


Javadocs are your friend:

void addHeader(String name, String value)

Adds a response header with the given name and value. This method allows response headers to have multiple values.

void setHeader(String name, String value)

Sets a response header with the given name and value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.


Both setHeader() and addHeader() will add a header and value to the response if the header is not already in the response. The difference between set and add shows up when the header is there. In that case: setHeader() overwrites the existing value, whereas addHeader() adds an additional value.

0

精彩评论

暂无评论...
验证码 换一张
取 消