HttpUtil.java |
/* PCS - A Framework For Java Web Applications Copyright (C) 2002 Patrick Carl, patrick.carl@web.de This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA $Id: HttpUtil.java,v 1.2 2003/01/08 02:33:09 pcs_org Exp $ */ package de.spieleck.pcs.http; import de.spieleck.pcs.Constants; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; /** * Utility class for programming servlets<br> * @author Patrick Carl */ public class HttpUtil implements Constants{ /** * extracts the parameters of the given request and returns them in a * Hashtable * param request the HttpServletRequest to extract parameters from * @return a Hashtable containig the parameters of the request or */ public static Hashtable getParameters(HttpServletRequest request){ Hashtable h = new Hashtable(); if(request.getMethod().equalsIgnoreCase("GET")){ try{ String query = request.getQueryString(); if(query != null && query.length() > 0) h = HttpUtil.tokenize(query); } catch(IllegalArgumentException ie){ ie.printStackTrace(System.err); } } else{ Enumeration params = request.getParameterNames(); String name = null; String value = null; while(params.hasMoreElements()) { name = (String) params.nextElement(); value = request.getParameter(name); h.put(name, value); } } h.put(SESSION_AS_PARAMETER_NAME, request.getSession()); return h; } /** * parses the given HTTP query String and extracts all parameters and * returns them in a Hashtable with mapping key and value. All keys and * values are Strings. */ private static Hashtable tokenize(String query){ Hashtable h = new Hashtable(); if(query == null || query.length() == 0) return h; StringTokenizer st = new StringTokenizer(query, "?=&"); String key = null; String val = null; while(st.hasMoreTokens()){ key = st.nextToken(); if(st.hasMoreTokens()) val = st.nextToken(); if(val != null) h.put(key, val); } return h; } }
HttpUtil.java |