aspose file tools*
The moose likes Servlets and the fly likes how many times the buttons are pressed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how many times the buttons are pressed" Watch "how many times the buttons are pressed" New topic
Author

how many times the buttons are pressed

Aleksandra Anakijeva
Greenhorn

Joined: Aug 15, 2013
Posts: 2
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

Joined: Mar 22, 2000
Posts: 12803
    
    5
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

Joined: Jan 24, 2012
Posts: 38

have to play with HttpSession and a very little javascritpt. See image



[Thumbnail for Servlet.JPG]



Sorry for my English, I'll improve
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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

Joined: Aug 15, 2013
Posts: 2
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

Joined: Sep 03, 2013
Posts: 33




use two html hidden types....

then you can get the values with request.getParameter..


SCJP
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

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.


SCJP, SCWCD.
|Asking Good Questions|
Rajesh Vassey
Greenhorn

Joined: Apr 28, 2008
Posts: 8

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


--SCJP,SCWCD--
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many times the buttons are pressed