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> </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
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 aString
Object available at index0
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With