Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a header in an HTTP response?

I've a servlet A where I'm setting a header in the HTTP response:

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userName=request.getParameter("userName");
    String newUrl = "http://somehost:port/ServletB";

    response.addHeader("REMOTE_USER", userName);

    response.sendRedirect(newUrl);
}

Now in a servlet B, I'm trying to get the header value that was set in the servlet A:

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userid = request.getHeader("REMOTE_USER");
}


But here the value of userid is coming as null. Please let me know what I'm missing here.

like image 404
Pakira Avatar asked Oct 23 '13 21:10

Pakira


People also ask

Do HTTP responses have headers?

A response header is an HTTP header that can be used in an HTTP response and that doesn't relate to the content of the message. Response headers, like Age , Location or Server are used to give a more detailed context of the response.

What is header in HTTP response?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response. For example, a request message can use headers to indicate it's preferred media formats, while a response can use header to indicate the media format of the returned body.

How do you set a value in response header?

Setting Response Headers from Servlets The most general way to specify headers is to use the setHeader method of HttpServletResponse. This method takes two strings: the header name and the header value. As with setting status codes, you must specify headers before returning the actual document.


2 Answers

First of all you have to understand the nature of

response.sendRedirect(newUrl);

It is giving the client (browser) 302 http code response with an URL. The browser then makes a separate GET request on that URL. And that request has no knowledge of headers in the first one.

So sendRedirect won't work if you need to pass a header from Servlet A to Servlet B.

If you want this code to work - use RequestDispatcher in Servlet A (instead of sendRedirect). Also, it is always better to use relative path.

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
    String userName=request.getParameter("userName");
    String newUrl = "ServletB";
    response.addHeader("REMOTE_USER", userName);
    RequestDispatcher view = request.getRequestDispatcher(newUrl);
    view.forward(request, response);
}

========================

public void doPost(HttpServletRequest request, HttpServletResponse response)
{
    String sss = response.getHeader("REMOTE_USER");
}
like image 158
pubsy Avatar answered Sep 28 '22 05:09

pubsy


In my Controller, I merely added an HttpServletResponse parameter and manually added the headers, no filter or intercept required and it works fine:

httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
httpServletResponse.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, X-Auth-Token, X-Csrf-Token, WWW-Authenticate, Authorization");
httpServletResponse.setHeader("Access-Control-Allow-Credentials", "false");
httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
like image 29
hd1 Avatar answered Sep 28 '22 05:09

hd1