将值从图像传递给 servlet

Passing value to servlet from image

从JSP我有

..
<form action="TestMartController" method="post">
<input type="hidden" value="math">
<input type="image" src="<%=request.getContextPath()%>/css/categories/math.jpg">
</form>
..

在我的 servlet 中我有

...
private static final String MATH = "WEB-INF/jsp/math.jsp";

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

        String forward = null;

        String action=request.getParameter("action");
        if(action.equals("math")){
        forward = MATH;
        flag = 1;
        }
        RequestDispatcher rd = request.getRequestDispatcher(forward);
        rd.forward(request, response);
}
...

当我点击图片时出现空指针异常。我想知道为什么它没有传递它应该传递的值。由于 hidden 将始终获取要传递的值。

您的 input type="hidden" 字段缺少 name="action" 属性。因此,动作参数为空。