request.getParameter为null

问题描述 投票:0回答:1

我需要在我的JSP页面中使用request.getParameter("action"),以定义应执行的命令(<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 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>
jsp servlets
1个回答
1
投票

我试图重现您的问题,您无法通过页面上的type="submit"进行任何控制。

当您单击提交按钮时,它将正确显示servlet中的request.getParameter("action")

<form name="hotelList" method="GET" action="MainServlet">
    <input type="hidden" name="action" value="open_list"/>
    <input type="submit" value="Send">
</form>
© www.soinside.com 2019 - 2024. All rights reserved.