Akhilesh
Akhilesh

Reputation: 67

Set Cookie in HttpServletRequest

is there any way to add cookie to HttpServletRequest

Please help me..

i have tried this. but its not working

   HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String cookie =   request.getHeader(HttpHeader.AUTHORIZATION.asString());
    HttpRequest httpRequest = new HttpRequest().setRequest(request);
    String authCookie = String.format("%s=%s", session_id, cookie );
    ServletRequest clientRequest = httpRequest.getRequest();
     httpRequest.setCookies(authCookie );

Upvotes: 1

Views: 28899

Answers (2)

Bence Dergez
Bence Dergez

Reputation: 323

The only way I found is to create a custom request wrapper extending HttpServletRequestWrapper

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.util.Arrays;
import java.util.List;

public class CustomRequestWrapper extends HttpServletRequestWrapper {

    private List<Cookie> cookies;

    public CustomRequestWrapper(HttpServletRequest request) {
        super(request);
        this.cookies = Arrays.asList(request.getCookies());
    }

    @Override
    public Cookie[] getCookies() {
        return this.cookies.toArray(new Cookie[0]);
    }

    public void addCookie(Cookie cookie) {
        this.cookies.add(cookie);
    }
}

This way you can easily remove or add cookies to the List in the class, and the requests getCookies() method will return your modified list of cookies. Note, that despite cookies have a separate getCookies() method, they are just http headers in the end, so the getHeaders() method won't return your modified cookie list in the Cookie header. If you want getHeaders() to work, you have to override that method as well.

Upvotes: 0

Yan Khonski
Yan Khonski

Reputation: 13083

I sent cookies in response. This is how I did it:

String contextPath = request.getContextPath();//We need this path to set cookie's path.
Cookie [] cookies = request.getCookies();
Cookie cookieToProcess = null;
for (Cookie cookie : cookies) {
    //Search cookie you need.
    if ("you-cookie-name".equals(cookie.getName())  && "your-coocie-path".equals(cookie.getPath())) {
        cookieToProcess = cookie;
        break;
    }
}
if (cookieToProcess == null) {
    //No such cookie. 
    //Possibly user enters your site for the first time or they disabled cookies.
    //In this case we create a new cookie.
    String cookieName = "your-cookie-name";
    String cookieValue = "your-cookie-value";
    Cookie newCookie = new Cookie(cookieName, cookieValue);
    newCookie.setPath(contextPath);
    response.addCookie(newCookie);
} else {
    String cookieValue = cookieToProcess.getValue();//Retrieve value from the cookie.
}

If you want to redirect or forward your request to the next jsp, servlet, etc, add request attribute see Difference between getAttribute() and getParameter()

Upvotes: 7

Related Questions