i think u can set the value of the cookie variable to a new value .. the old
value will not exist.
so when the user clicks the change user link redirect him to the homepage and
let him enter the username . set this value to the cookie variable
let me know if it works out
regards
jaya krishna
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
begin:vcard
n:marada;jaya krishna reddy
tel;home:hyderbad-72
tel;work:CMC LTd
x-mozilla-html:FALSE
adr:;;;;;;
version:2.1
email;internet:[EMAIL PROTECTED]
fn:jaya krishna
end:vcard