• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to add up values from a for looped textbox in JSP

 
Sohaila Razali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Is there a way to add values from a textbox in a for loop? the following is part of the code in my jsp page

<%
DetailsMod bean = null;
List resultList = (List) session.getAttribute("list");
int count1=-1;
String value2 = "";
if(resultList.size() > 0 ) {
int rowNum = 1;
for(int i=0; i<resultList.size(); i++){
bean = (DetailsMod) resultList.get(i);
%>
<input type="text" name="tbx_cost" value="<%=bean.getCost()%>"/>

<% count1 = i;
}
}%>

lets say the resultList returns 2 as value, then there will be 2 textboxes.
if user were to input 2000 in the first box and 3000 in the second box, is there a way for me to add these values up to 5000 and store it in a variable to pass it to another page?
Thanks in advance for any possible help! :)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about it for a minute. The JSP code executes on the server in order to create the HTML page to send to the browser. So do you think that there is something you can do with JSP code that executes on the client after the response has been sent? Of course not. If this is not a familiar concept, please read this article to understand what JSP is all about.

You'll need to use JavaScript on the page to add up the values when the user enters them, or submit a form to a servlet which can get the values and add them.

By the way, putting Java code in JSP is a bad practice that has been obsolete for over 12 years now. You should be using the modern JSTL and EL, not Java scriptlets.
 
Sohaila Razali
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thnks for the reply.

I'm totally new to JSP btw. The existing codes that the company gave me all consist of java scriptlets. So If I were to change the part that i'm working on with what you mentioned and leave the old codes be, will it affect the entire application?

Given my current code, how do I convert it to pure JSP without the scriptlets? btw I'm using both servlet and javascript as well.

Last but not least, do you happen to know an article or site on JSP that would be good for beginner like me to explore?

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would not advise trying to convert legacy paces.

The concept that you need to understand is that the JSP code executes on the server before the page is sent to the browser. So you cannot add up the numbers as the user enters them in the JP code of the page. You need to submit the values to the next servlet or page where the code can get the values and add them up. Or use JavaScript which runs on the browser.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!