在我的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);
}
答案 0 :(得分:1)
您可以定义另一个隐藏参数,例如
<input name="action" type="hidden"
value="${pageContext.request.contextPath}/CurrencyController?
action=listCurrency" />
然后在servlet中使用相同的代码request.getParameter(“action”)来获取它的值。