Java Code Examples for javax.servlet.ServletRequest#getParameterNames()

The following examples show how to use javax.servlet.ServletRequest#getParameterNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Return a map containing all parameters with the given prefix.
 * Maps single values to String and multiple values to String array.
 * <p>For example, with a prefix of "spring_", "spring_param1" and
 * "spring_param2" result in a Map with "param1" and "param2" as keys.
 * @param request the HTTP request in which to look for parameters
 * @param prefix the beginning of parameter names
 * (if this is null or the empty string, all parameters will match)
 * @return map containing request parameters <b>without the prefix</b>,
 * containing either a String or a String array as values
 * @see javax.servlet.ServletRequest#getParameterNames
 * @see javax.servlet.ServletRequest#getParameterValues
 * @see javax.servlet.ServletRequest#getParameterMap
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, @Nullable String prefix) {
	Assert.notNull(request, "Request must not be null");
	Enumeration<String> paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<>();
	if (prefix == null) {
		prefix = "";
	}
	while (paramNames != null && paramNames.hasMoreElements()) {
		String paramName = paramNames.nextElement();
		if (prefix.isEmpty() || paramName.startsWith(prefix)) {
			String unprefixed = paramName.substring(prefix.length());
			String[] values = request.getParameterValues(paramName);
			if (values == null || values.length == 0) {
				// Do nothing, no values found at all.
			}
			else if (values.length > 1) {
				params.put(unprefixed, values);
			}
			else {
				params.put(unprefixed, values[0]);
			}
		}
	}
	return params;
}
 
Example 2
Source File: ServletUtils.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a encoded URL query string with the parameters from the given request. If the
 * request is a GET, then the returned query string will simply consist of the query
 * string from the request. If the request is a POST, the returned query string will
 * consist of the form variables.
 * <p>
 * <strong>Note</strong>: This method does not support multi-value parameters.
 *
 * @param request The request for which the query string will be generated.
 *
 * @return An encoded URL query string with the parameters from the given request.
 */
public static String requestParamsToQueryString(ServletRequest request) {

    StringBuffer queryString = new StringBuffer();

    String paramName = null;
    String paramValue = null;

    Enumeration paramNames = request.getParameterNames();

    while (paramNames.hasMoreElements()) {
        paramName = (String)paramNames.nextElement();
        paramValue = request.getParameter(paramName);

        queryString.append(encode(paramName)).append("=").append(encode(paramValue))
                .append("&");
    }

    if (endsWith(queryString, '&')) {
        queryString.deleteCharAt(queryString.length() - 1);
    }

    return queryString.toString();
}
 
Example 3
Source File: WebUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Return a map containing all parameters with the given prefix.
 * Maps single values to String and multiple values to String array.
 * <p>For example, with a prefix of "spring_", "spring_param1" and
 * "spring_param2" result in a Map with "param1" and "param2" as keys.
 * @param request HTTP request in which to look for parameters
 * @param prefix the beginning of parameter names
 * (if this is null or the empty string, all parameters will match)
 * @return map containing request parameters <b>without the prefix</b>,
 * containing either a String or a String array as values
 * @see javax.servlet.ServletRequest#getParameterNames
 * @see javax.servlet.ServletRequest#getParameterValues
 * @see javax.servlet.ServletRequest#getParameterMap
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
	Assert.notNull(request, "Request must not be null");
	Enumeration<String> paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<String, Object>();
	if (prefix == null) {
		prefix = "";
	}
	while (paramNames != null && paramNames.hasMoreElements()) {
		String paramName = paramNames.nextElement();
		if ("".equals(prefix) || paramName.startsWith(prefix)) {
			String unprefixed = paramName.substring(prefix.length());
			String[] values = request.getParameterValues(paramName);
			if (values == null || values.length == 0) {
				// Do nothing, no values found at all.
			}
			else if (values.length > 1) {
				params.put(unprefixed, values);
			}
			else {
				params.put(unprefixed, values[0]);
			}
		}
	}
	return params;
}
 
