Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many times the buttons are pressed

 
Aleksandra Anakijeva
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to write one html code where i have two simpe buttons and one submit and when i click on submit servlet is generate which shows how many times the buttons are pressed?

For example i pressed first button1, then two times button2, then 3 times button1 and when i pressed submit the servlet shows "You pressed 4 times button1 and 2 times button2"
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Counting button clicks would have to be done by Javascript running in the HTML page.

Servlets only know about requests.

Bill
 
Jhonnathan Emilio Cardona Saineda
Ranch Hand
Posts: 38
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have to play with HttpSession and a very little javascritpt. See image
Servlet.JPG
[Thumbnail for Servlet.JPG]
Servlet
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give that requirement, each "button press" must execute an HTTP request - therefore it must be a type = SUBMIT button in a HTML form and have a unique name so you can count the press.

Every request is going to generate a new HTML page.

This is sounding more and more like a homework assignment, tch!

Bill
 
Aleksandra Anakijeva
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with javaScript but nothing happened.

this is my script
out.println("<script language='javaScript'>");
out.println("function Add(int a) { return ++a;}");
out.println("</script>");

out.print("<input type='button' value='Press='butt"+i+"' onclick='Add(array[i])' ></p> ");

I use array[i] because i don't know how much buttons i have so when i click the second button array[1] will increment.
but when i click submit nothing happen

j=(Integer)(request.getAttribute("niza[i]"));
this is is the code of final servlet.
 
Sourabh Bawage
Ranch Hand
Posts: 33
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



use two html hidden types....

then you can get the values with request.getParameter..
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aleksandra Anakijeva wrote:
this is my script
out.println("<script language='javaScript'>");
out.println("function Add(int a) { return ++a;}");
out.println("</script>");
...........
this is is the code of final servlet.


Write a plain HTML page for displaying the buttons and add Javascript to it. Let the page submit to the servlet.
DO NOT write Javascript in servlet, never.
 
Rajesh Vassey
Greenhorn
Posts: 8
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This can be done in many ways.
My suggestion is use jquery button click events for both buttons.
Have two global variables corresponding to click counts of the two buttons.
When you click on final submit button then submit the variables using ajax request to servlet.
At servlet try to get the params and process them
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic