1

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 );
Akhilesh
  • 67
  • 1
  • 1
  • 5

2 Answers2

7

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()

Community
  • 1
  • 1
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
0

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.

Bence Dergez
  • 313
  • 4
  • 10
  • This won't work as is. `Arrays.asList(request.getCookies())` will return a fixed-size list so `this.cookies.add(cookie)` will fail with an `UnsupportedOperationException`. So use `this.cookies = new ArrayList<>(Arrays.asList(request.getCookies()));` in the constructor instead. – morten.c Dec 14 '22 at 12:29