Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing date from jsp to servlet using getParameterValues

I am having error in servlet to pass the value from JSP..

JSP codes :

<table>                 
<tr>
    <td>Day</td>
    <td>Start</td>
    <td>End</td>
    <td>Date</td>
    <td>&nbsp;</td>                                                          
</tr>               
<tr>
   <td><select name="availableDay">  
       <!--Listing days-->                                                                                         
       </select></td>
   <td><input type="time"   name="availableStart"/></td>
   <td><input type="time"   name="availableEnd"/></td>
   <td><input type="date"   name="availableDate" /></td>
   <td><input type="button" class="add" name="action" value="Add More"</td>
</tr>
</table>

I want to pass availableDate to servlet. Fyi, the row in JSP is dynamically generated. Thus, I pass using []. My servlet codes :

  public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {

  String[] presentationID = request.getParameterValues("selectavailability");
  String[] availableDay =  request.getParameterValues("availableDay"); 
  String[] availableStart =  request.getParameterValues("availableStart");
  String[] availableEnd =  request.getParameterValues("availableEnd");
  String[] availableDate = request.getParameterValues("availableDate");  

  SimpleDateFormat availDate = new SimpleDateFormat("dd-MM-yyyy");
  Date chosenDate = availDate.parse(availableDate);// THIS IS WHERE I AM GETTING ERROR

  try {

        if(availableDay != null && availableStart != null && availableEnd != null)
        {
          for (int i = 0; i < availableDay.length; i++) 
          {

            AvailabilityBean available = new  AvailabilityBean(); 
            available.setLecturerID(request.getParameter("lecturerID"));
            available.setAvailableDay(availableDay[i]);
            available.setAvailableStart(availableStart[i]);
            available.setAvailableEnd(availableEnd[i]);
            available.setAvailableDate(availableDate[i]); //EFFECTED THIS LINE TOO

            available = AddAvailableDAO.addavailable(available);


           }
        }

     }
                   response.sendRedirect("addAvailability.jsp");                
 }
catch (Throwable theException) {
            System.out.println("hhhhhhh"+theException);
        }
    }

For extra, AvailabilityBean:

    private String availableID;
    private String lecturerID;
    private String availableDay;
    private String availableStart;
    private String availableEnd;
    private Date availableDate;
    private String presentationID;

Error : incompatible types: String[] cannot be converted to String.

Can you show me where is my mistake? And how can i solve this? Thank you

like image 562
Elly Avatar asked Nov 09 '22 16:11

Elly


1 Answers

Here is your availableDate Variable which is an array of String.

 String[] availableDate = request.getParameterValues("availableDate");

Now , you are using SimpleDateFormat Class to format your String data into Date.

Here parse(String text) will take a String Argument not a type of String[] (Array).

 SimpleDateFormat availDate = new SimpleDateFormat("dd-MM-yyyy");
  Date chosenDate = availDate.parse(availableDate);// availableDate is a String Array not a String.

So, It will raise an compile-time error incompatible types: String[] cannot be converted to String.

better you should try this

 Date chosenDate = availDate.parse(availableDate[0]); //preferred index you may pass as per your requirement.

Note:- availableDate[0] will return a String Object available at index 0.

like image 158
Vikrant Kashyap Avatar answered Nov 14 '22 22:11

Vikrant Kashyap