/** * Get a String parameter, with a fallback value. Never throws an exception. * Can pass a distinguished value to default to enable checks of whether it was supplied. * @param request current HTTP request * @param name the name of the parameter * @param defaultVal the default value to use as fallback */ public static String getStringParameter(ServletRequest request, String name, String defaultVal) { String val = request.getParameter(name); return (val != null ? val : defaultVal); }
/** * Get a String parameter, throwing an exception if it isn't found. * @param request current HTTP request * @param name the name of the parameter * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static String getRequiredStringParameter(ServletRequest request, String name) throws ServletRequestBindingException { return STRING_PARSER.validateRequiredString(name, request.getParameter(name)); }
/** * Get a double parameter, throwing an exception if it isn't found or isn't a number. * @param request current HTTP request * @param name the name of the parameter * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static double getRequiredDoubleParameter(ServletRequest request, String name) throws ServletRequestBindingException { return DOUBLE_PARSER.parseDouble(name, request.getParameter(name)); }
/** * Get an int parameter, throwing an exception if it isn't found or isn't a number. * @param request current HTTP request * @param name the name of the parameter * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static int getRequiredIntParameter(ServletRequest request, String name) throws ServletRequestBindingException { return INT_PARSER.parseInt(name, request.getParameter(name)); }
/** * Get a long parameter, throwing an exception if it isn't found or isn't a number. * @param request current HTTP request * @param name the name of the parameter * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static long getRequiredLongParameter(ServletRequest request, String name) throws ServletRequestBindingException { return LONG_PARSER.parseLong(name, request.getParameter(name)); }
/** * Get a float parameter, throwing an exception if it isn't found or isn't a number. * @param request current HTTP request * @param name the name of the parameter * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static float getRequiredFloatParameter(ServletRequest request, String name) throws ServletRequestBindingException { return FLOAT_PARSER.parseFloat(name, request.getParameter(name)); }
/** * Get a boolean parameter, throwing an exception if it isn't found * or isn't a boolean. * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true; * treats every other non-empty value as false (i.e. parses leniently). * @param request current HTTP request * @param name the name of the parameter * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ public static boolean getRequiredBooleanParameter(ServletRequest request, String name) throws ServletRequestBindingException { return BOOLEAN_PARSER.parseBoolean(name, request.getParameter(name)); }
/** * Get a String parameter, or {@code null} if not present. * @param request current HTTP request * @param name the name of the parameter * @return the String value, or {@code null} if not present * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ @Nullable public static String getStringParameter(ServletRequest request, String name) throws ServletRequestBindingException { if (request.getParameter(name) == null) { return null; } return getRequiredStringParameter(request, name); }
/** * Get a Float parameter, or {@code null} if not present. * Throws an exception if it the parameter value isn't a number. * @param request current HTTP request * @param name the name of the parameter * @return the Float value, or {@code null} if not present * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ @Nullable public static Float getFloatParameter(ServletRequest request, String name) throws ServletRequestBindingException { if (request.getParameter(name) == null) { return null; } return getRequiredFloatParameter(request, name); }
/** * Get a Double parameter, or {@code null} if not present. * Throws an exception if it the parameter value isn't a number. * @param request current HTTP request * @param name the name of the parameter * @return the Double value, or {@code null} if not present * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ @Nullable public static Double getDoubleParameter(ServletRequest request, String name) throws ServletRequestBindingException { if (request.getParameter(name) == null) { return null; } return getRequiredDoubleParameter(request, name); }
/** * Get an Integer parameter, or {@code null} if not present. * Throws an exception if it the parameter value isn't a number. * @param request current HTTP request * @param name the name of the parameter * @return the Integer value, or {@code null} if not present * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ @Nullable public static Integer getIntParameter(ServletRequest request, String name) throws ServletRequestBindingException { if (request.getParameter(name) == null) { return null; } return getRequiredIntParameter(request, name); }
/** * Get a Long parameter, or {@code null} if not present. * Throws an exception if it the parameter value isn't a number. * @param request current HTTP request * @param name the name of the parameter * @return the Long value, or {@code null} if not present * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ @Nullable public static Long getLongParameter(ServletRequest request, String name) throws ServletRequestBindingException { if (request.getParameter(name) == null) { return null; } return getRequiredLongParameter(request, name); }
/** * Get a long parameter, with a fallback value. Never throws an exception. * Can pass a distinguished value as default to enable checks of whether it was supplied. * @param request current HTTP request * @param name the name of the parameter * @param defaultVal the default value to use as fallback */ public static long getLongParameter(ServletRequest request, String name, long defaultVal) { if (request.getParameter(name) == null) { return defaultVal; } try { return getRequiredLongParameter(request, name); } catch (ServletRequestBindingException ex) { return defaultVal; } }
/** * Get an int parameter, with a fallback value. Never throws an exception. * Can pass a distinguished value as default to enable checks of whether it was supplied. * @param request current HTTP request * @param name the name of the parameter * @param defaultVal the default value to use as fallback */ public static int getIntParameter(ServletRequest request, String name, int defaultVal) { if (request.getParameter(name) == null) { return defaultVal; } try { return getRequiredIntParameter(request, name); } catch (ServletRequestBindingException ex) { return defaultVal; } }
/** * Get a float parameter, with a fallback value. Never throws an exception. * Can pass a distinguished value as default to enable checks of whether it was supplied. * @param request current HTTP request * @param name the name of the parameter * @param defaultVal the default value to use as fallback */ public static float getFloatParameter(ServletRequest request, String name, float defaultVal) { if (request.getParameter(name) == null) { return defaultVal; } try { return getRequiredFloatParameter(request, name); } catch (ServletRequestBindingException ex) { return defaultVal; } }
/** * Get a double parameter, with a fallback value. Never throws an exception. * Can pass a distinguished value as default to enable checks of whether it was supplied. * @param request current HTTP request * @param name the name of the parameter * @param defaultVal the default value to use as fallback */ public static double getDoubleParameter(ServletRequest request, String name, double defaultVal) { if (request.getParameter(name) == null) { return defaultVal; } try { return getRequiredDoubleParameter(request, name); } catch (ServletRequestBindingException ex) { return defaultVal; } }
/** * Get a Boolean parameter, or {@code null} if not present. * Throws an exception if it the parameter value isn't a boolean. * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true; * treats every other non-empty value as false (i.e. parses leniently). * @param request current HTTP request * @param name the name of the parameter * @return the Boolean value, or {@code null} if not present * @throws ServletRequestBindingException a subclass of ServletException, * so it doesn't need to be caught */ @Nullable public static Boolean getBooleanParameter(ServletRequest request, String name) throws ServletRequestBindingException { if (request.getParameter(name) == null) { return null; } return (getRequiredBooleanParameter(request, name)); }
@Test public void getParameter() throws Exception { this.request.setContent("name=value".getBytes("ISO-8859-1")); this.filter.doFilter(this.request, this.response, this.filterChain); assertEquals("value", this.filterChain.getRequest().getParameter("name")); }
@Test public void getParameterNullValue() throws Exception { this.request.setContent("name=value".getBytes("ISO-8859-1")); this.filter.doFilter(this.request, this.response, this.filterChain); assertNotSame("Request not wrapped", this.request, this.filterChain.getRequest()); assertNull(this.filterChain.getRequest().getParameter("noSuchParam")); }
@Test public void getParameterFromQueryString() throws Exception { this.request.addParameter("name", "value1"); this.request.setContent("name=value2".getBytes("ISO-8859-1")); this.filter.doFilter(this.request, this.response, this.filterChain); assertNotSame("Request not wrapped", this.request, this.filterChain.getRequest()); assertEquals("Query string parameters should be listed ahead of form parameters", "value1", this.filterChain.getRequest().getParameter("name")); }