Java examples for Servlet JSP:Servlet
Get int-type parameter from Servlet.
import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Main{ /**//from ww w . j a v a 2 s . c o m * Get int-type parameter. If parameter is not exist, or cannot be converted * to int, default value is return. * * @param name Parameter name. * @param defaultValue Default int value. * @return int value of specified parameter. */ public static int getIntParameter(String name, int defaultValue) { String value = getStringParameter(name); try { return Integer.parseInt(value); } catch (NumberFormatException e) { return defaultValue; } } /** * Get parameter value as String. If parameter is not exist, return null. * * @param name Parameter name. * @return String value, or null if no such parameter. */ public static String getStringParameter(String name) { return getStringParameter(name, null); } /** * Get parameter value as String. If parameter is not exist, return default value. * * @param name Parameter name. * @param defaultValue Default String value. * @return String value. */ public static String getStringParameter(String name, String defaultValue) { HttpServletRequest request = ActionContext.getActionContext() .getHttpServletRequest(); String value = request.getParameter(name); return value == null ? defaultValue : value; } }