Hey!
Try this:
Cookie cookie;
for (int i = 0; i < cookies.length; i++) {
cookie = new Cookie(Mycookies[i].getName(), Mycookies[i].getValue());
cookie.setMaxAge(0);
response.addCookie(cookie);
}
I cut this from some working code that I have. I hope that it helps and I hope
I
didn't leave anything out.
Basically, you need to rewrite the Cookie to setMaxAge(0).
Danny Rubis
Yogesh Agarwal wrote:
> Hi all,
> I am writing a servlet which uses cookies to identify a user
> on a web site. But I want the user to have an option of deleting
> the cookie if a new user comes in. Somehow, I am not able to delete
> the cookie for which I wrote the following code which is executed
> every time the 'change user' link is clicked.
> Please help me out as where am I going wrong or what might
> be the possible mistakes.
>
> Thanx in advance,
>
> Yogesh Agarwal
>
> ####################################################################
> import javax.servlet.*;
> import javax.servlet.http.*;
> import java.io.*;
> import java.sql.*;
>
> public class ChangeUser extends HttpServlet{
>
> public void init(ServletConfig cfg) throws ServletException
> {
> super.init(cfg);
> }
>
> public void doGet(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException
> {
> res.setContentType("text/html");
> PrintWriter pw = res.getWriter();
>
> // Read Cookies from the client browser
> Cookie[] Mycookies = req.getCookies();
> String UserName=null;
> String UserId=null;
> if(Mycookies != null)
> {
> for (int i=0; i < Mycookies.length; i++)
> {
> Cookie thisCookie = Mycookies[i];
> UserName= thisCookie.getName();
> UserId = thisCookie.getValue();
> if( UserName.equals("UserCookie"))
> //Cookie found
> {
> thisCookie.setMaxAge(0);
> break;
> }
> }
> }
> res.sendRedirect("myhomepage");
> }
>
> public void doPost(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException
> {
> doGet(req,res);
> }
> }
>
> ____________________________________________________________________
> Get free email and a permanent address at http://www.netaddress.com/?N=1
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html