Jordi 45454
Jordi 45454

Reputation: 295

working with jsp(java) arrays and checkboxes

I am creating a program that first of all, with a number set by the user, it create an array of days of the week on the screen, with a checkbox. (The [] is a checkbox).

Example:

If the user has selected the number 2, there will appear 2 rows:

Week 1: Monday[X] Tuesday[X] Wednesday[X] Thursday[] Freeday[] Saturday[] Sunday[].

Week 2: Monday[] Tuesday[X] Wednesday[X] Thursday[X] Freeday[] Saturday[] Sunday[]

Then, in the second page, I want to show THE DAYS THAT THE USER HAVE CLICK TO. Something like that:

Days selected on the Week 1: Monday, Tuesday, Wednesday.

Days selected on the Week 2: Tuesday, Wednesday, Thursday.

but... with this code I'm getting something like this, and I don't want it...:

Week: 1 - Monday--

Week: 2 - Tuesday--

Week: 3 - Wednesday--

Week: 4 - Tuesday--

Week: 5- Wednesday--

Week: 6 - Thursday--

How can I solve it?

Thanks you!

 if(request.getParameter("number")!=null)
        {  
            String number;
            int number2;
            number=request.getParameter("number");
            number2=Integer.parseInt(number);
            out.println("<form action='ex2c.jsp' method='get'>");
            String[] dias = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
            for (int i=0; i<number2; i++)
            {
                out.print("<br>");
                out.println("Week: "+i+1+" - " ); 

                for(int j=0; j<7; j++)
                {

                    out.println("<input type='checkbox' name='check' value='"+dias[j]+"'>"+dias[j]+"</input>");    
                }
            } 
                out.println("<br><br><input type='submit' name='ok'/></form>"); 
        }
   if(request.getParameter("ok")!=null)
    {  
       // String num=request.getParameter("number");
       // String [] number;
        String [] number = request.getParameterValues("check");

        out.println(number.length);
        out.println("<br />");
        for (int i=0;i<number.length;i++)   
        {
            out.println("<br><br>Week: "+i+1+" - " );
            out.println(number[i]+"--");

            for(int j=0; j<2; j++)
            {

                //out.println(number[j]+"--") ;   
            }

        }


        }

Upvotes: 0

Views: 2142

Answers (1)

albciff
albciff

Reputation: 18517

You're sending all the week days in the same form without differentiating the week, you're putting all the days in a input with name="check", and you are receiving it all together in String [] number = request.getParameterValues("check");.

The thing is you're sending Monday, Tuesday, Wednesday for week 1, and Tuesday, Wednesday, Thursday for week 2 because you're creating:

<form action='ex2c.jsp' method='get'>
   <input type='checkbox' name='check' value='Monday'>Monday</input>
   <input type='checkbox' name='check' value='Tuesday'>Tuesday</input>
   ...
   <input type='checkbox' name='check' value='Sunday'>Sunday</input>
   <input type='checkbox' name='check' value='Monday'>Monday</input>
   <input type='checkbox' name='check' value='Tuesday'>Tuesday</input>
   ...
   <input type='checkbox' name='check' value='Sunday'>Sunday</input>
   <input type='submit' name='ok'/>
</form>

So when you submit this form, through this code String [] number = request.getParameterValues("check"); you get: ["Monday","Tuesday","Wednesday","Tuesday", "Wednesday", "Thursday"].

If you want to show in the second jsp the days checked by user in the first jsp, you've to found a way to group the days in a specific week and send this information to the server in order that they can differentiate between its.

You can try using your number parameter to add a _number to your name inputs and send also this number with the form to check how many weeks you've to print, in based on your code:

First jsp:

if(request.getParameter("number")!=null)
{  
   String number;
   int number2;
   number=request.getParameter("number");
   number2=Integer.parseInt(number);
   out.println("<form action='ex2c.jsp' method='get'>");
   // ADDS THE INPUT WITH WEEKS
   out.println("<input type='text' name='weeks' value='" + number + "'>");
   String[] dias = {"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
   for (int i=1; i<=number2; i++)
   {
       out.print("<br>");
       out.println("Week: "+ i + " - " ); 

       for(int j=0; j<7; j++)
       {
          // ADD THE WEEK NUMBER IN THE NAME
          out.println("<input type='checkbox' name='check_" + i + "' value='"+dias[j]+"'>"+dias[j]+"</input>");    
       }
    } 
    out.println("<br><br><input type='submit' name='ok'/></form>"); 
}

With this you generate something like:

<form action='ex2c.jsp' method='get'>
   <input type='text' name='weeks' value='2'>
   <input type='checkbox' name='check_1' value='Monday'>Monday</input>
   <input type='checkbox' name='check_1' value='Tuesday'>Tuesday</input>
   ...
   <input type='checkbox' name='check_1' value='Sunday'>Sunday</input>
   <input type='checkbox' name='check_2' value='Monday'>Monday</input>
   <input type='checkbox' name='check_2' value='Tuesday'>Tuesday</input>
   ...
   <input type='checkbox' name='check_2' value='Sunday'>Sunday</input>
   <input type='submit' name='ok'/>
</form>

Then your second jsp:

if(request.getParameter("ok")!=null)
{  
   int weeks =Integer.parseInt(request.getParameter("weeks"));
   for(int i=1;i<=weeks;i++){
      out.println("<br><br>Week: "+ i +" - " );
      String [] days = request.getParameterValues("check_"+i);     
      for(int j=0;i<days.length;j++)   
      {
         out.println(days[j] + " ");      
      }
      out.println("<br />");
   }
}

I don't try this code however I think that the idea works. Besides I want to comment that the way how you render a jsp seems very odd to me.

Hope this helps,

Upvotes: 2

Related Questions