package levik.servlet.util;

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletUtil {
    public static String getCookieValue(HttpServletRequest request, String name) {
	Cookie cookie = getCookie(request,name);
	if (cookie!=null)
	    return cookie.getValue();
	return null;
    }

    public static Cookie getCookie(HttpServletRequest request, String name) {
	Cookie [] cookies = request.getCookies();
	for (int i=0; i<cookies.length; i++) {
	    if (cookies[i].getName().equals(name))
		return cookies[i];
	}
	return null;
    }

    public static int getIntValueFromCookie(HttpServletRequest request, String name, int def) {
	try {
	    String value = getCookieValue(request,name);
	    return Integer.parseInt(value);
	}
	catch (Exception e) {
	    return def;
	}
    }
}
