如何将多个<input type =“”复选框“”/>作为数组发布到Java servlet?

时间:2016-03-31 11:00:32

标签: java html arrays post checkbox

这个问题与How to post multiple <input type="checkbox" /> as array in PHP?相同,但我无法使解决方案在我的java servlet设置中有效。当使用将[]添加到分组复选框的name属性的apporach时,我只获得第一个选中的选项。我不确定它是否是仅包含一个元素的实际发布数组,或者如果我没有在服务器端访问它。这是我在java中检查值的方法:

@Override
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {  
    for(String name : request.getParameterMap().keySet()){      
        System.out.println(name +": " + request.getParameter(name));        
    }
}

这会打印countries[]: US,即使我在US输入后检查了更多的checbox。值将更改,之后复选框是已检查的第一个复选框。我做错了什么?

这是我的HTML:

<form action="mypage" method="post">
    <input id="cb-country-gb" type="checkbox" name="countries[]" class="hide" value="GB"/>
    <input id="cb-country-us" type="checkbox" name="countries[]" class="hide" value="US"/>
    <input id="cb-country-ge" type="checkbox" name="countries[]" class="hide" value="GE"/>
    <input id="cb-country-es" type="checkbox" name="countries[]" class="hide" value="ES"/>
    <button type="submit" class="btn btn-primary">Search</button>
</form>

2 个答案:

答案 0 :(得分:1)

如果您选中多个复选框,则请求包含名称为countries[]的多个参数。

如果您致电request.getParameter("countries[]"),则仅返回第一个参数值。

相反,您需要使用

String[] checked = request.getParameterValues("countries[]");
if (checked != null)
     ...

答案 1 :(得分:1)

您应该使用public class Utilities { public static String getTopPackage(Context context) { if (android.os.Build.VERSION.SDK_INT < 21) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses(); return tasks.get(0).processName; } else { List<UsageStats> usageStats = getUsageStats(context, 43200000); if (usageStats == null || usageStats.size() == 0) { return ""; } /* here you can do what you want with the usageStats list of processes... */ RecentUseComparator mRecentComp = new RecentUseComparator(); Collections.sort(usageStats, mRecentComp); return usageStats.get(0).getPackageName(); } } public static List<UsageStats> getUsageStats(Context context, long forTime) { if (android.os.Build.VERSION.SDK_INT >= 21) { long ts = System.currentTimeMillis(); UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE); List<UsageStats> usageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, ts - forTime, ts); return usageStats; } return null; } static class RecentUseComparator implements Comparator<UsageStats> { @Override public int compare(UsageStats lhs, UsageStats rhs) { return (lhs.getLastTimeUsed() > rhs.getLastTimeUsed()) ? -1 : (lhs.getLastTimeUsed() == rhs.getLastTimeUsed()) ? 0 : 1; } } public static boolean canGetUsageStatistics(Context context) { if (android.os.Build.VERSION.SDK_INT < 21) { return true; } else { try { PackageManager packageManager = context.getPackageManager(); ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0); AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName); return (mode == AppOpsManager.MODE_ALLOWED); } catch (PackageManager.NameNotFoundException e) { return false; } } } } 返回包含给定请求参数所具有的所有值的String对象数组:

使用以下代码测试:

[getParameterValues][1]