aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
Author

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 SaveChanges.java servlet are used.
SaveChanges.java 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--%>

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

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

<%Requests req=(Requests)session.getAttribute("SHARES");
req.calPurchase();%>
<%--Requests.java 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())%>;
if(balamt-puramt<500)
{
alert("Insufficient Balance");
return false;
}

}
</script>

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

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



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

</body>
</html>

cheers,
Poonam K.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

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]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new values of javascript variables not being considered for 2nd request
 
Similar Threads
JavaScript in JSP
send variables along with html content
How to reference a variable of jsp in the script function?
Calling JNDI from JSP
Storing field value in java properties file.