Example 4
Source File: Servlets.java    From dpCms with Apache License 2.0 6 votes vote down vote up
/**
 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
 * 
 * 返回的结果的Parameter名已去除前缀.
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
	Validate.notNull(request, "Request must not be null");
	Enumeration paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<String, Object>();
	if (prefix == null) {
		prefix = "";
	}
	if((paramNames != null)) {
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			if ("".equals(prefix) || paramName.startsWith(prefix)) {
				String unprefixed = paramName.substring(prefix.length());
				String[] values = request.getParameterValues(paramName);
				if ((values == null) || (values.length == 0)) {
					// Do nothing, no values found at all.
				} else if (values.length > 1) {
					params.put(unprefixed, values);
				} else {
					params.put(unprefixed, values[0]);
				}
			}
		}
	}
	return params;
}
 
Example 5
Source File: Servlets.java    From spring-boot-quickstart with Apache License 2.0 6 votes vote down vote up
/**
 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
 * 
 * 返回的结果的Parameter名已去除前缀.
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
	Validate.notNull(request, "Request must not be null");
	Enumeration paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<String, Object>();
	if (prefix == null) {
		prefix = "";
	}
	while ((paramNames != null) && paramNames.hasMoreElements()) {
		String paramName = (String) paramNames.nextElement();
		if ("".equals(prefix) || paramName.startsWith(prefix)) {
			String unprefixed = paramName.substring(prefix.length());
			String[] values = request.getParameterValues(paramName);
			if ((values == null) || (values.length == 0)) {
				// Do nothing, no values found at all.
			} else if (values.length > 1) {
				params.put(unprefixed, values);
			} else {
				params.put(unprefixed, values[0]);
			}
		}
	}
	return params;
}
 
Example 6
Source File: WebUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the target page specified in the request.
 * @param request current servlet request
 * @param paramPrefix the parameter prefix to check for
 * (e.g. "_target" for parameters like "_target1" or "_target2")
 * @param currentPage the current page, to be returned as fallback
 * if no target page specified
 * @return the page specified in the request, or current page if not found
 * @deprecated as of Spring 4.3.2, in favor of custom code for such purposes
 */
@Deprecated
public static int getTargetPage(ServletRequest request, String paramPrefix, int currentPage) {
	Enumeration<String> paramNames = request.getParameterNames();
	while (paramNames.hasMoreElements()) {
		String paramName = paramNames.nextElement();
		if (paramName.startsWith(paramPrefix)) {
			for (int i = 0; i < WebUtils.SUBMIT_IMAGE_SUFFIXES.length; i++) {
				String suffix = WebUtils.SUBMIT_IMAGE_SUFFIXES[i];
				if (paramName.endsWith(suffix)) {
					paramName = paramName.substring(0, paramName.length() - suffix.length());
				}
			}
			return Integer.parseInt(paramName.substring(paramPrefix.length()));
		}
	}
	return currentPage;
}
 
Example 7
Source File: WebUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return a map containing all parameters with the given prefix.
 * Maps single values to String and multiple values to String array.
 * <p>For example, with a prefix of "spring_", "spring_param1" and
 * "spring_param2" result in a Map with "param1" and "param2" as keys.
 * @param request HTTP request in which to look for parameters
 * @param prefix the beginning of parameter names
 * (if this is null or the empty string, all parameters will match)
 * @return map containing request parameters <b>without the prefix</b>,
 * containing either a String or a String array as values
 * @see javax.servlet.ServletRequest#getParameterNames
 * @see javax.servlet.ServletRequest#getParameterValues
 * @see javax.servlet.ServletRequest#getParameterMap
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
	Assert.notNull(request, "Request must not be null");
	Enumeration<String> paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<String, Object>();
	if (prefix == null) {
		prefix = "";
	}
	while (paramNames != null && paramNames.hasMoreElements()) {
		String paramName = paramNames.nextElement();
		if ("".equals(prefix) || paramName.startsWith(prefix)) {
			String unprefixed = paramName.substring(prefix.length());
			String[] values = request.getParameterValues(paramName);
			if (values == null || values.length == 0) {
				// Do nothing, no values found at all.
			}
			else if (values.length > 1) {
				params.put(unprefixed, values);
			}
			else {
				params.put(unprefixed, values[0]);
			}
		}
	}
	return params;
}
 
