Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookie handling

Status
Not open for further replies.

sqoti

Programmer
Dec 1, 2000
50
US
Is there a way to parse out values from a cookie using java(not javascript). For example cookie = Name = "CookieName" and Value = "Herb,/docs/sample/file/". I want to use java to take cookie and value and split it by delimiting the comma. Hence I would have two value to work with
Herb and /docs/sample/file. I have been looking and can't find an example of this is java.

Thanks in advance!!!
 
you can use the StringTokenizer class to parse the tokens or even use the 1.4 additional java.lang.String classes split(..) methods which take regular expressions and return arrays.

-pete
 
Are you writing a desktop application to check cookies coming from a site or a servlet(or JSP page) to check cookies coming from a web browser?

If you're writing a desktop app heres a chunk of code I've used before:
Code:
import java.net.*;
import java.io.*;
import java.util.*;

public class CookieSplitter {

	private static final String cookieName = "Name of cookie to find";

	public static void proxy(){
		// if you need to set up a proxy for your connection
		Properties systemProperties = System.getProperties();
		systemProperties.setProperty("http.proxyHost","your proxy");
		systemProperties.setProperty("http.proxyPort","port to connect to on the proxy");
	}

	public static void main(String[] args){
		try{
			// if you use a proxy
			proxy();

			String[] cookies = null;

			// just grabbing the url from the args for this example
			URL url = new URL(args[0]);
			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			connection.connect();
			InputStream in = connection.getInputStream();
			Iterator iterator = connection.getHeaderFields().entrySet().iterator();
			if(iterator != null){
				while(iterator.hasNext()){
					String headerString = iterator.next().toString();
					int beginIndex;
					if((beginIndex = headerString.indexOf(cookieName)) != -1){
						// usually cookies take the form name=value;
						// so to get the value for this cookie move the beginIndex along
						// cookieName.length()+1 spaces
						String s = headerString.substring(beginIndex+cookieName.length()+1);
						// and split this new string on the first ';' in case there
						// is more than one cookie in the header
						s = s.split(";")[0];
						// now s will be the "Herb,/docs/sample/file/" given in your example
						// so to split that it's a simple
						cookies = s.split(",");
						break;
					}
				}
			}

			if(cookies != null && cookies.length == 2){
				// use the 2 cookies you were looking for!
			}

			in.close();
			connection.disconnect();
		}catch(MalformedURLException mfuex){
			mfuex.printStackTrace();
		}catch(IOException ioex){
			ioex.printStackTrace();
		}
	}
}

Messy, I know, but it works!!

If you're writing serverside try just do a:

Code:
Cookie[] cookies = request.getCookies();

and parse these (not gonna write the full serlvet/jsp 4 u ;-))

hope this helps.

P.S. The 'h's in HttpURLConnection should be uppercase, won't go through properly for some reason!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top