You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Huseyin Sinecan <hs...@yahoo.com> on 2007/04/24 15:29:41 UTC

http header setting problem in servlet

                     Hello all,

I want to set header parameters of http response in my servlet.

I set parameters as below:

**************************************
theResponse.reset();
theResponse.resetBuffer();

theResponse.setHeader("Request Version", "HTTP/1.1");
  theResponse.setStatus(200);
  theResponse.setHeader("Date", "Wed, 18 Apr 2007 09:24:04 GMT");
  theResponse.setHeader("Server", "Tomcat");
  theResponse.addCookie(theRequest.getCookies()[0]);
  theResponse.setHeader("pragma", "no-cache"); // HTTP 1.0
  theResponse.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
  theResponse.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // a past date
  theResponse.setDateHeader("Last-Modified", (new java.util.Date()).getTime());
  theResponse.setHeader("Connection", "close");
  theResponse.setContentType("text/plain");

theResponse.flushBuffer();

//..........
//some other things...
//..........

  PrintWriter aPrintWriter = theResponse.getWriter();
  aPrintWriter.write("this is the string to be put into the body part of the http packet.");

aPrintWriter.flush();
**************************************

However, when I check what I am sending as response (using the ethereal network sniffing tool) I see the result below:
***************************************
HTTP/1.1 200 OK\r\n
     Request Version: HTTP/1.1
     Response Code: 200
Server: Apache-Coyote/1.1\r\n
Content-Length: 68\r\n
Date: Tue, 24 Apr 2007 17:55:10 GMT\r\n
\r\n
Data (68 bytes)
***************************************

I am setting header parameters as it should be.. But it doesn't care about my parameters and Tomcat puts some values that it selects itself..

Would anyone has any idea about what I am doing wrong here..?

Thanks & Regards


Huseyin Sinecan

       
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.

Re: http header setting problem in servlet

Posted by Martin Gainty <mg...@hotmail.com>.
I assume you've seen this post on implementing httplib?
http://www.ethereal.com/lists/ethereal-users/200512/msg00203.html

In the meanwhile Curl implements this feature with curl_setopt
http://us2.php.net/curl

M--
This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.

----- Original Message ----- 
From: "Huseyin Sinecan" <hs...@yahoo.com>
To: <us...@tomcat.apache.org>
Sent: Tuesday, April 24, 2007 9:29 AM
Subject: http header setting problem in servlet


