从图像传递值到servlet

时间:2015-03-28 21:38:39

标签: java jsp servlets

来自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);
}
...

当我点击图像时,我得到了空指针异常。我想知道为什么它没有传递它应该的值。因为隐藏将总是得到值。

1 个答案:

答案 0 :(得分:0)

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