Code sample below shows how to set cookies.

Java Code:
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res) {
    res.addCookie(new Cookie("mycookie_name", "mycookie_value"));
    }
    
  public void doGet(HttpServletRequest req, HttpServletResponse res) {
    doPost(req, res);
    }
    
}