Example 8
Source File: StatelessFilter.java    From jsets-shiro-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
protected AuthenticationToken createHmacToken(ServletRequest request, ServletResponse response) {
	
	String appId = request.getParameter(ShiroProperties.PARAM_HMAC_APP_ID);
	String timestamp = request.getParameter(ShiroProperties.PARAM_HMAC_TIMESTAMP);
	String digest= request.getParameter(ShiroProperties.PARAM_HMAC_DIGEST);
	List<String> parameterNames = Lists.newLinkedList();
	Enumeration<String> namesEnumeration = request.getParameterNames();
	while(namesEnumeration.hasMoreElements()){
           String parameterName = namesEnumeration.nextElement();
           parameterNames.add(parameterName);
       }
	StringBuilder baseString = new StringBuilder();
	parameterNames.stream()
		.sorted()
		.forEach(name -> {
			if(!ShiroProperties.PARAM_HMAC_APP_ID.equals(name)
				&&!ShiroProperties.PARAM_HMAC_TIMESTAMP.equals(name)
				&&!ShiroProperties.PARAM_HMAC_DIGEST.equals(name))
				baseString.append(request.getParameter(name));
	});
	baseString.append(appId);
	baseString.append(timestamp);
	String host = request.getRemoteHost();
	return new HmacToken( host, appId, timestamp, baseString.toString(), digest);
}
 
Example 9
Source File: ServletUtils.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a encoded URL query string with the parameters from the given request. If the
 * request is a GET, then the returned query string will simply consist of the query
 * string from the request. If the request is a POST, the returned query string will
 * consist of the form variables.
 * <p>
 * <strong>Note</strong>: This method does not support multi-value parameters.
 *
 * @param request The request for which the query string will be generated.
 *
 * @return An encoded URL query string with the parameters from the given request.
 */
public static String requestParamsToQueryString(ServletRequest request) {

    StringBuffer queryString = new StringBuffer();

    String paramName = null;
    String paramValue = null;

    Enumeration paramNames = request.getParameterNames();

    while (paramNames.hasMoreElements()) {
        paramName = (String)paramNames.nextElement();
        paramValue = request.getParameter(paramName);

        queryString.append(encode(paramName)).append("=").append(encode(paramValue))
                .append("&");
    }

    if (endsWith(queryString, '&')) {
        queryString.deleteCharAt(queryString.length() - 1);
    }

    return queryString.toString();
}
 
Example 10
Source File: WebUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Return a map containing all parameters with the given prefix.
 * Maps single values to String and multiple values to String array.
 * <p>For example, with a prefix of "spring_", "spring_param1" and
 * "spring_param2" result in a Map with "param1" and "param2" as keys.
 * @param request the HTTP request in which to look for parameters
 * @param prefix the beginning of parameter names
 * (if this is null or the empty string, all parameters will match)
 * @return map containing request parameters <b>without the prefix</b>,
 * containing either a String or a String array as values
 * @see javax.servlet.ServletRequest#getParameterNames
 * @see javax.servlet.ServletRequest#getParameterValues
 * @see javax.servlet.ServletRequest#getParameterMap
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, @Nullable String prefix) {
	Assert.notNull(request, "Request must not be null");
	Enumeration<String> paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<>();
	if (prefix == null) {
		prefix = "";
	}
	while (paramNames != null && paramNames.hasMoreElements()) {
		String paramName = paramNames.nextElement();
		if ("".equals(prefix) || paramName.startsWith(prefix)) {
			String unprefixed = paramName.substring(prefix.length());
			String[] values = request.getParameterValues(paramName);
			if (values == null || values.length == 0) {
				// Do nothing, no values found at all.
			}
			else if (values.length > 1) {
				params.put(unprefixed, values);
			}
			else {
				params.put(unprefixed, values[0]);
			}
		}
	}
	return params;
}
 
