You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Anoop kumar V <an...@gmail.com> on 2006/02/03 19:49:00 UTC

request.getHeader() gives null

HI All,

I am using Tomcat5.
I have a simple jsp displaying the header information that I have set in
another jsp page. For some reason I can see all the default headers except
the headers that I have set in this jsp. I am doing a jsp:forward to go to
the next page which displays the header information ( I have also tried
redirect - but just wont work)...

------------------------------------------------------------------------------------------------------------
------------------------------------------
index.jsp----------------------------------------------------
------------------------------------------------------------------------------------------------------------

<%@ page import="java.io.PrintWriter"%>
<%--
 @author <a href="mailto: akumar@iditarodsys.com">Anoop Kumar V.</a>
         Date: Jan 19, 2006 4:13:05 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head><title>Simple jsp page to show header values</title>
  </head>
  <body>

  <br/>
  <%
      System.out.println("Jsp called successfully");

      response.addHeader("userName","GSE_FMS");
      response.setHeader("userName","GSE_FMS");  //just to be sure.....
      response.addHeader("myValue","theValue");

      boolean isheader = response.containsHeader("userName");

      System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\ncontains userName
header ..."+isheader);
      System.out.println("contains myValue header ..." +
response.containsHeader("myValue"));
      //response.sendRedirect("/TestHeaderResponse.jsp");

  %>
  <jsp:forward page="/TestHeaderResponse.jsp"/>
  </body>
</html>
------------------------------------------------------------------------------------------------------------
--------------------------
TestHeaderResponse.jsp----------------------------------------------
------------------------------------------------------------------------------------------------------------
<%@ page import="java.util.Enumeration"%>
<%--
 Created for Sikorsky GSE
 @author <a href="mailto: akumar@iditarodsys.com">Anoop Kumar V.</a>
         Date: Jan 19, 2006 4:13:05 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head><title>Simple jsp page to show header values</title>
  </head>
  <body>
  <br/>
  <%
      System.out.println("Test header response page called successfully");

      String s = request.getHeader("userName");
      System.out.println("user: "+s);           // Issue: always returns
null even though set in the previous page
      out.println("USERNAME HEADER: " + s +"<br/>");

      Enumeration headernames = request.getHeaderNames();
      System.out.println("Headers---------");
      while (headernames.hasMoreElements()){
          String headername = (String)headernames.nextElement();
          String headervalue = request.getHeader(headername);
          System.out.println(headername+"----"+headervalue);
          out.println("    " + headername+"----"+headervalue + "<br/>");
          out.flush();
    }
  %>
  </body>
</html>
------------------------------------------------------------------------------------------------------------

I think I am doing something fundamentally wrong....

Also can someone help me with understanding how the setHeader and getHeader
works - we use response.setHeader to set but use request.getHeader to read
the header values. Is it that the response of the first page becomes the
request of the next jsp....
--
Thanks and best regards,
Anoop

Re: request.getHeader() gives null

Posted by Anoop kumar V <an...@gmail.com>.
Thanks a lot to both David Smith and Yue Mu - I am exploring other options
of setting values.

Also someone else has suggested that I use POST from a form - i willl try
that too - thanks a ton,

Anoop Kumar V.

