为什么不能在doPut servlet方法中从JSP获取参数

时间:2017-08-24 21:56:37

标签: java twitter-bootstrap jsp servlets

我不明白为什么不能在doPut方法中访问参数。 只想在JSP页面上使用 name 标记获取servlet参数值。 我在JSP页面上使用 method =“put”,并且 action =“ReservationServlet”并且也没有任何例外。

公共类ReservationServlet扩展了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);

}

}

和我在JSP页面上的表单:

<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>

谢谢!

1 个答案:

答案 0 :(得分:0)

  

我正在使用method =&#34; put&#34;在JSP页面上的表单

根据HTML standard,您不能在表单方法中放置&#34; put&#34;

如果你把它放在表单的method属性中,它不会产生PUT,而是会执行GET请求。 (无效使用方法属性会导致默认&#39; get&#39;)

如果你想使用PUT方法,你必须通过类似AJAX的方法来实现。

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

或者你也可以attach it to the file input stream

看看你的代码,我不明白你做PUT的原因是什么?为什么不使用正常的GET / POST请求,对你来说会更容易..