permaculture playing cards*
The moose likes JSP and the fly likes send variables along with html content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "send variables along with html content" Watch "send variables along with html content" New topic
Author

send variables along with html content

Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
Hi everybody,
A JSP is converted to a servlet, and a response is sent as a html file. If I�m using a JSP to send the response can I send variables (like customer�s purchase amount so far and customer�s balance amount) along with the html content in the response and then use it to alert the customer if he /she purchases more items and his/her purchase amount is greater than the balance amount.

cheers,
Poonam K.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60776
    
  65

Originally posted by Poonam Kadu:
If I�m using a JSP to send the response can I send variables ...


Depends what you mean by "variables".

If you mean Java variables, then of course not. What would deal with them on the client side? Remember, from the point of view of the browser, it's getting an HTML page just like any other. The fact that it was generated with JSP is moot.

If you mean Javascript variables, then yes. You can include Javascript variables initialized to whatever value you'd like in the generated page markup.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
Thanks for your explanation.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.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
You need to set the new amount value to request object after modification and before submitting the page to display.
Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
In DisReq.jsp I'm using a shopping cart like object of class Requests.java which contains a method calPurchase() to calculate the purchase amount

<%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--%>

When SaveChanges.java forwards the httprequest to DisReq.jsp ,I think it will recalculate the values for script variables puramt,balamt instead of considering the old values

cheers ,
Poonam K.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Poonam Kadu:
<%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--%>


It doesn't qualify to be called MVC at all.
Poonam Kadu
Ranch Hand

Joined: Jul 21, 2006
Posts: 49
The Requests.java class is not only used by DisReq.jsp but primarily used by other servlets .

So can you please explain me why Requests.java doesn't qualify to be a model of MVC structure.

cheers,
Poonam K.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
As you said your request.calPurchase() method calculates the purchase amount, hence I would call it M of MVC. Calling your M from V is Model 1 not MVC. It should be called through your controller.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
For this particular issue what you need is to set the new purchase amount accordingly after calculation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: send variables along with html content
 
Similar Threads
Writing JSP response content to file
authoring functional specification
new values of javascript variables not being considered for 2nd request
Ajax invalid Jason response with current page HTML
Javascript issue when loading the JSP page content via an AJAX call