request.getParameter (), показывающий ноль - PullRequest
0 голосов
/ 12 ноября 2018

У меня есть следующая форма в JSP

<form method="POST" action="${pageContext.request.contextPath}/">
    <div class="form-group col-md-3 col-md-offset-1">
        <input type="number" name="minPrice" id="minPrice" title="Minimum Price" class="form-control"
               placeholder="Minimum Price"/>
    </div>
    <div class="form-group col-md-3">
        <input type="number" name="maxPrice" id="maxPrice" title="Maximum Price" class="form-control"
               placeholder="Maximum Price"/>
    </div>
    <div class="form-group col-md-3">
        <input type="text" name="company" id="company" title="Company" class="form-control"
               placeholder="Company"/>
    </div>
    <button type="submit" class="btn btn-danger">
        <span class="glyphicon glyphicon-search"></span> Search
    </button>
</form>

И метод doPost() в моем сервлете:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Float minPrice , maxPrice;
    try {
        minPrice = Float.parseFloat(request.getParameter("minPrice"));
    }
    catch (Exception ex) {
        minPrice = null;
    }

    try {
        maxPrice = Float.parseFloat(request.getParameter("maxPrice"));
    }
    catch (Exception ex) {
        maxPrice = null;
    }

    String company;
    try {
        company = request.getParameter(request.getParameter("company"));
    }
    catch (Exception ex) {
        company = null;
    }
    System.out.println(minPrice + " " + maxPrice + " " + company);

    Connection conn = MyUtils.getStoredConnection(request);

    String errorString = null;
    List<Car> list = null;
    try {
        list = DBUtils.getCars(conn);
    } catch (SQLException e) {
        e.printStackTrace();
        errorString = e.getMessage();
    }

    if (minPrice != null && maxPrice != null) {
        if (maxPrice < minPrice) {
            errorString = "";
        }
    }

    if (list != null) {
        System.out.println(list.size());

        Iterator<Car> iter = list.iterator();
        while (iter.hasNext()) {
            Car car = iter.next();

            if (minPrice != null) {
                //System.out.println("Min is not null");
                if (car.getCostPerDay() < minPrice) {
                    iter.remove();
                    continue;
                }
            }
            if (maxPrice != null) {
                //System.out.println("Max is not null");
                if (car.getCostPerDay() > maxPrice) {
                    iter.remove();
                }
            }
            if (company != null) {
                if (!car.getCompany().equalsIgnoreCase(company))
                    iter.remove();
            }
        }
        System.out.println(list.size());
    }

    request.setAttribute("errorString", errorString);
    request.setAttribute("productList", list);

    RequestDispatcher dispatcher = request.getServletContext()
            .getRequestDispatcher("/WEB-INF/views/carListView.jsp");
    dispatcher.forward(request, response);
}

Когда я ввожу minPrice или maxPrice или оба, я вижу вывод, как в сообщении System.out.println (), например:

Когда maxPrice равно 10000: null 10000.0 null

Когда minPrice равно 4000: 4000.0 null null

Когда компанией является Марути Сузуки: null null null

Я установил атрибут имени компании в FORM, чтобы к нему можно было получить доступ через сервлет, так же, как я это делал для minPrice и maxPrice, но я не могу получить его значение внутри сервлета. Я не знаю, где я иду не так

1 Ответ

0 голосов
/ 12 ноября 2018

почему вы делаете getParameter из getParameter, как в

request.getParameter(request.getParameter("company"));

изменить на

company = request.getParameter("company");

Также Я предлагаю вам не просто проглотить ваши исключения

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...