javascript - Serlvet的request.getParameter和onclick使用

标签 javascript html jsp

当单击图像时,我想提交到 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/

相关文章:

javascript - JS/jQuery 根据单选/选择值显示文本框

php - 将html属性值传递给php中的下一个脚本

java - Spring 4 迁移 : OutOfMemoryError: PermGen Space

javascript - ES6箭头函数TypeError : Cannot read property '1' of null with multiple params using Karma

javascript - 分发具有依赖项的可重用 JavaScript 模块的最佳方式是什么?

javascript - 使用 angularJS 获取数据

java - Tomcat 是否实现或运行 Java Servlet 和 JavaServer Pages (JSP) 规范?

javascript - 导入语法 React Redux

javascript - 在 Firefox 上禁用鼠标滚轮的滚动点击?

java - 在多个 JSP 页面中添加 ArrayList