0

I have a form , i want to input numbers one at a time.

I create an object and pass the getParameter() values to it,

   <form action="index.jsp" name="form" method="GET">
    <input type="text" value="" name="input" />
    <input type="hidden" name="hiddenCounter"  value="" />  
    <%
    String input = request.getParameter("input");
    String hiddenCounter = request.getParameter("hiddenCounter");               
    control.MainProgram main = new control.MainProgram(input, hiddenCounter);
    %>           

    <input type="submit" value="Submit Numbers" />
    <% out.println(main.getResult()); %>
    </form>  

The constructor in a java class parses the values to int and sets the variables

public MainProgram(String input, String hiddenCounter) {    

    try {         
       number = Integer.parseInt(input);           
       counter = Integer.parseInt(hiddenCounter);           
    } catch (NumberFormatException e) {
    }        
}


public int getResult() {
    return number;
}

how can i sum up the numbers each time a number is submitted?

The problem is every time the constructor is called it sets the variable to the numbers submitted.

something like

number+=number; 

dosent work ( cause its resetting the variable )

ive searched & searched can someone help ?

Aaron
  • 75
  • 8
  • Scriptlets `<% ... %>` in JSP have been deprecated for eight years. Here's an article from 2006 explaining why they're bad and what you can use instead. http://www.javaranch.com/journal/200603/Journal200603.jsp#a5 Of course, there are more modern alternative to the article. – Stephen P Oct 10 '14 at 16:49

2 Answers2

0

If you're not worried about multi-threaded access, you can simply declare number static.

The variable will be bound to the control.MainProgram class rather than its instance.

You then...

try {         
       number += Integer.parseInt(input); 

... then declare getResult static as well...

... then invoke it by <% out.println(control.MainProgram.getResult()); %>

Note that this doesn't give you the average, as your title implies - it gives you the sum.

But it's simple enough to implement an average in the body of your static int getResult method.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • sorry again , but how would i then add hidden field counter and execute the code after a certain number of submissions ? – Aaron Oct 10 '14 at 16:53
  • @Aaron the exact same way. Declare `counter` as `static`, the calculate the average based on `counter` and the summed `number` in your `static getResult` method. – Mena Oct 10 '14 at 17:21
0

You have to pass the running total of the sum into the session variable. You can do this by getting the session like this HttpSession session = request.getSession(); session.setAttribute("UserName", username); You can then get the attribute by using the getAttribute() function. That should work for concurrent users. If you need more help

Joshua
  • 222
  • 1
  • 5
  • Thanks mate ! ill try that once i can get a hidden field counter working :) – Aaron Oct 10 '14 at 16:54
  • I believe that you are going about this the wrong way. You don't need the hidden field counter you can just store the count in the session as well. The session is like a grab bag for variables between jsp pages and servlets. It should be used over hidden form fields. Unless you expect the person to bookmark a certain page or stay on that page for 24+ hours – Joshua Oct 10 '14 at 17:02