Abhiram Behera
Abhiram Behera

Reputation: 69

selected checkbox values from jsp to servlet is found null

I am working in an e-counselling project. I want to provide selection of multiple branch and schools at a time.I found that SelectedValues is always null.Please tell me what is wrong with it.

<table border="1" cellpadding="0" cellspacing="0" >
      <% for(i=0;i<branchSchools.length;i++){ %>
        <tr class=<%= (i%2==0)?"rowstyle":"altrowstyle" %> >
          <td>
            <input type="checkbox" name="SelectedBranchSchool"    id='SelectedBranchSchool<%= i %>' value="<%= i %>">
           </td>
            <td  >
              <%= branchSchools.item[i].branchCode %>
             </td>
             <td  >
               <%= branchSchools.item[i].school %>
              </td>
              <td align="right">
                <form name="frm5" action="addMultipleChoiceFill" method="post">
                  <% request.getSession().setAttribute("BranchSchools",branchSchools);
                     request.getSession().setAttribute("FilledChoices",filledChoices);
                  %>
                 <input type="submit" name="addmult"  value="AddMultiple"/>
               </form>
          </td>
   ![enter image description here][1]

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    BranchSchools branchSchools=null;
    FilledChoices filledChoices=null;
    branchSchools=(BranchSchools) request.getSession().getAttribute("BranchSchools");
    request.getSession().removeAttribute("BranchSchools");
    filledChoices=(FilledChoices)request.getSession().getAttribute("FilledChoices");
    request.getSession().removeAttribute("FilledChoices");
    String[] SelectedValues = request.getParameterValues("SelectedBranchSchool");
    for(int i=0; i<SelectedValues.length; i++){
        int k=Integer.valueOf(SelectedValues[i]);
        filledChoices.addFilledChoice(branchSchools.item[k]);
        branchSchools.removeBranchSchool(k);
    }
    request.setAttribute("BranchSchools",branchSchools);
    request.setAttribute("FilledChoices",filledChoices);
    request.setAttribute("Change", "yes");
    RequestDispatcher rd=getServletContext().getRequestDispatcher("/choiceFill.jsp?");
    rd.forward(request,response);

Upvotes: 0

Views: 723

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

Your checkbox is not inside the form, so it's not submitted when the form is submitted:

<input type="checkbox" name="SelectedBranchSchool" ...>
...
<form name="frm5" action="addMultipleChoiceFill" method="post">
   ...
   <input type="submit" name="addmult"  value="AddMultiple"/>
</form>

Also, please respect the Java naming conventions, and read about MVC, the JSTL, and best practices.

Upvotes: 1

Related Questions