Example 11
Source File: ServletUtils.java    From datax-web with MIT License 6 votes vote down vote up
/**
 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
 * 返回的结果的Parameter名已去除前缀.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
    Enumeration paramNames = request.getParameterNames();
    Map<String, Object> params = new TreeMap<String, Object>();
    String pre = prefix;
    if (pre == null) {
        pre = "";
    }
    while (paramNames != null && paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if ("".equals(pre) || paramName.startsWith(pre)) {
            String unprefixed = paramName.substring(pre.length());
            String[] values = request.getParameterValues(paramName);
            if (values == null || values.length == 0) {
                values = new String[]{};
                // Do nothing, no values found at all.
            } else if (values.length > 1) {
                params.put(unprefixed, values);
            } else {
                params.put(unprefixed, values[0]);
            }
        }
    }
    return params;
}
 
Example 12
Source File: ApsRequestParamsUtil.java    From entando-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static String[] getApsParams(String paramPrefix, String separator, ServletRequest request) {
	String[] apsParams = null;
	Enumeration params = request.getParameterNames();
       while (params.hasMoreElements()) {
       	String pname = (String) params.nextElement();
       	if (pname.startsWith(ACTION_PREFIX)) {
       		pname = pname.substring(ACTION_PREFIX.length());
       	}
		if (pname.startsWith(ENTANDO_ACTION_PREFIX)) {
       		pname = pname.substring(ENTANDO_ACTION_PREFIX.length());
       	}
       	if (pname.startsWith(paramPrefix)) {
       		apsParams = splitParam(pname, separator);
       		break;
       	}
       }
       return apsParams;
}
 
Example 13
Source File: WebUtil.java    From hdw-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
 * <p>
 * 返回的结果的Parameter名已去除前缀.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> getParametersWith(ServletRequest request, String prefix) {
    Assert.notNull(request, "Request must not be null");
    Enumeration paramNames = request.getParameterNames();
    Map<String, Object> params = new TreeMap<String, Object>();
    String pre = prefix;
    if (pre == null) {
        pre = "";
    }
    while (paramNames != null && paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if ("".equals(pre) || paramName.startsWith(pre)) {
            String unprefixed = paramName.substring(pre.length());
            String[] values = request.getParameterValues(paramName);
            if (values == null || values.length == 0) {
                values = new String[]{};
                // Do nothing, no values found at all.
            } else if (values.length > 1) {
                params.put(unprefixed, values);
            } else {
                params.put(unprefixed, values[0]);
            }
        }
    }
    return params;
}
 
Example 14
Source File: JspRuntimeLibrary.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public static void introspect(Object bean, ServletRequest request)
                              throws JasperException
{
    Enumeration<String> e = request.getParameterNames();
    while ( e.hasMoreElements() ) {
        String name  = e.nextElement();
        String value = request.getParameter(name);
        introspecthelper(bean, name, value, request, name, true);
    }
}
 
Example 15
Source File: ListTagUtil.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a link containing the URL + ALL the parameters of the
 * request query string minus the sort links, and the alpha link +
 * the additional params passed in the paramsToAdd map. The resulting
 * string is urlEncode'd with {@literal & encoded as &amp;} so it can be used
 * in a href directly.
 *
 * @param request the Servlet Request
 * @param listName the current list name
 * @param paramsToAdd the params you might want to append to the url
 *                  for example makeSortLink passes in sortByLabel
 *                  while alpha bar passes in params that are specific to it.
 *  @param paramsToIgnore params to not include that would be normally
 * @return a link containing the URL  + (OtherParams - Sort - Alpha) + paramsToAdd
 */
public static String makeParamsLink(ServletRequest request,
                                    String listName,
                                    Map<String, String> paramsToAdd,
                                    List<String> paramsToIgnore) {
    String url = (String) request.getAttribute(ListTagHelper.PARENT_URL);
    String sortByLabel = makeSortByLabel(listName);
    String sortByDir =   makeSortDirLabel(listName);
    String alphaKey =   AlphaBarHelper.makeAlphaKey(listName);
    StringBuilder params = new StringBuilder();
    if (url.indexOf('?') < 0) {
        params.append("?");
    }
    else if (url.indexOf('?') != url.length() - 1) {
        url += "&amp;";
    }

    for (Enumeration<String> en = request.getParameterNames(); en.hasMoreElements();) {
        String paramName = en.nextElement();
        if (!sortByLabel.equals(paramName) && !sortByDir.equals(paramName) &&
                !alphaKey.equals(paramName) && !paramsToIgnore.contains(paramName)) {
            if (params.length() > 1) {
                params.append("&amp;");
            }
            params.append(StringUtil.urlEncode(paramName)).append("=")
                        .append(StringUtil.urlEncode(request.getParameter(paramName)));
        }
    }
    for (String key : paramsToAdd.keySet()) {
        if (params.length() > 1) {
            params.append("&amp;");
        }
        params.append(StringUtil.urlEncode(key)).append("=")
                    .append(StringUtil.urlEncode(paramsToAdd.get(key)));
    }

    return url + params.toString();
}
 
