检查循环中的复选框并检索仅选中复选框的值

时间:2013-12-18 07:12:49

标签: java

任何人都可以告诉我如何检查是否在循环中选中了复选框...我只想保存已选中复选框的值..我已经完成了这样的代码

 for(int i=0;i<9;i++)
         {
        String name=request.getParameter("b");
        String ca[]=request.getParameterValues("a"+i);

           int caa=Integer.parseInt(ca[i]);
          int ff=caa-1;
            String m9=""+ff;

         String rr[]=request.getParameterValues("g"+i);
    int rrr=Integer.parseInt(rr[i]);
    int rre=rrr-1;
    String m=""+rre;

    String ttl[]=request.getParameterValues("h"+i);
    int ttt=Integer.parseInt(ttl[i]);
    int tte=ttt-1;
    String m1=""+tte;

    String ws[]=request.getParameterValues("j"+i);
    int wss=Integer.parseInt(ws[i]);
    int wsse=wss-1;
    String m2=""+wsse;

    String ht[]=request.getParameterValues("k"+i);
    int htt=Integer.parseInt(ht[i]);
    int htte=htt-1;
    String m3=""+htte;

    String mp[]=request.getParameterValues("l"+i);
    int mpp=Integer.parseInt(mp[i]);
    int mppe=mpp-1;
    String m4=""+mppe;

    String lv[]=request.getParameterValues("m"+i);
    int lvv=Integer.parseInt(lv[i]);
    int lvve=lvv-1;
    String m5=""+lvve;

    String mc[]=request.getParameterValues("n"+i);
    int mcc=Integer.parseInt(mc[i]);
    int mcce=mcc-1;
    String m6=""+mcce;

    String wb[]=request.getParameterValues("o"+i);
    int wbb=Integer.parseInt(wb[i]);
    int wbbe=wbb-1;
    String m7=""+wbbe;

    String wt[]=request.getParameterValues("p"+i);
    int wtt=Integer.parseInt(wt[i]);
    int wtt1=wtt-1;
    String m8=""+wtt1;

    String sql2="update nearloc set CA=?,RR=?,TTL=?,WS=?,HT=?,MP=?,LV=?,MC=?,WB=?,WT=? where location=?";
    PreparedStatement prst2=con.prepareStatement(sql2);

    prst2.setString(1,m9);

    prst2.setString(2,m);
    prst2.setString(3,m1);
    prst2.setString(4,m2);
    prst2.setString(5,m3);
    prst2.setString(6,m4);
    prst2.setString(7,m5);
    prst2.setString(8,m6);
    prst2.setString(9,m7);
    prst2.setString(10,m8);
    prst2.setString(11, name);


    if(prst2.executeUpdate()==1)
    {
        request.setAttribute("iidd",id);
        RequestDispatcher              

           rd= request.getRequestDispatcher("FMS08_SendVeh.jsp");
        rd.forward(request, response);
    }

a,g,k,n等是循环中复选框的名称。我想检查仅选中复选框的值并将其保存到数据库中。问题是,如果我留下任何复选框,则会显示NullPointerException

1 个答案:

答案 0 :(得分:0)

if (request.getParameterMap().containsKey("a"+i)) {
     String ca[]=request.getParameterValues("a"+i);
}

if (request.getParameter("a"+i) != null) {
     String ca[]=request.getParameterValues("a"+i);
}