Post Form不会调用Servlet

时间:2017-09-19 11:43:14

标签: java html jsp servlets web.xml

所以我有一个调用Servlet来执行搜索的帖子,但问题是,当我按下提交按钮时,它完全没有响应。虽然在不同的.jsp页面中使用相同的形式时,它可以正常工作。如果它与servlet映射有关,那么tomcat服务器会出现一个错误页面,但我没有得到这样的错误页面。它完全没有反应。

我的HTML表单:

   <form action="AnonSearchServlet" class="form-inline" method="post">
      <input type="search" name="location" class="form-control input-lg" placeholder="Destination, City, Address" required>
      <div class="input-group">
        <input type="search" class="form-control input-lg" placeholder="When" name="daterange" value="" required/>
        <div class="input-group-addon">
          <span class="glyphicon glyphicon-calendar"></span>
        </div>
      </div>
      <input style="width: 20%; height: 46px" type="number" min="1" name="accomodates" class="form-control" placeholder="Guests" required>
      <button type="submit" class="btn btn-danger btn-lg">Search</button>
    </form>

我的Servlet:

public class AnonSearchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String start_date;
        String end_date;
        String date_range = request.getParameter("daterange");
        UserBean user = new UserBean();
        String[] tokens = date_range.split(" ");
        start_date = tokens[0];
        end_date = tokens[2];

        try {
            HttpSession session = request.getSession(true);
            user.setUserID(0);
            System.out.println(user.getUserID());

            ResultSet search_rs = null;
            SearchBean search_bean = new SearchBean();

            search_bean.setUserId(user.getUserID());
            search_bean.setLocation(request.getParameter("location"));
            System.out.println(request.getParameter("location"));
            search_bean.setStreet(request.getParameter("location"));
            search_bean.setNeighbourhood(request.getParameter("location"));
            search_bean.setAccomodates(request.getParameter("accomodates"));
            search_bean.setStartDate(start_date);
            search_bean.setEndDate(end_date);

            search_bean = SearchDAO.search(search_bean);
            session.setAttribute("current_search", search_bean);
            response.sendRedirect("searchResults.jsp");


        } catch (Throwable thException) {
            System.out.println(thException);
        }
    }
}

我的web.xml:

    <servlet>
        <servlet-name>AnonSearchServlet</servlet-name>
        <servlet-class>Servlets.AnonSearchServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AnonSearchServlet</servlet-name>
        <url-pattern>/AnonSearchServlet</url-pattern>
    </servlet-mapping>

2 个答案:

答案 0 :(得分:0)

@CharisAlex这是你的jsp页面中的问题<script src="https://d3js.org/d3.v4.min.js"></script>标签没有提交表单操作,所以你必须修改你的代码

<button>

替换成

<button type="submit" class="btn btn-danger btn-lg">Search</button>`

答案 1 :(得分:-1)

尝试使用

<input type="submit" value="submit">

而不是使用

<button type="submit" class="btn btn-danger btn-lg">Search </button>