JSP表单传递参数

时间:2018-04-04 13:33:44

标签: forms jsp servlets parameters

在我的jsp中,我正在调用一个servlet:

<form method="GET" action ="${pageContext.request.contextPath}/CurrencyController?action=listCurrency">
    Currency code: <input type="text" name="currencyCode" id="currencyCode" /> 
    <br /> 
    <input type="submit" value="Search" />
</form>

但是在我的Servlet中request.getParameter("action")null。那么如何传递action参数?

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String action = request.getParameter("action");

    if (action.equalsIgnoreCase("delete")){
        String currencyCode = request.getParameter("currencyCode");
        ...    
    } else if (action.equalsIgnoreCase("edit")){
        String currencyCode = request.getParameter("currencyCode");
        ...
    } else if (action.equalsIgnoreCase("listCurrency")){
        request.setAttribute("currencies", dao.getCurrencyByCode(request.getParameter("currencyCode")));
    } else {
        forward = INSERT_OR_EDIT;
    }

    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);
}

1 个答案:

答案 0 :(得分:1)

您可以定义另一个隐藏参数,例如

<input name="action" type="hidden" 
value="${pageContext.request.contextPath}/CurrencyController? 
action=listCurrency" /> 

然后在servlet中使用相同的代码request.getParameter(“action”)来获取它的值。