permaculture playing cards*
The moose likes JSP and the fly likes new values of javascript variables not being considered for 2nd request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "new values of javascript variables not being considered for 2nd request" Watch "new values of javascript variables not being considered for 2nd request" New topic

new values of javascript variables not being considered for 2nd request

Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
(Sorry for posting the same content under a different subject.)

Dear Bear Bibeault,
Thanks for your explanation regarding "sending variables along with html content".Now I�ve got a clear idea of what a browser understands and what it doesn�t . But now I�ve another problem�
When the user clicks on the checkout button for the first time , if purchase amount is greater than balance then the alert message �Insufficient Balance� is displayed.
Seeing this alert message the user modifies his/her purchase requests so that purchase amount is less than balance.
To modify a (purchase/sell)request , Modify.jsp and servlet are used. makes the required changes to the Requests object(it�s a Model, shoppingcart kind of object) and forwards the httprequest to DisReq.jsp

When the user clicks on checkout button(for the 2nd time) after modifying purchase requests, the browser uses the old values of script variables puramt and balamt (used during first checkout) to check whether purchase amount< balance

<%--DisReq.jsp displays the Buy/Sell stocks requests so far placed by the user--%>
<%--Allows the user to remove or modify a desired request, place more requests and checkout--%>

<body bgcolor="#6495ed" text="#000000">

<%@ page import="java.util.*" %>
<%@ page import="client.model.*" %>

<%Requests req=(Requests)session.getAttribute("SHARES");
< is a model (MVC) which acts like a shopping cart
req.calPurchase() calculates the purchase amount--%>
<script language="javascript" >
function checkBal()
<%--req.getAmt() and req.getBal() return purchase amount and balance respectively--%>
var puramt = <%=req.getAmt()%>;
var balamt=<%=Integer.parseInt(req.getBal())%>;
alert("Insufficient Balance");
return false;


<form method="POST" action="" onSubmit=" return checkBal()" >
<input type="submit" value="Checkout"><br><br>
<a href="Reqst.html">Place More Requests</a>

<form method="POST" action="Modify.jsp" >
<%--code to display requests--%>
<input type="submit" name="action" value="Modify" >

<form method="POST" action="Remove.jsp" >
<%--code to display requests--%>
<input type="submit" name="action" value="Remove Request">


Poonam K.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60782

Every time you submit to the server and send a page to the browser, the whole page, including its Javascript is re-interpreted. There is no way to retain Javascript variable values across requests unless you persist them to the server. Frequently the session is used to hold data between requests.

You might also investigate Ajax which will let you post back to the server without a full-page reload. But bear in mind that the user can undo everything by simply hitting the refresh button.

Bottom line: all important data must be presisted to the server and sent to the browser as necessary.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
wood burning stoves
subject: new values of javascript variables not being considered for 2nd request
Similar Threads
How to reference a variable of jsp in the script function?
Calling JNDI from JSP
Storing field value in java properties file.
send variables along with html content
JavaScript in JSP