How to detect that a browser don't accept cookie from a servlet

Hello,how can my servlet or jsp detect that the distant browser doesn't accept cookie ?In this case, I want to display an error message.Is there a API to do that ?Thanks in advance.Jean
[236 byte] By [jdaguet] at [2007-9-26 6:41:24]
# 1

That is a good question....

I can think of two ways:

1) Set up your browser to deny cookies and then upload to a servlet that calls addCookie() . See if an Exception is thrown and, if so, just key off that Exception- in other words, catch that Exception and forward them to a page that explains they need to enable cookies. I'm not sure if this approach would work since addCookie() throws no Exceptions- but I suppose that it will depend upon how it is implemented in your server.

2) write a javascript or java routine that adds a cookie and then checks to see if the cookie is immediately present. You can forward the user to the proper page based upon its availablity. That routine might be process-intensive for every page, but hopefully you would only need to complete it once, at login- same with suggestion number one.... ...take care. If you find easier solutions, please post it.

rvflannery at 2007-7-1 16:00:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

What do u think about this method

I think this is the most easy to deploy

public class myServlet extends

HttpServlet {

public void doGet(request HttpServletRequest, response HttpServletResponse) {

String result = request.getParameter("testcookie");

if (result!=null) {

if ("true".equalsIgnoreCase(result)) {

Cookie C2 = request.getCookie();

if (C2==null) {

System.out.println("cookies not allowed");

} else {

System.out.println("cookies allowed");

}

}

} else {

Cookie C = new Cookie();

C.setValue("essai de cookie");

response.addCookie(C);

response.sendRedirect("/myservlet?testcookie=true");

}

}

}

jdaguet at 2007-7-1 16:00:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...