request.getParameter имеет значение null - PullRequest
0 голосов
/ 22 апреля 2020

Мне нужно получить request.getParameter("action"), который я использую на своей странице JSP, чтобы определить, какая команда должна выполняться (<input type="hidden" name="action" value="open_list"/>). Результат null. В чем проблема? Или как я могу передать команду по-другому?

CommandFactory. java

public class CommandFactory {
    public Command defineCommand(HttpServletRequest request) {
        Command current = new ErrorCommand();
        String action = request.getParameter("action");
        if (action == null || action.isEmpty()) {
            return current;
        }
        try {
            CommandEnum currentEnum = CommandEnum.valueOf(action.toUpperCase());
            current = currentEnum.getCommand();
        } catch (IllegalArgumentException e){
            request.setAttribute("wrongAction", action);
        }
        return current;
    }
}

MainServlet. java

    public class MainServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;

    public MainServlet() {
        super();
    }

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

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

    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CommandFactory client = new CommandFactory();
        Command command = client.defineCommand(request);
        String page = command.execute(request, response);
        if (page != null) {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
            dispatcher.forward(request, response);
        }else {
            response.sendRedirect(PageURL.ERROR_PAGE);
        }
    }
}

JSP page

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form name="hotelList" method="GET" action="/MainServlet">
    <input type="hidden" name="action" value="open_list"/>
    <table>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>ADDRESS</th>
            <th>RATING</th>
            <th>OWNERNAME</th>
        </tr>
        <c:forEach items="${list}" var="hotel">
            <tr>
                <td>${hotel.id}</td>
                <td>${hotel.name}</td>
                <td>${hotel.address}</td>
                <td>${hotel.rating}</td>
                <td>${hotel.ownerName}</td>
            </tr>
        </c:forEach>
    </table>
</form>
</body>
</html>

1 Ответ

1 голос
/ 22 апреля 2020

Я пытался воспроизвести вашу проблему, у вас нет контроля с type="submit" на вашей странице.

Когда вы нажмете кнопку «Отправить», ваш сервлет получит правильное значение request.getParameter("action").

<form name="hotelList" method="GET" action="MainServlet">
    <input type="hidden" name="action" value="open_list"/>
    <input type="submit" value="Send">
</form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...