• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set Server HTTP Header

 
David Sica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the server is overriding your setting, you're probably out of luck with spoofing the header. Why would you want to do this?
 
David Sica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They wouldn't be happy with a custom header?
 
David Sica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Sica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
David Sica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try checking the http connector configuration in the server.xml. Check to see if the "server" attribute is specified.
 
David Sica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic