0

I don't get it why can't access parameter in doPut method. Just want to get in servlet parameter value with name tag on JSP page. I'm using method="put" in form on JSP page and action="ReservationServlet". And also don't get any exception.

public class ReservationServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {

        List<Doctor> listDoctors = DoctorDao.getInstance().findAll(ResourceManager.getConnection());
        request.setAttribute("listDoctors", listDoctors);
        RequestDispatcher rd = request.getRequestDispatcher("rezervacija.jsp");
        rd.forward(request, response);

    } catch (SQLException ex) {
        Logger.getLogger(ReservationServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DentistException ex) {
        Logger.getLogger(ReservationServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    System.out.println("USAOOOOOO U POSTTTTT");
    String vreme = (String) request.getParameter("choose-time");
    System.out.println(vreme);

    RequestDispatcher rd = request.getRequestDispatcher("rezervacija.jsp");
    rd.forward(request, response);

    processRequest(request, response);

}

@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("USAOOOO OVDEEEEEEEEEEEEE");
    String vreme = (String) request.getParameter("choose-time");
    System.out.println(vreme);

    processRequest(request, response);

}

}

and my form on JSP page:

<form action="ReservationServlet" method="POST">

<!-- CALENDAR -->
<label for="datepicker">Datum:</label>
   <div class="form-group">
      <div class="input-group">
         <input type="text" id="datepicker" placeholder="Unesite datum"
              class="form-control" required="true">
              <span class="input-group-addon">
              <span class="fa fa-calendar"
                 id="datepicker"></span></span>
      </div>
   </div>

<!-- SAT -->
<label for="choose-time">Vreme:</label>
   <div class="form-group">
       <div class="input-group clockpicker" data-placement="left"
             data-align="top" data-autoclose="true">
             <input type="text" class="form-control"
                    id="choose-time" name="choose-time" required="true">
             <span class="input-group-addon">
             <span class="glyphicon glyphicon-time"></span>
                                                    </span>
       </div>
   </div>

<!-- VRSTA USLUGE -->
<div class="form-group">
     <div class="input-group" style="width: 100%;">
          <label for="choose-service">Vrsta usluge:</label>
          <select class="form-control" id="choose-service" style="border-
                  radius:3%;" name="choose-service">
          <option id="service1"></option>
          <option id="service2"></option>
          <option id="service3"></option>
          <option id="service4"></option>
          </select>
     </div>
</div>

<!-- DOCTOR -->
<div class="form-group">
     <div class="input-group" style="width: 100%;">
          <label for="choose-doctor">Doktor:</label>
          <select class="form-control" id="choose-doctor" style="border-
                  radius:3%;" name="choose-doctor">
          <c:forEach items="${requestScope.listDoctors}" var="listDoctors" >
               <option><c:out value="${listDoctors.firstName} 
                       ${listDoctors.lastName} "/></option>
          </c:forEach>
          </select>
     </div>
</div>

<button type="submit" class="btn btn-info btn-lg">REZERVIŠI</button>
</form>

Thanks!

1 Answers1

0

I'm using method="put" in form on JSP page

According to the HTML standard, you cannot put "put" in the form method.

If you do put it in the method attribute of a form, it will not result in a PUT, but instead it will do a GET request. ( invalid use of method attribute results in the default 'get')

If you want to use the PUT method, you have to do it via something like AJAX.

$.ajax({
  url: '/ReservationServlet',
  type: 'PUT',
  data: "choose-time=10AM&choose-service=sphincterotomy",
  success: function(data) {
    alert('Load was performed.');
  }
});

Or you can also attach it to the file input stream.

Looking at your code, i don't understand what your reasons are for doing a PUT? Why not just use the normal GET/POST requests, it would be much easier for you..

Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • 1
    Thank you on nice explanation! Yeah, at the end I did with POST. @Jonathan Laliberte –  Aug 25 '17 at 10:20