Example 16
Source File: JspRuntimeLibrary.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public static void introspect(Object bean, ServletRequest request)
                              throws JasperException
{
    Enumeration<String> e = request.getParameterNames();
    while ( e.hasMoreElements() ) {
        String name  = e.nextElement();
        String value = request.getParameter(name);
        introspecthelper(bean, name, value, request, name, true);
    }
}
 
Example 17
Source File: WebUtils.java    From open-cloud with MIT License 5 votes vote down vote up
/**
 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
 * <p>
 * 返回的结果的Parameter名已去除前缀.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> getParametersWith(ServletRequest request, String prefix) {
    Assert.notNull(request, "Request must not be null");
    Enumeration paramNames = request.getParameterNames();
    Map<String, Object> params = new TreeMap<String, Object>();
    String pre = prefix;
    if (pre == null) {
        pre = "";
    }
    while (paramNames != null && paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if ("".equals(pre) || paramName.startsWith(pre)) {
            String unprefixed = paramName.substring(pre.length());
            String[] values = request.getParameterValues(paramName);
            if (values == null || values.length == 0) {
                values = new String[]{};
                // Do nothing, no values found at all.
            } else if (values.length > 1) {
                params.put(unprefixed, values);
            } else {
                params.put(unprefixed, values[0]);
            }
        }
    }
    return params;
}
 
Example 18
Source File: ServletUtils.java    From frpMgr with MIT License 5 votes vote down vote up
/**
 * 取得带相同前缀的Request Parameters, copy from spring WebUtils.
 * 返回的结果的Parameter名已去除前缀.
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
	Validate.notNull(request, "Request must not be null");
	Enumeration paramNames = request.getParameterNames();
	Map<String, Object> params = new TreeMap<String, Object>();
	String pre = prefix;
	if (pre == null) {
		pre = "";
	}
	while (paramNames != null && paramNames.hasMoreElements()) {
		String paramName = (String) paramNames.nextElement();
		if ("".equals(pre) || paramName.startsWith(pre)) {
			String unprefixed = paramName.substring(pre.length());
			String[] values = request.getParameterValues(paramName);
			if (values == null || values.length == 0) {
				values = new String[]{};
				// Do nothing, no values found at all.
			} else if (values.length > 1) {
				params.put(unprefixed, values);
			} else {
				params.put(unprefixed, values[0]);
			}
		}
	}
	return params;
}
 
Example 19
Source File: AgnUtils.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Getter for property parameterMap.
 *
 * @return Value of property parameterMap.
 */
public static Map<String, String> getRequestParameterMap(ServletRequest req) {
	Map<String, String> parameterMap = new HashMap<>();
	Enumeration<String> e = req.getParameterNames();
	while (e.hasMoreElements()) {
		String parameterName = e.nextElement();
		String parameterValue = req.getParameter(parameterName);
		parameterMap.put(parameterName, parameterValue);
	}
	return parameterMap;
}
 
Example 20
Source File: ServletRequestHandler.java    From commons-jxpath with Apache License 2.0 5 votes vote down vote up
protected void collectPropertyNames(HashSet set, Object bean) {
    super.collectPropertyNames(set, bean);
    ServletRequestAndContext handle = (ServletRequestAndContext) bean;
    ServletRequest servletRequest = handle.getServletRequest();
    Enumeration e = servletRequest.getAttributeNames();
    while (e.hasMoreElements()) {
        set.add(e.nextElement());
    }
    e = servletRequest.getParameterNames();
    while (e.hasMoreElements()) {
        set.add(e.nextElement());
    }
}