2

单击图像时,我想提交给 servlet1 并确定单击了哪个图像。asnwer 应重定向到 index.jsp 并在“单击状态”后显示。我怎样才能让它在 servlet 端工作?我如何使用 request.getParameter 来确定单击了哪个图像?

index.jsp(部分)

<% 

String message = "NoImageWasClicked";
if (session.getAttribute("message") != null) {
    message = session.getAttribute("message");
}

%>

<form method="post" action="servlet1">
      <input id='img1' type='image' onclick='submit()'/>
      <input id='img2' type='image' onclick='submit()'/>
      <input id='img2' type='image' onclick='submit()'/>
      Clicking status: <%=message%>
</form>

小服务程序1:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");        
        request.getParameter ( ??????????? )
        ???????
        request.getSession().setAttribute("message", ????????);
        response.sendRedirect("index.jsp");
    }
4

1 回答 1

2

You don't need to use any onclick handler to submit the form. That's what an input of type image does natively. Change the form method to get and analize the browser location bar to see which parameters are sent. Or use Firebug. Or read the documentation of the input tag or type image:

The coordinate is sent to the server during form submission by sending two entries for the element, derived from the name of the control but with ".x" and ".y" appended to the name with the x and y components of the coordinate respectively.

于 2012-07-29T22:16:08.303 回答