Scriptlet problem..
Hello guys/gals,
I'm having some problem with scriptlet.
When i hit on the ADD button, it will go to the javascript, print the 'count' value (e.g. 0) and then add 1 to count and reprint the new 'count' value(e.g. 1). This works fine.
However, the problem comes when i hit the ADD button again. Instead of continuing from 1 and then 2, it resets to 0 and then 1. Any ideas? i'm lost in a world of my own.
-- CODE --
<script language="javascript">
<%! private int count = 0; %>
function addition() {
alert("Before Count = <% out.print(count);%>");
<% count++; %>
alert("After Count = <% out.print(count);%>");
}
</script>
<body>
.....
<html:button property="submitProperty" onclick="javascript:addition()">
<bean:message key="button.add"/>
</html:button>
......
</body>
-- END CODE --
[982 byte] By [
-TiKuS-a] at [2007-11-26 13:07:54]

# 1
It is bound to happen & thats how JSP is made..
all the scriptlet code is being executed at the server side. You would only find the reponse part at the clientend it cannotbe modified if wanna do that again a request has to be sent to the server.
just for your confirmation just checkout the present code from browser's view source part.
you find a HTML SOURCE something similar to the one below.
<script language="javascript">
function addition() {
alert("Before Count = 0");
alert("After Count = 1");
}
</script>
<body>
......
<input typ="button" name="submitProperty" onclick="javascript:addition()">
......
</body>
looking at your javascript function
function addition() {
alert("Before Count = 0");
alert("After Count = 1");
}
i don't think there is any dynamic component found there all you have is a set of static values.
But if you restructure your code in the following way where you are taking help of javascript variable it'd surely work
<script language="javascript">
<%! private int count = 0; %>
var count = <% out.print(count);%>;
function addition() {
alert("Before Count = "+count);
count = count + 1;
alert("After Count = "+count);
}
</script>
<body>
.....
<html:button property="submitProperty" onclick="javascript:addition()">
<bean:message key="button.add"/>
</html:button>
......
</body>
Hope this helps....
REGARDS,
RAHUL
# 2
Thanks alot.
I did consider using javascript variables but there is another problem with it. Let say in my javascript function i need to generate a jsp tag:
function generate() {
var str = '<div align="left"><html:text size="10" maxlength="20" name="salesForm" property='<%=\"salesSubForm[\" + count +\"].status\"%>'/></div>';
}
in the code above, the count was the counter which i had problem in my first posting. If i use javascript, the count variable will lose its numerical value and be treated as a string variable with value "count" instead of a numerical value.
That's why i tried considering using scriplets and it works but had problem as u mention its the was jsp workds with scriptlets.
any idea or any way i can escape the count javascript variable?
# 3
>in the code above, the count was the counter which i had problem in >my first posting. If i use javascript, the count variable will lose its >numerical value and be treated as a string variable with value "count" >instead of a numerical value.
No it wud not
var count = <%=count>
wud input var count = 10 not var count = '10' at the client side you can as well increment it.
and coming about
'<html:text size="10" maxlength="20" name="salesForm" property='<%=\"salesSubForm[\" + count +\"].status\"%>'/>'
is again a taglib which gets compiled and converted @ server side not at the client side... instead make use of classic html <input type='text'>
again usage of cutomized taglibrary..... friend this can only be used at the server side scripting..... what servlet container does is it has to
# 4
I think i sorta understand the client server thingy. I tried using the <input type="text" .... > but the problem still remains..
If i declare:
var count = <%=count%>;
then the declaration below:
cell1.innerHTML='<div align="left"><html:text size="10" maxlength="20" name="salesForm" property='<%=\"salesSubForm[\" + count +\"].statusDate\"%>'/></div>';
then count is a variable in javascript and i cannot insert it into the scriptlet above cause count was not declared as a Java variable.
how do u overcome this?