2

Jsp with two buttons

<%@ 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>

This is the 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"); 
    }  

}
 }

My code is not giving the correct output on clicking the buttons. I want when i click Pen then it should enter in if() and print the text and i want same for the paper button.

Sandeep
  • 173
  • 2
  • 3
  • 13

4 Answers4

1

write this code in jsp, it will be helpful to you

 <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>
Manohar Ch
  • 453
  • 1
  • 3
  • 22
0

Add two hidden field.

<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>

In Servlet

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

At time there is only one value other is null. You can do this with null check.

sar
  • 1,277
  • 3
  • 21
  • 48
0

Use jQuery to do your task.

Change your html code to these lines of code.

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

And add these lines in your script

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

You can use jQuery 1.7.1 and above.

halfer
  • 19,824
  • 17
  • 99
  • 186
Bharathi
  • 451
  • 1
  • 6
  • 17
  • Hey i have no knowledge of JQuery. Can u please explain in html only. – Sandeep Dec 16 '13 at 08:53
  • If you want to use only html, you need to use radio buttons. I am not sure if there is other way. jquery is easy to use. You jus copy paste my jquery code into script tag of html. for reference you can refer a sample jquery example. [jquery example Click Me](http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_hide) follow this link. – Bharathi Dec 16 '13 at 09:22
-1

Encode the button's value into button's name.

In HTML:

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

In servlet: iterate over all parameters, look for parameter which startsWith("good") and if yes, subtrack the prefix good:. If there are no other submit buttons in your page, you can simply name your buttons just Pen and Paper and check the presence of these parameters as well. There is no javascript needed for this task.

Kojotak
  • 2,020
  • 2
  • 16
  • 29