File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes problem setting custom header in response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem setting custom header in response" Watch "problem setting custom header in response" New topic
Author

problem setting custom header in response

Rahul Birdie
Greenhorn

Joined: Oct 31, 2007
Posts: 5
Hi,
I am having problem setting custom header in my response.I have tried using setHeader(),addHeader()...but i dont see the header in the response.Read some where that we can override setheader() in our response wrapper class.Can some one please tell me how to do that.

thanks in advance
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
show us the code & the response headers.


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Rahul Birdie
Greenhorn

Joined: Oct 31, 2007
Posts: 5
here is my servlet :
public void doGet(HttpServletRequest request, HttpServletResponse response){
response.addHeader("name", "rahul");
response.setHeader("name", "rahul");
response.sendRedirect("/some.jsp");}


in my JSP :
<%
Enumeration e =request.getHeaderNames();
while(e.hasMoreElements()){
String header= String.valueOf(e.nextElement());
out.println("Header :" + e.nextElement()+ "<BR>");
out.println("Value : " + request.getHeader(header));
}%>


I am using livehttp header too to check the header.Output from live http


GET /StatusReturn/StatusFilterCode/WebContent/WEB-INF/jsp/final.jsp HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-securid
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: JSESSIONID=54411B48ED5F49AD57DE911CD31A839A

HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=4BCAE7B35828BE3426D0EAA075AE4B0C; Path=/
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 273
Date: Wed, 31 Oct 2007 23:35:24 GMT
[ October 31, 2007: Message edited by: Rahul Birdie ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42608
    
  65
If you're doing a sendRedirect, you're asking the browser to make another request (to the JSP). Once the browser makes that request, the response header of the previous request are lost.

And in any case, request headers and response headers are different, so the code wouldn't work no matter what.


Ping & DNS - my free Android networking tools app
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
would a RequestDispatcher & Request Attribute achieve what you are after:

Rahul Birdie
Greenhorn

Joined: Oct 31, 2007
Posts: 5
I have tried requestdispatcher too,doesnt seem to set the header....i dont see it in live http header..

GET /StatusReturn/StatusFilterCode/WebContent/WEB-INF/jsp/final.jsp HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-securid
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: JSESSIONID=E92B4D52912EB982974DD7D1F7AE4FCB

HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=45DF1E1A2F08BBD1C421CAD57788EC9B; Path=/
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 383
Date: Thu, 01 Nov 2007 01:10:10 GMT
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Let's back up a bit.
Why are you trying to set a custom header?
There might be a better way to achieve your goal than by using headers.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rahul Birdie
Greenhorn

Joined: Oct 31, 2007
Posts: 5
I am trying to create a test env. for which i need to set an error status say 403...but i am not able to do it...i have tried setstatus(403) and the setHeader(SC_ , message) but i am not getting the status in the jsp.I am using only one jsp page.


Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

If you were to look at the interface for HttpServletResponse you will find the supported means to set an error code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rahul Birdie
Greenhorn

Joined: Oct 31, 2007
Posts: 5
i have looked at the interface but setheader,addheader,setstatus,senderror are the methods i can use and i have tried all of them.I have used filters too but still nothin.In DD i even have specified <error> tags.I still cant figure out what to do.....
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

A response code is not a header.
It's part of the start-line.
See:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4
for more information on HTTP messages.

Did you try using:
response.sendError(response.SC_FORBIDDEN, YOUR_MESSAGE_HERE);
?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem setting custom header in response