Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0

    Killing a cookie


    How would I kill a cookie using JSP?

    Is it the same as PHP where you just set the expiration to a time in the past/-1?
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Yes
  4. #3
  5. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Well, I have this code, but it's not deleting the cookie:
    Code:
    Cookie[] allCookies = request.getCookies();
    Cookie cookieUid = null;
    int cookieFound = 0;
    
    for(int k=0; k<allCookies.length; k++)
    {
    	cookieUid = allCookies[k];
    	if (allCookies[k].getName().equals("user_id2"))
    	{
    		cookieFound = 1;
    		break;
    	}
    }
    	cookieUid.setMaxAge(-1);
    	response.addCookie(cookieUid);
    Should I be using the name part of the NAME=VALUE pair when setting a cookie?
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    This is from the javadocs:

    A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
    Cookie.setMaxAge()

    I apologize for misleading you, I did not check the javadocs originally either.

    Here is the j2ee javadocs url. I believe I posted a link to the j2se api for you, no?
    http://java.sun.com/j2ee/sdk_1.3/techdocs/api/
    Last edited by Nemi; July 7th, 2003 at 02:55 PM.
  8. #5
  9. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Ahh ok, so it needs to be 0 then?

    And yes, you posted the java docs link, but I couldn't find anything for cookies. Though, I should have searched for the methods themselves. THough, I couldn't find javax.servlet.* where Cookie is stored.
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    THough, I couldn't find javax.servlet.* where Cookie is stored.
    You won't find the javax.servlet.* package, or any package used server-side for servlets/jsp, in the j2se javadocs. They are a part of the J2EE spec and have their own javadocs page. This can be confusing for people new to the language. Bookmark both of those links and you should have all the documentatoni you need for web development. If you can't find it in one, look in the other. Eventually you will figure out what is located where.
  12. #7
  13. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Ok, it's still not deleting the cookie after changing maxAge to 0 and not -1. Here's my code
    Code:
    	cookieUid.setMaxAge(0);
    	response.addCookie(cookieUid);
    Same vars as above post.
  14. #8
  15. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Hmm, I am not sure what to tell you. I have tested it and it works as documented for me.

    You can add the same cookie more than once. Are you inadvertantly adding the same cookie again with a valid date? Can you post your code?
  16. #9
  17. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Unfortunately, I'm home now, so I can't post the code. (it's on my puter at work.)

    I don't think I'm pretty sure i'm not setting it twice as I went into my cookie dir and there was never more than 1 cookie for my IP (domain)

    I'll post the code when i get in work tomorrow.
  18. #10
  19. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Here's the code in it's entirety:
    Code:
    <% // logout.jsp %>
    <%@ page import="java.io.*, java.util.*, java.net.*" %>
    <%@ page import="javax.servlet.http.Cookie" %>
    
    <%@ include file="header.jsp" %>
    <%
    // Request all the client cookies
    Cookie[] allCookies = request.getCookies();
    Cookie cookieUid = null;
    int cookieFound = 0;
    
    for(int k=0; k<allCookies.length; k++)
    {
    	cookieUid = allCookies[k];
    	if (allCookies[k].getName().equals("user_id2"))
    	{
    		cookieFound = 1;
    		break;
    	}
    }
    
    if (session.getValue("user_id2") != null || cookieUid.getValue() != null)
    {
    	String one, two;
    	one = (String)session.getValue("user_id2");
    	two = cookieUid.getValue();
    	out.println("Session: " + one + "<br>Cookie " + two + "<br>");
    	session.invalidate();
    	cookieUid.setMaxAge(0);
    	response.addCookie(cookieUid);
    	out.println("<span class=\"ok\">You have been successfully logged out.<br/><br/></span>");
    	out.println("<span class=\"normaltext\"><a href=\"/infosys/infra/software/database/\">Click here to return to the home page</a></span>");
    	out.println("<br/>Session: " + one + "<br>Cookie " + two + "<br>");
    }
    else
    {
    	out.println("<span class=\"error\">No session or cookie found. You may have reached this page in error</spasn>");
    }
    %>
    <%@ include file="footer.jsp" %>
  20. #11
  21. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    What is in the header anf footer jsp's? Do you set any cookies in there?
  22. #12
  23. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0
    Nope. Other than an if statement in header, they're both pure html.
  24. #13
  25. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Boy, I don't know what to tell you. As far as I can tell the problem is not in this code. I can't see anything at least.
  26. #14
  27. PHP & Java Error Master
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2003
    Location
    My Computer
    Posts
    1,219
    Rep Power
    0


    Maybe I'm not looking for the right cookie as mentioned above. Here's how it's set.
    Warning: lots o code
    Code:
    String do_check = null;
    String sql_1, num, fname, lname, user_id;
    int sub_uid, k;
    
    // The name of the cookie being stored on the client.
    Cookie cookieUid = null;
    
    // If it doesn't exist, use this as the NAME part of NAME=VALUE
    String cookieName = "user_id2";
    int cookieFound = 0;
    int sessionFound = 0;
    
    // This will store the value of the cookie
    String tempString, temp_id;
    
    // Request all the client cookies
    Cookie[] allCookies = request.getCookies();
    
    if(allCookies == null) // No cookies found
    {
    	out.println("Uh oh, no cookies for you!");
    }
    else
    {
    
    	// Go through the array looking for our cookie
    	for(k=0; k<allCookies.length; k++)
    	{
    		cookieUid = allCookies[k];
    		if (allCookies[k].getName().equals("user_id2"))
    		{
    			cookieFound = 1;
    			break;
    		}
    	}
    }
    
    String uid = (String)session.getValue("user_id2"); // Get the session val.
    if((String)session.getValue("user_id2") != null)
    {
    	sessionFound = 1; // If it's not null, then we've got a session.
    }
    // If our cookie is found:
    if(cookieFound == 1 || sessionFound == 1)
    {
    	if(sessionFound == 1 && cookieFound == 0)
    	{
    		// Session is found, no cookie. Set the cookie.
    		String ses_val;
    		ses_val = uid;
    		//ses_val = Integer.toString(uid);
    		Cookie cookie = new Cookie("user_id2", ses_val);
    		cookie.setMaxAge(31536000);
    		cookie.setPath("/");
    		response.addCookie(cookie);
    	}
    	else if(sessionFound == 0 && cookieFound == 1)
    	{
    		//Cookie's found, no session. Set the session.
    		tempString = cookieUid.getValue();
    		//out.println("Temp String is " + tempString + "<br/>");
    		temp_id = tempString;
    		//out.println("Temp id is " + temp_id + "<br/>");
    		session.putValue("user_id2", temp_id);
    	}
    	else
    	{
    		// Cookie and session are found.
    		// This is debug info.
    		/*
    		tempString = cookieUid.getValue();
    		temp_id = (String)session.getValue("user_id2");
    		out.println("SessionFound: = " + sessionFound + " and CookieFound = " + cookieFound + "<br/>");
    		out.println("Cookie val: " + tempString + "<br/>");
    		out.println("Session val: " + temp_id + "<br/>");
    		*/
    	}
    }
    else
    {
    	Connection conn = null;
    
    	if (request.getParameter("check") != null)
    	{
    		do_check = request.getParameter("check");
    	}
    	if (do_check != null)
    	{
    		if(do_check.equals("go"))
    		{
    			try
    			{
    				String db_url = "";
    				String db_user = "";
    				String db_pass = "";
    				Class.forName("");
    
    				conn = DriverManager.getConnection(db_url,db_user,db_pass);
    
    				Statement sql_st = conn.createStatement(); // Create the connection statement
    
    
    				num = request.getParameter("n_num");
    				num = num.toUpperCase();
    
    				// Check n number and access, then set the cookie and session.
    				sql_1 = "SELECT user_id FROM prj.prj_user_t WHERE user_name = '" + num + "'";
    				//out.println("Query1: " + sql_1 + "<br/>");
    
    				ResultSet rs_1 = sql_st.executeQuery(sql_1); // Create result set
    				rs_1.next();
    				sub_uid = rs_1.getInt("user_id");
    
    				sql_1 = "";
    				
    				sql_1 = "SELECT u.user_id, user_lname, user_fname \n";
    				sql_1 = sql_1 + "FROM prj.prj_user_t u, PRJ.PRJ_GRP_T g, PRJ.JN_USER_GRP_T j \n";
    				sql_1 = sql_1 + "WHERE u.user_id = " + sub_uid + "\n";
    				sql_1 = sql_1 + "AND   g.grp_id  = j.grp_id \n";
    				sql_1 = sql_1 + "AND   g.grp_id  = 1\n";
    				sql_1 = sql_1 + "ORDER BY u.user_lname, u.user_fname \n";
    				//out.println("Query2: " + sql_1 + "<br/>");
    
    				rs_1 = sql_st.executeQuery(sql_1);
    				//out.println("<br/>Execution complete<br/>");
    				rs_1.next();
    				//out.println("rs_1.next complete<br/>");
    				user_id = rs_1.getString("user_id");
    				fname = rs_1.getString("user_fname");
    				lname = rs_1.getString("user_lname");
    				//out.println("<br/>" + user_id + "<br/>" + fname + "<br/>" + lname);
    
    				session.setAttribute("user_id2", user_id); // Set the session
    
    				// Set the cookie
    				Cookie cookie = new Cookie("user_id2", user_id);
    				cookie.setMaxAge(31536000);
    				cookie.setPath("/");
    				response.addCookie(cookie);
    				response.sendRedirect("/infosys/infra/software/database/index.jsp");
    
    			//**********************************************************************
    			// Close database connection
    			//**********************************************************************
    			}
    			catch(SQLException e)
    			{
    				out.println("SQLException: " + e.getMessage() + "<BR>");
    				while((e = e.getNextException()) != null)
    					out.println(e.getMessage() + "<BR>");
    			}
    			catch(ClassNotFoundException e)
    			{
    				out.println("ClassNotFoundException: " + e.getMessage() + "<BR>");
    			}
    			finally
    			{
    				//Clean up resources, close the connection.
    				if(conn != null)
    				{
    					try
    					{
    						conn.close();
    					}
    					catch (Exception ignored)
    					{
    						// Nuttin
    					}
    				}
    			}
    		//**********************************************************************
    		}
    	}
      	else
    	{
    		response.sendRedirect("/infosys/infra/software/database/check.jsp");
    	}
    }
    %>
  28. #15
  29. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Running this through the debugger I notice that the cookie retrieved in the logout.jsp has a null path, but you set the path to "/" when created. When I set this to the root dir when trying to remove it, it works. Before that it wouldn't.
    Code:
    <% // logout.jsp %>
    <%@ page import="java.io.*, java.util.*, java.net.*" %>
    <%@ page import="javax.servlet.http.Cookie" %>
    
    <%@ include file="header.jsp" %>
    <%
    // Request all the client cookies
    Cookie[] allCookies = request.getCookies();
    Cookie cookieUid = null;
    int cookieFound = 0;
    
    for(int k=0; k<allCookies.length; k++)
    {
    	cookieUid = allCookies[k];
    	if (allCookies[k].getName().equals("user_id2"))
    	{
    		cookieFound = 1;
    		break;
    	}
    }
    
    if (session.getValue("user_id2") != null || cookieUid.getValue() != null)
    {
    	String one, two;
    	one = (String)session.getValue("user_id2");
    	two = cookieUid.getValue();
    	out.println("Session: " + one + "<br>Cookie " + two + "<br>");
    	session.invalidate();
    	cookieUid.setMaxAge(0);
    //added this line
    	cookieUid.setPath("/");
    	response.addCookie(cookieUid);
    	out.println("<span class=\"ok\">You have been successfully logged out.<br/><br/></span>");
    	out.println("<span class=\"normaltext\"><a href=\"/infosys/infra/software/database/\">Click here to return to the home page</a></span>");
    	out.println("<br/>Session: " + one + "<br>Cookie " + two + "<br>");
    }
    else
    {
    	out.println("<span class=\"error\">No session or cookie found. You may have reached this page in error</spasn>");
    }
    %>
    <%@ include file="footer.jsp" %>
    See if this doesn't work for you also.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo