当单击图像时,我想提交到 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>
servlet1:
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");
}
最佳答案
您不需要使用任何 onclick 处理程序来提交表单。这就是图像类型的输入本身所做的事情。将表单方法更改为 get
并分析浏览器位置栏以查看发送了哪些参数。或者使用Firebug。或阅读the documentation输入标签或类型图像:
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.
关于javascript - Serlvet的request.getParameter和onclick使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11713224/