如何知道在jsp上单击了哪个按钮

时间:2013-12-16 06:21:28

标签: java google-app-engine jsp servlets

带有两个按钮的Jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <body style="background-color:black;">


<p>
<p><input type="button" name="good" value="Pen" onclick="location.href='hello';"> </p>
<p><input type="button" name="good" value="Paper" onclick="location.href='hello';">
</p>

</body> 
</html>

这是servlet

package pack.exp;
import java.io.IOException;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;


@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet 
{

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws 
    IOException 
{
    String val=req.getParameter("good");  
    if("Pen".equals(val))
    {  
        resp.setContentType("text/plain");
        resp.getWriter().println("Pen was clicked" ); 
    }  
    else if("Paper".equals(val))
    {
        resp.setContentType("text/plain");
        resp.getWriter().println("Paper was clicked"); 
    }  

}
 }

单击按钮时,我的代码未提供正确的输出。我想当我点击Pen然后它应该输入if()并打印文本,我想要相同的纸张按钮。

4 个答案:

答案 0 :(得分:1)

使用jquery完成任务。

将您的html代码更改为这些代码行。

<form method="post" action="#" name="Form" id="Form" >
<input type="button" value="one" id="One"/> 
<input type="button" value="two" id="Two"/>
</form>

并在脚本中添加这些行

$('input:button').click(function() {
 alert($(this).val());
 var value=$(this).val();
 var url='hello?good=';
 url+=value;
 $("#Form").attr("action",url); 
 $("#Form").submit();   
});

您可以使用jquery 1.7.1及更高版本。 希望这对你有所帮助。快乐编码:)

答案 1 :(得分:0)

添加两个隐藏字段。

<form id="my form" >
 <input type="hidden" name="myPen" />
 <input type="hidden" name="myPaper" />
 <input type="button" name="good" value="pen" on click="{document.myform.mypen.value=this.value;location.href='hello';}"
 <input type="button" name="good" value="paper" on click=" {document. myform. mypaper.value=this.value;location. href='hello';}" />
</form>

在Servlet中

  String val=req.getParameter('mypen'); 
  String val1=req.getParameter("mypaper");

当时只有一个值null。您可以使用null check执行此操作。

答案 2 :(得分:0)

将按钮的值编码为按钮的名称。

在HTML中:

<p><input type="button" name="good:Pen" onclick="location.href='hello';"> </p>
<p><input type="button" name="good:Paper" onclick="location.href='hello';">

在servlet中:迭代所有参数,查找startsWith("good")的参数,如果是,则减去前缀good:。如果您的页面中没有其他提交按钮,则只需为PenPaper命名按钮,并检查这些参数是否存在。此任务不需要javascript。

答案 3 :(得分:0)

在jsp中编写此代码,对您有所帮助

 <html>
 <body style="background-color:black;">

 <p><form method="post" action="hello">
 <p><input type="submit" name="good" value="Pen" > </p>
 <p><input type="submit" name="good" value="Paper" >
 </p></form>

 </body> 
 </html>