>                     Hello all,
>
> I want to set header parameters of http response in my servlet.
>
> I set parameters as below:
>
> **************************************
> theResponse.reset();
> theResponse.resetBuffer();
>
> theResponse.setHeader("Request Version", "HTTP/1.1");
>  theResponse.setStatus(200);
>  theResponse.setHeader("Date", "Wed, 18 Apr 2007 09:24:04 GMT");
>  theResponse.setHeader("Server", "Tomcat");
>  theResponse.addCookie(theRequest.getCookies()[0]);
>  theResponse.setHeader("pragma", "no-cache"); // HTTP 1.0
>  theResponse.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
>  theResponse.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // a 
> past date
>  theResponse.setDateHeader("Last-Modified", (new 
> java.util.Date()).getTime());
>  theResponse.setHeader("Connection", "close");
>  theResponse.setContentType("text/plain");
>
> theResponse.flushBuffer();
>
> //..........
> //some other things...
> //..........
>
>  PrintWriter aPrintWriter = theResponse.getWriter();
>  aPrintWriter.write("this is the string to be put into the body part of 
> the http packet.");
>
> aPrintWriter.flush();
> **************************************
>
> However, when I check what I am sending as response (using the ethereal 
> network sniffing tool) I see the result below:
> ***************************************
> HTTP/1.1 200 OK\r\n
>     Request Version: HTTP/1.1
>     Response Code: 200
> Server: Apache-Coyote/1.1\r\n
> Content-Length: 68\r\n
> Date: Tue, 24 Apr 2007 17:55:10 GMT\r\n
> \r\n
> Data (68 bytes)
> ***************************************
>
> I am setting header parameters as it should be.. But it doesn't care about 
> my parameters and Tomcat puts some values that it selects itself..
>
> Would anyone has any idea about what I am doing wrong here..?
>
> Thanks & Regards
>
>
> Huseyin Sinecan
>
>
> ---------------------------------
> Ahhh...imagining that irresistible "new car" smell?
> Check outnew cars at Yahoo! Autos. 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: http header setting problem in servlet

Posted by Hassan Schroeder <ha...@gmail.com>.
On 4/24/07, Huseyin Sinecan <hs...@yahoo.com> wrote:

> Thanks for the answer.. Here is the complete code of doGet method:

For clarity, here's my entire adaptation of your code:
-----------------------------------------------------------------------------------
package com.example.test;

import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class Something extends HttpServlet {

		protected void doGet(HttpServletRequest theRequest,
HttpServletResponse theResponse) throws ServletException, IOException
{

       theResponse.reset();
       theResponse.resetBuffer();

       theResponse.setHeader("Request Version", "HTTP/1.1");
       theResponse.setStatus(200);
       theResponse.setHeader("Date", "Wed, 18 Apr 2007 09:24:04 GMT");
       theResponse.setHeader("Server", "Tomcat");
       theResponse.addCookie(theRequest.getCookies()[0]);
       theResponse.setHeader("pragma", "no-cache"); // HTTP 1.0
       theResponse.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
       theResponse.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00
GMT"); // a past date
       theResponse.setDateHeader("Last-Modified", (new
java.util.Date()).getTime());
       theResponse.setHeader("Connection", "close");
       theResponse.setContentType("text/plain");

       theResponse.flushBuffer();

       String aResponse = "what now?";

       PrintWriter aPrintWriter = theResponse.getWriter();
       aPrintWriter.write(aResponse);
       aPrintWriter.flush();

   }
}
-----------------------------------------------------------------------------------

And here's the result from LiveHTTPHeaders -- hopefully that'll give
you something to go on...
-----------------------------------------------------------------------------------
HTTP/1.x 200 OK
Date: Tue, 24 Apr 2007 16:32:06 GMT
Server: foo/bar <= ignore, that's my own setting on the Connector
Set-Cookie: JSESSIONID=746B06EB8432B81A63CBA04725E2F131
Pragma: no-cache
Cache-Control: no-cache
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Last-Modified: Tue, 24 Apr 2007 16:32:06 GMT
Connection: close
Content-Type: text/plain
Transfer-Encoding: chunked
-----------------------------------------------------------------------------------

HTH!
-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: http header setting problem in servlet

Posted by Huseyin Sinecan <hs...@yahoo.com>.
Hello,

Thanks for the answer.. Here is the complete code of doGet method:


************************
    protected void doGet(HttpServletRequest theRequest, HttpServletResponse theResponse) throws ServletException, IOException {

        theResponse.reset();
        theResponse.resetBuffer();

        theResponse.setHeader("Request Version", "HTTP/1.1");
        theResponse.setStatus(200);
        theResponse.setHeader("Date", "Wed, 18 Apr 2007 09:24:04 GMT");
        theResponse.setHeader("Server", "Tomcat");
        theResponse.addCookie(theRequest.getCookies()[0]);
        //theResponse.setHeader("Set-Cookie", "JSESSIONID=FixForBuggyProxy;Version=1;Discard;Path=\"/\""); // HTTP 1.1
        theResponse.setHeader("pragma", "no-cache"); // HTTP 1.0
        theResponse.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
        theResponse.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // a past date
        theResponse.setDateHeader("Last-Modified", (new java.util.Date()).getTime());
        //theResponse.setHeader("Set-Cookie2", "JSESSIONID=ForBuggyCachingProxies;Path=\"/\""); // HTTP 1.1
        theResponse.setHeader("Connection", "close");
        theResponse.setContentType("text/plain");

        theResponse.flushBuffer();

        String aResponse = "";
        try {
            RequestProcessor aRequestProcessor = new RequestProcessor();

            aResponse = aRequestProcessor.processRequest(theRequest);
        } catch (InvalidReqException e) {

            System.out.println("EXCEPTION OCCURED: " + e);
        } //end of try-catch block
        catch (Exception ex) {
            System.out.println(ex);
        }

        //theResponse.setContentLength(aResponse.length());

        PrintWriter aPrintWriter = theResponse.getWriter();
        aPrintWriter.write(aResponse);
        aPrintWriter.flush();

    } //end of doGet method
************************

I think I am playing with the correct response...
And here is an example communication between the client and my servlet application:
*******************************
POST /MmpNg/MmpNgServlet?x=y HTTP/1.1 
Cookie: JSESSIONID=FixForBuggyProxy 
User-Agent: OutlookAddin 
Host: 192.168.10.77 
Content-Length: 163 
Cache-Control: no-cache 
 
"data coming from client"
HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Content-Length: 68 
Date: Tue, 24 Apr 2007 17:55:10 GMT 
 "data to be sent to client"
*******************************

Would you have any comments on this..? Sth is certainly wrong, but I am not able to see it..

Thanks & Regards


Huseyin Sinecan




Hassan Schroeder <ha...@gmail.com> wrote: On 4/24/07, Huseyin Sinecan  wrote:

> I want to set header parameters of http response in my servlet.

Are you sure you're working on the right response object? Perhaps
you can post a simple but complete example that fails...

I just tried the following file as a JSP:
------------------------------------------------------------------
<%
 response.setHeader("HooHaa", "whatever");
 response.setHeader("Cache-Control", "no-cache");
%>
what now?
------------------------------------------------------------------
:: and can see the headers are set (using LiveHTTPHeaders).

-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org



       
---------------------------------
Ahhh...imagining that irresistible "new car" smell?
 Check outnew cars at Yahoo! Autos.

Re: http header setting problem in servlet

Posted by Hassan Schroeder <ha...@gmail.com>.
On 4/24/07, Huseyin Sinecan <hs...@yahoo.com> wrote:

> I want to set header parameters of http response in my servlet.

Are you sure you're working on the right response object? Perhaps
you can post a simple but complete example that fails...

I just tried the following file as a JSP:
------------------------------------------------------------------
<%
	response.setHeader("HooHaa", "whatever");
	response.setHeader("Cache-Control", "no-cache");
%>
what now?
------------------------------------------------------------------
:: and can see the headers are set (using LiveHTTPHeaders).

-- 
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org