illusionz70
asked on
jsp - javascript communication
hi
following is the code
<% if(session.getAttribute("s etS")!=nul l)
{
String attval = (String) session.getAttribute("setS ");
%>
<script>
var tempS = "<%= attval %>";
DDSignControl1.UpdateData( tempS,temp S.length); // my functions
if(DDSignControl1.SelectCe rtificateF romUI()) //show certificate selection box
{
var signa =DDSignControl1.Sign(); //sign
form.submit(); // submit
}
</script>
<%
}
session.removeAttribute("s etS");
}
%>
basically here i get some values from session ( which is a string) , i perform some signing on the string (which is done in javascript ) and then submit the form.
there are 2 issues here which is where i want your help
1 : ifinstead of string i get a vector(of strings) how do i assign the vector to javascript.
the certificate chooser box should be displayed only once. so i need the sign the content in a loop.
2 : how do i submit this signed content to servlet. ( hidden fields are the first thing that come to my mind)
looking forward for your inputs.
thanks
following is the code
<% if(session.getAttribute("s
{
String attval = (String) session.getAttribute("setS
%>
<script>
var tempS = "<%= attval %>";
DDSignControl1.UpdateData(
if(DDSignControl1.SelectCe
{
var signa =DDSignControl1.Sign(); //sign
form.submit(); // submit
}
</script>
<%
}
session.removeAttribute("s
}
%>
basically here i get some values from session ( which is a string) , i perform some signing on the string (which is done in javascript ) and then submit the form.
there are 2 issues here which is where i want your help
1 : ifinstead of string i get a vector(of strings) how do i assign the vector to javascript.
the certificate chooser box should be displayed only once. so i need the sign the content in a loop.
2 : how do i submit this signed content to servlet. ( hidden fields are the first thing that come to my mind)
looking forward for your inputs.
thanks
What does the vector contain? What type of objects? If they are strings you can concatenate all of them and then assign them to the javascript variable.
It is fairly simple. Just convert the string representation of the vector to the JavaScript Array. The Vector class has a toString() method which will return a string like this:
[item1,item2,...,itemN]
so in Javascript remove the [] chars and call split(',') to convert the string back to an Array().
You need to store signature results on the form regardless if it is a String or Array, so your JSP page will have to create hidden fields
for each expected result. Maybe something like this:
<form name='form'...>
<%for(int i=0;i<attVector.size();i++ ) {%>
<input type='hidden' name='signa' value='<%=attVector.get(i) %>/>
<%}%>
...Other form data...
</form>
[item1,item2,...,itemN]
so in Javascript remove the [] chars and call split(',') to convert the string back to an Array().
You need to store signature results on the form regardless if it is a String or Array, so your JSP page will have to create hidden fields
for each expected result. Maybe something like this:
<form name='form'...>
<%for(int i=0;i<attVector.size();i++
<input type='hidden' name='signa' value='<%=attVector.get(i)
<%}%>
...Other form data...
</form>
Don't rely on toString () ;-) what if its implementation changes tomorrow ;-) ? Might be better off by taking out each element and then concatenating its String form.
ASKER
vector contains strings.
also i need to sign all the strings seperately not as one string.
also i need to sign all the strings seperately not as one string.
You can store them into an array like suggested by MogalManic.
But instead of using toString () and split it, its better to loop through the Vector and then get the Strings.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mayank : better to loop through the Vector and then get the Strings.
well how to do that ??
gironis :
so the certificate selection will pop up evertime in the loop i want him to select certificate only once.
thanks
well how to do that ??
gironis :
so the certificate selection will pop up evertime in the loop i want him to select certificate only once.
thanks
Then have a drop down menu and put all the vector's info in there.
ASKER
no .. thats not what i meant.
the vector contains strings whcih have to be signed with a certificate.
i need to pop up the certificate selection box , from which the user will select a certificate.now with this certificatre i need to sign the vector contents one by one.
store all these signatures and then send it to a servlet.
the vector contains strings whcih have to be signed with a certificate.
i need to pop up the certificate selection box , from which the user will select a certificate.now with this certificatre i need to sign the vector contents one by one.
store all these signatures and then send it to a servlet.
ASKER
MogalManic
:
the string i get in the vector may already have ',' .. will it still work??
thanks
:
the string i get in the vector may already have ',' .. will it still work??
thanks
You just need a for loop like the sample code in my question.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
have got it working pretty much...
<% for(int k=1;k<attval.size();k++)
{
%>
alert("inside k loop"); //javascript part...
tempS[<%=k%>] = "<%= attval.elementAt(k)%>";
alert(tempS[<%=k%>]);
<%
}
%>
the problem here is that ... if "<%= attval.elementAt(k)%>"; contains a string which contains new line , the code gives error . here tempS is the javascript array...
for eg in the veiw source of the jsp
tempS[1] = "{IFN0}{null}{null}{
-}";
this gives a java script error saying that the line is not properly ended...how do i get around this...
<% for(int k=1;k<attval.size();k++)
{
%>
alert("inside k loop"); //javascript part...
tempS[<%=k%>] = "<%= attval.elementAt(k)%>";
alert(tempS[<%=k%>]);
<%
}
%>
the problem here is that ... if "<%= attval.elementAt(k)%>"; contains a string which contains new line , the code gives error . here tempS is the javascript array...
for eg in the veiw source of the jsp
tempS[1] = "{IFN0}{null}{null}{
-}";
this gives a java script error saying that the line is not properly ended...how do i get around this...
ASKER
have got it working pretty much...
==>should have been
have almost got it to wrk... :)
==>should have been
have almost got it to wrk... :)
Just get rid of the characters that you do not want. Use regular expressions to do it.
ASKER
i want the whole string .... coz i will sign the whole string intact with the newline characters also.....
>> tempS[<%=k%>] = "<%= attval.elementAt(k)%>";
Try setting a hidden variable with that value in the form, and set the value of tempS[1] with that hidden variable.
Try setting a hidden variable with that value in the form, and set the value of tempS[1] with that hidden variable.
I am not sure what we could do. A possible solution is to get rid of the newline character, hold the index of where it is and upon submitting put it there again.
ASKER
is there any way i can store the entire string in a javascript variable along with the newline ??
ASKER
mayank ::
oopsy .. didnt notice your comment seems like a good idea...i was doin the same but in the opposite order u suggested..
will try that and get back.
oopsy .. didnt notice your comment seems like a good idea...i was doin the same but in the opposite order u suggested..
will try that and get back.
I'm not sure if it would work, but if I could, well and good ;-)
Try mayankeagle's suggestion.
>> tempS[<%=k%>] = "<%= attval.elementAt(k)%>";
If that doesn't work you will have to write a Java routine that converts the newline characters to \n and/or \r:
<%!
private String escapeNL(String str)
{
final String escapeChars="\n\r";
final String[] relChars={"\\n","\\r"};
StringBuffer results=new StringBuffer(str);
boolean done=false;
for (int i<0;i<escapeChars.length;i ++) {
while((int pos=results.indexOf(escape Chars.char At(i))>-1) {
escapeChars.replaceChars(p os, pos+1, replChars[i]));
}
}
return results.toString();
}
%>
...
>> tempS[<%=k%>] = "<%=escapeNL(attval.elemen tAt(k).toS tring())%> ";
>> tempS[<%=k%>] = "<%= attval.elementAt(k)%>";
If that doesn't work you will have to write a Java routine that converts the newline characters to \n and/or \r:
<%!
private String escapeNL(String str)
{
final String escapeChars="\n\r";
final String[] relChars={"\\n","\\r"};
StringBuffer results=new StringBuffer(str);
boolean done=false;
for (int i<0;i<escapeChars.length;i
while((int pos=results.indexOf(escape
escapeChars.replaceChars(p
}
}
return results.toString();
}
%>
...
>> tempS[<%=k%>] = "<%=escapeNL(attval.elemen
I guess the newline characters would anyway be stored as \n in the String obtained by a call to attval.elementAt(k).toStri ng() ?
ASKER
MogalManic
your code doesnt compile.
your code doesnt compile.
What is the error message?
>> i<escapeChars.length
i < escapeChars.length ()
i < escapeChars.length ()
Better to store it in a variable rather than call the length () method everytime.
int len = escapeChars.length () ;
for ( i = 0 ; i < len ; i ++ )
int len = escapeChars.length () ;
for ( i = 0 ; i < len ; i ++ )
ASKER
THAT I FIGURED...
it says something abt misplaced ")"
also i dont think there is
replaceChars(pos, pos+1, replChars[i]));
it says something abt misplaced ")"
also i dont think there is
replaceChars(pos, pos+1, replChars[i]));
>> THAT I FIGURED...
Then what is the exact error? Can you post the message and the line at which it occurs?
>> escapeChars.replaceChars(p os, pos+1, replChars[i]));
There is an extra ) at the end over there. Remove it.
Then what is the exact error? Can you post the message and the line at which it occurs?
>> escapeChars.replaceChars(p
There is an extra ) at the end over there. Remove it.
>> replChars
would be relChars.
would be relChars.
ASKER
is replaceChars a method in java ... my compiler complains . i am using jdk1.3.1
AFAIK, its not a member of the String class. And:
>> escapeChars.replaceChars(p os, pos+1, replChars[i]));
- attempts to call it on 'escapeChars' (String). I wonder why. String is immutable, so even if that method was there, it would not modify the original String ;-)
I guess it was supposed to be:
results.replace ( pos, pos+1, relChars[i] ) ;
>> escapeChars.replaceChars(p
- attempts to call it on 'escapeChars' (String). I wonder why. String is immutable, so even if that method was there, it would not modify the original String ;-)
I guess it was supposed to be:
results.replace ( pos, pos+1, relChars[i] ) ;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks yo all
:)