aspose file tools*
The moose likes Servlets and the fly likes How to set Server HTTP Header Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to set Server HTTP Header" Watch "How to set Server HTTP Header" New topic
Author

How to set Server HTTP Header

David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
I'm trying to process an HTTP request and I want to set the "Server" HTTP header in my response. So, my code says:



where response is the HttpServletResponse object.

However, when the response is received and the "Server" header is displayed it's showing "Apache-Coyote/1.1". I AM able to successfully set other headers (like Warning).

Does anyone know if there's a way to override the default value set by the servlet container (obviously Tomcat in this case)?

Thanks,
David
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

If the server is overriding your setting, you're probably out of luck with spoofing the header. Why would you want to do this?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
Originally posted by Bear Bibeault:
If the server is overriding your setting, you're probably out of luck with spoofing the header. Why would you want to do this?


This is for a company internal web server. I have been given the requirement by the web consumer to send back the Server header parameter with a value indicative of the name/version of this particular service.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

They wouldn't be happy with a custom header?
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
Originally posted by Bear Bibeault:
They wouldn't be happy with a custom header?


Well that appears to be my fallback if I can't get "Server" set properly. I figured you all here at the Saloon would be able to tell me if it's even possible to set the "Server" header or not. If not, then I'll have to go back to them and negotiate a custom header.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Not sure if wrapping the response object would allow you to overwrite this.
Coyote is the connector itself. It may add this after all of the spec driven servlet stuff has finished.

if you feel ambitious, grab the source to Tomcat and do grep search for the string "Apache-Coyote/1.1". It should be easy to find and replace.

Obviously, altering the server to suit the app has its pitfalls but it wouldn't hurt to learn exactly where that header is being written.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
Originally posted by Ben Souther:
Not sure if wrapping the response object would allow you to overwrite this.
Coyote is the connector itself. It may add this after all of the spec driven servlet stuff has finished.

if you feel ambitious, grab the source to Tomcat and do grep search for the string "Apache-Coyote/1.1". It should be easy to find and replace.

Obviously, altering the server to suit the app has its pitfalls but it wouldn't hurt to learn exactly where that header is being written.


Thanks for the suggestion. I'm not sure I'm that ambitious and I don't think I want to alter the server to suit only this portion of the app. Our HTTPS receiver could theoretically need to set different "Server" values depending on the HTTPS request being sent thus my desire to be able to set it in the app code (not the server code).
Majid Al-Fifi
Ranch Hand

Joined: Aug 22, 2006
Posts: 45
I have tested it on Tomcat 5.0.30 and weblogic 8.1,
I used the following:
public void service(HttpServletRequest req, HttpServletResponse res){
res.setHeader("Server","myserver");
}
I also tried it with doGet and doPost and the result is as follows:

Tomcat 5.0.30:
HTTP/1.1 200 OK
Server: myserver
Content-length: 0
Date: Sat, 02 Sep 2006 12:44:54 GMT
Connection: close


Weblogic 8.1:
HTTP/1.1 200 OK
Date: Sat, 02 Sep 2006 12:46:00 GMT
Server: myserver
Content-length: 0
Connection: Close

I don't know why it is not working for you...


SCJP1.4, SCWCD1.4
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
Originally posted by Majid Al-Fifi:
I have tested it on Tomcat 5.0.30 and weblogic 8.1,

I don't know why it is not working for you...


Interesting. It doesn't seem like it should matter but I'm using Tomcat 5.5.17.

From the following site:

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Response-Headers.html

I read the following:

"Server => What server am I? Servlets don't usually set this; the Web server itself does."
Majid Al-Fifi
Ranch Hand

Joined: Aug 22, 2006
Posts: 45
Originally posted by David Sica:


"Server => What server am I? Servlets don't usually set this; the Web server itself does."



yes they don't usually set it but they can.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

Originally posted by Majid Al-Fifi:

yes they don't usually set it but they can.


But not if the container override the value. Then the point becomes moot.
Rashid Mayes
Ranch Hand

Joined: Jan 11, 2006
Posts: 160
Try checking the http connector configuration in the server.xml. Check to see if the "server" attribute is specified.


Rashid Mayes
http://www.hostj2me.com/ - http://www.worlddeveloper.org/
David Sica
Ranch Hand

Joined: Jan 09, 2002
Posts: 39
Originally posted by Rashid Mayes:
Try checking the http connector configuration in the server.xml. Check to see if the "server" attribute is specified.


Thanks for the tip. My Connector configuration looks like:



I don't see a server attribute in this configuration file.
Rashid Mayes
Ranch Hand

Joined: Jan 11, 2006
Posts: 160
The server attribute is optional. Try added it to see if you get the desired result. Here is a link to the documentation. The server attribute is discussed under "standard implementation" about 2/3 thirds of the way down.

http://tomcat.apache.org/tomcat-5.5-doc/config/http.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set Server HTTP Header