On 2/5/06, David Smith <dn...@cornell.edu> wrote:
>
> Anoop --
>
> Correct me if I'm wrong, but it looks like you are trying to set
> response headers on a 302 response and then expecting those headers to
> return on the next request.  I can't find any information indicating
> this behavior is supported by the HTTP protocol.  You are better off
> setting cookies, GET parameters, or session attributes to transfer info
> the the next request.  The first two will work for any request to the
> same server.  The last one will only work if the next request is going
> to the same webapp.
>
> --David
>
> Yue Mu wrote:
> > HI Anoop,
> >
> >   >> Is it that the response of the first page becomes the request of
> the next jsp....
> >
> >   No - "the next jsp" gets the same request, which is the whole idea of
> "forwarding" a request. This is why you don't see the newly added headers
> (to the response!).
> >
> >   I'm not sure what you are trying to do but you need to use
> Response.containHeaders(java.lang.String name) to check if a header is
> already there.
> >
> >   Hope this helps.
> >
> >   Regards,
> >   Z.
> >
> > Anoop kumar V <an...@gmail.com> wrote:
> >   I would really appreciate if someone can answer my question..
> >
> > Thanks,
> > Anoop
> >
> > On 2/3/06, Anoop kumar V wrote:
> >
> >> HI All,
> >>
> >> I am using Tomcat5.
> >> I have a simple jsp displaying the header information that I have set
> in
> >> another jsp page. For some reason I can see all the default headers
> except
> >> the headers that I have set in this jsp. I am doing a jsp:forward to go
> to
> >> the next page which displays the header information ( I have also tried
> >> redirect - but just wont work)...
> >>
> >>
> >>
> ------------------------------------------------------------------------------------------------------------
> >> ------------------------------------------
> >> index.jsp----------------------------------------------------
> >>
> >>
> ------------------------------------------------------------------------------------------------------------
> >>
> >>
> >>
> >>>  @author Anoop Kumar V.
> >>>
> >>          Date: Jan 19, 2006 4:13:05 PM
> >> --%>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> >
> >>>       System.out.println("Jsp called successfully");
> >>>
> >>       response.addHeader("userName","GSE_FMS");
> >>       response.setHeader("userName","GSE_FMS");  //just to be sure.....
> >>       response.addHeader("myValue","theValue");
> >>
> >>       boolean isheader = response.containsHeader("userName");
> >>
> >>       System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\ncontains
> >> userName header ..."+isheader);
> >>       System.out.println("contains myValue header ..." +
> >> response.containsHeader("myValue"));
> >>       //response.sendRedirect("/TestHeaderResponse.jsp");
> >>
> >>   %>
> >>
> >>
> >>
> >>
> >>
> ------------------------------------------------------------------------------------------------------------
> >> --------------------------
> >> TestHeaderResponse.jsp----------------------------------------------
> >>
> >>
> ------------------------------------------------------------------------------------------------------------
> >>
> >>
> >>>  Created for Sikorsky GSE
> >>>
> >>  @author Anoop Kumar V.
> >>          Date: Jan 19, 2006 4:13:05 PM
> >> --%>
> >>
> >>
> >>
> >>
> >>
> >>
> >>
> >
> >
> >>>       System.out.println("Test header response page called
> successfully");
> >>>
> >>       String s = request.getHeader("userName");
> >>       System.out.println("user: "+s);           // Issue: always
> returns
> >> null even though set in the previous page
> >>       out.println("USERNAME HEADER: " + s +"
> >>
> > ");
> >
> >>       Enumeration headernames = request.getHeaderNames();
> >>       System.out.println("Headers---------");
> >>       while (headernames.hasMoreElements()){
> >>           String headername = (String)headernames.nextElement();
> >>           String headervalue = request.getHeader(headername);
> >>           System.out.println(headername+"----"+headervalue);
> >>           out.println("    " + headername+"----"+headervalue + "
> >>
> > ");
> >
> >>           out.flush();
> >>     }
> >>   %>
> >>
> >>
> >>
> >>
> ------------------------------------------------------------------------------------------------------------
> >>
> >> I think I am doing something fundamentally wrong....
> >>
> >> Also can someone help me with understanding how the setHeader and
> >> getHeader works - we use response.setHeader to set but use
> >> request.getHeader to read the header values. Is it that the response of
> >> the first page becomes the request of the next jsp....
> >> --
> >> Thanks and best regards,
> >> Anoop
> >>
> >>
> >>
> >
> >
> > --
> > Thanks and best regards,
> > Anoop
> >
> >
> >
> > ---------------------------------
> > Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>


--
Thanks and best regards,
Anoop

Re: request.getHeader() gives null

Posted by David Smith <dn...@cornell.edu>.
Anoop --

Correct me if I'm wrong, but it looks like you are trying to set 
response headers on a 302 response and then expecting those headers to 
return on the next request.  I can't find any information indicating 
this behavior is supported by the HTTP protocol.  You are better off 
setting cookies, GET parameters, or session attributes to transfer info 
the the next request.  The first two will work for any request to the 
same server.  The last one will only work if the next request is going 
to the same webapp.

--David

Yue Mu wrote:
> HI Anoop,
>    
>   >> Is it that the response of the first page becomes the request of the next jsp....
>    
>   No - "the next jsp" gets the same request, which is the whole idea of "forwarding" a request. This is why you don't see the newly added headers (to the response!). 
>    
>   I'm not sure what you are trying to do but you need to use Response.containHeaders(java.lang.String name) to check if a header is already there.
>    
>   Hope this helps.
>    
>   Regards,
>   Z.
>
> Anoop kumar V <an...@gmail.com> wrote:
>   I would really appreciate if someone can answer my question..
>
> Thanks,
> Anoop
>
> On 2/3/06, Anoop kumar V wrote:
>   
>> HI All,
>>
>> I am using Tomcat5.
>> I have a simple jsp displaying the header information that I have set in
>> another jsp page. For some reason I can see all the default headers except
>> the headers that I have set in this jsp. I am doing a jsp:forward to go to
>> the next page which displays the header information ( I have also tried
>> redirect - but just wont work)...
>>
>>
>> ------------------------------------------------------------------------------------------------------------
>> ------------------------------------------
>> index.jsp----------------------------------------------------
>>
>> ------------------------------------------------------------------------------------------------------------
>>
>>
>>     
>>>  @author Anoop Kumar V.
>>>       
>>          Date: Jan 19, 2006 4:13:05 PM
>> --%>
>>
>>
>>
>>
>>
>>
>>
>>     
>
>   
>>>       System.out.println("Jsp called successfully");
>>>       
>>       response.addHeader("userName","GSE_FMS");
>>       response.setHeader("userName","GSE_FMS");  //just to be sure.....
>>       response.addHeader("myValue","theValue");
>>
>>       boolean isheader = response.containsHeader("userName");
>>
>>       System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\ncontains
>> userName header ..."+isheader);
>>       System.out.println("contains myValue header ..." +
>> response.containsHeader("myValue"));
>>       //response.sendRedirect("/TestHeaderResponse.jsp");
>>
>>   %>
>>
>>
>>
>>
>> ------------------------------------------------------------------------------------------------------------
>> --------------------------
>> TestHeaderResponse.jsp----------------------------------------------
>>
>> ------------------------------------------------------------------------------------------------------------
>>
>>     
>>>  Created for Sikorsky GSE
>>>       
>>  @author Anoop Kumar V.
>>          Date: Jan 19, 2006 4:13:05 PM
>> --%>
>>
>>
>>
>>
>>
>>
>>     
>
>   
>>>       System.out.println("Test header response page called successfully");
>>>       
>>       String s = request.getHeader("userName");
>>       System.out.println("user: "+s);           // Issue: always returns
>> null even though set in the previous page
>>       out.println("USERNAME HEADER: " + s +"
>>     
> ");
>   
>>       Enumeration headernames = request.getHeaderNames();
>>       System.out.println("Headers---------");
>>       while (headernames.hasMoreElements()){
>>           String headername = (String)headernames.nextElement();
>>           String headervalue = request.getHeader(headername);
>>           System.out.println(headername+"----"+headervalue);
>>           out.println("    " + headername+"----"+headervalue + "
>>     
> ");
>   
>>           out.flush();
>>     }
>>   %>
>>
>>
>>
>> ------------------------------------------------------------------------------------------------------------
>>
>> I think I am doing something fundamentally wrong....
>>
>> Also can someone help me with understanding how the setHeader and
>> getHeader works - we use response.setHeader to set but use
>> request.getHeader to read the header values. Is it that the response of
>> the first page becomes the request of the next jsp....
>> --
>> Thanks and best regards,
>> Anoop
>>
>>
>>     
>
>
> --
> Thanks and best regards,
> Anoop
>
>
> 		
> ---------------------------------
> Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
>   


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


Re: request.getHeader() gives null

Posted by Yue Mu <yu...@yahoo.com>.
HI Anoop,
   
  >> Is it that the response of the first page becomes the request of the next jsp....
   
  No - "the next jsp" gets the same request, which is the whole idea of "forwarding" a request. This is why you don't see the newly added headers (to the response!). 
   
  I'm not sure what you are trying to do but you need to use Response.containHeaders(java.lang.String name) to check if a header is already there.
   
  Hope this helps.
   
  Regards,
  Z.

Anoop kumar V <an...@gmail.com> wrote:
  I would really appreciate if someone can answer my question..

Thanks,
Anoop

On 2/3/06, Anoop kumar V wrote:
>
> HI All,
>
> I am using Tomcat5.
> I have a simple jsp displaying the header information that I have set in
> another jsp page. For some reason I can see all the default headers except
> the headers that I have set in this jsp. I am doing a jsp:forward to go to
> the next page which displays the header information ( I have also tried
> redirect - but just wont work)...
>
>
> ------------------------------------------------------------------------------------------------------------
> ------------------------------------------
> index.jsp----------------------------------------------------
>
> ------------------------------------------------------------------------------------------------------------
>
> 
> >  @author Anoop Kumar V.
>          Date: Jan 19, 2006 4:13:05 PM
> --%>
> 
> 
> 
> 
> 
>
> 

> >       System.out.println("Jsp called successfully");
>
>       response.addHeader("userName","GSE_FMS");
>       response.setHeader("userName","GSE_FMS");  //just to be sure.....
>       response.addHeader("myValue","theValue");
>
>       boolean isheader = response.containsHeader("userName");
>
>       System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\ncontains
> userName header ..."+isheader);
>       System.out.println("contains myValue header ..." +
> response.containsHeader("myValue"));
>       //response.sendRedirect("/TestHeaderResponse.jsp");
>
>   %>
> 
> 
> 
>
> ------------------------------------------------------------------------------------------------------------
> --------------------------
> TestHeaderResponse.jsp----------------------------------------------
>
> ------------------------------------------------------------------------------------------------------------
> 
> >  Created for Sikorsky GSE
>  @author Anoop Kumar V.
>          Date: Jan 19, 2006 4:13:05 PM
> --%>
> 
> 
> 
> 
> 
> 

> >       System.out.println("Test header response page called successfully");
>
>
>       String s = request.getHeader("userName");
>       System.out.println("user: "+s);           // Issue: always returns
> null even though set in the previous page
>       out.println("USERNAME HEADER: " + s +"
");
>
>       Enumeration headernames = request.getHeaderNames();
>       System.out.println("Headers---------");
>       while (headernames.hasMoreElements()){
>           String headername = (String)headernames.nextElement();
>           String headervalue = request.getHeader(headername);
>           System.out.println(headername+"----"+headervalue);
>           out.println("    " + headername+"----"+headervalue + "
");
>           out.flush();
>     }
>   %>
> 
> 
>
> ------------------------------------------------------------------------------------------------------------
>
> I think I am doing something fundamentally wrong....
>
> Also can someone help me with understanding how the setHeader and
> getHeader works - we use response.setHeader to set but use
> request.getHeader to read the header values. Is it that the response of
> the first page becomes the request of the next jsp....
> --
> Thanks and best regards,
> Anoop
>
>


--
Thanks and best regards,
Anoop


		
---------------------------------
Relax. Yahoo! Mail virus scanning helps detect nasty viruses!

Re: request.getHeader() gives null

Posted by Anoop kumar V <an...@gmail.com>.
I would really appreciate if someone can answer my question..

Thanks,
Anoop

On 2/3/06, Anoop kumar V <an...@gmail.com> wrote:
>
> HI All,
>
> I am using Tomcat5.
> I have a simple jsp displaying the header information that I have set in
> another jsp page. For some reason I can see all the default headers except
> the headers that I have set in this jsp. I am doing a jsp:forward to go to
> the next page which displays the header information ( I have also tried
> redirect - but just wont work)...
>
>
> ------------------------------------------------------------------------------------------------------------
> ------------------------------------------
> index.jsp----------------------------------------------------
>
> ------------------------------------------------------------------------------------------------------------
>
> <%@ page import="java.io.PrintWriter"%>
> <%--
>  @author <a href="mailto: akumar@iditarodsys.com">Anoop Kumar V.</a>
>          Date: Jan 19, 2006 4:13:05 PM
> --%>
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html>
>   <head><title>Simple jsp page to show header values</title>
>   </head>
>   <body>
>
>   <br/>
>   <%
>       System.out.println("Jsp called successfully");
>
>       response.addHeader("userName","GSE_FMS");
>       response.setHeader("userName","GSE_FMS");  //just to be sure.....
>       response.addHeader("myValue","theValue");
>
>       boolean isheader = response.containsHeader("userName");
>
>       System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\ncontains
> userName header ..."+isheader);
>       System.out.println("contains myValue header ..." +
> response.containsHeader("myValue"));
>       //response.sendRedirect("/TestHeaderResponse.jsp");
>
>   %>
>   <jsp:forward page="/TestHeaderResponse.jsp"/>
>   </body>
> </html>
>
> ------------------------------------------------------------------------------------------------------------
> --------------------------
> TestHeaderResponse.jsp----------------------------------------------
>
> ------------------------------------------------------------------------------------------------------------
> <%@ page import="java.util.Enumeration"%>
> <%--
>  Created for Sikorsky GSE
>  @author <a href="mailto: akumar@iditarodsys.com">Anoop Kumar V.</a>
>          Date: Jan 19, 2006 4:13:05 PM
> --%>
> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
> <html>
>   <head><title>Simple jsp page to show header values</title>
>   </head>
>   <body>
>   <br/>
>   <%
>       System.out.println("Test header response page called successfully");
>
>
>       String s = request.getHeader("userName");
>       System.out.println("user: "+s);           // Issue: always returns
> null even though set in the previous page
>       out.println("USERNAME HEADER: " + s +"<br/>");
>
>       Enumeration headernames = request.getHeaderNames();
>       System.out.println("Headers---------");
>       while (headernames.hasMoreElements()){
>           String headername = (String)headernames.nextElement();
>           String headervalue = request.getHeader(headername);
>           System.out.println(headername+"----"+headervalue);
>           out.println("    " + headername+"----"+headervalue + "<br/>");
>           out.flush();
>     }
>   %>
>   </body>
> </html>
>
> ------------------------------------------------------------------------------------------------------------
>
> I think I am doing something fundamentally wrong....
>
> Also can someone help me with understanding how the setHeader and
> getHeader works - we use response.setHeader to set but use
> request.getHeader to read the header values. Is it that the response of
> the first page becomes the request of the next jsp....
> --
> Thanks and best regards,
> Anoop
>
>


--
Thanks and best regards,
Anoop