jsp - javascript communication

hi
following is the code

<% if(session.getAttribute("setS")!=null)
{
        String attval = (String) session.getAttribute("setS");
%>
<script>
var tempS =  "<%= attval %>";
DDSignControl1.UpdateData(tempS,tempS.length); // my functions
if(DDSignControl1.SelectCertificateFromUI())  //show certificate selection box
{
      var signa =DDSignControl1.Sign();  //sign
      form.submit();                              // submit
}      
</script>
<%      
}
session.removeAttribute("setS");
}
%>

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
LVL 4
illusionz70Asked:
Who is Participating?
 
MogalManicConnect With a Mentor Commented:
Sorry, I wrote the code too quickly (Should have wrote it inside my IDE):
  private String escapeNL(String str)
  {
    final String[] escapeChars={"\n", "\r"};
    final String[] replChars={"\\n","\\r"};
    StringBuffer results=new StringBuffer(str);
    boolean done=false;
    for (int i=0;i<escapeChars.length;i++) {
      int pos;
      while((pos=results.indexOf(escapeChars[i]))>-1) {
        results.replace(pos, pos+1, replChars[i]);
      }
    }

    return results.toString();
  }

Mistakes I made:
  -Misspelld replChars
  -replaceChar()  sb replace()
  -Used wrong object in replace() call
  -IndexOf() only takes String arguments
0
 
girionisCommented:
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.
0
 
MogalManicCommented:
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>

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
illusionz70Author Commented:
vector contains strings.
also i need to sign all the strings seperately not as one string.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can store them into an array like suggested by MogalManic.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
But instead of using toString () and split it, its better to loop through the Vector and then get the Strings.
0
 
girionisConnect With a Mentor Commented:
What I would do would be:

<% if(session.getAttribute("setS")!=null)
{
        Vector attval = (Vector) session.getAttribute("setS");
%>
<script>
<%
    for (int i=0; i<attval.zise(); i++)
    {
%>
        var tempS =  "<%= attval.elementAt(i) %>";
        DDSignControl1.UpdateData(tempS,tempS.length); // my functions
        if(DDSignControl1.SelectCertificateFromUI())  //show certificate selection box
        {
            var signa =DDSignControl1.Sign();  //sign
        }    

<%  
    }  
%>
    form.submit();                              // submit
    </script>
<%
}
session.removeAttribute("setS");
}
%>

I might have some syntax errors. Just ignore them and follow the logic.
0
 
illusionz70Author Commented:
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
0
 
girionisCommented:
Then have a drop down menu and put all the vector's info in there.
0
 
illusionz70Author Commented:
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.
0
 
illusionz70Author Commented:
MogalManic
:
the string i get in the vector may already have ',' .. will it still work??

thanks
0
 
girionisCommented:
You just need a for loop like the sample code in my question.
0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
>> better to loop through the Vector and then get the Strings.
>> well how to do that ??

The way girionis has shown in his code.

>> the string i get in the vector may already have ',' .. will it still work??

The toString () approach will not work in that case.
0
 
illusionz70Author Commented:
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...
0
 
illusionz70Author Commented:
have got it working pretty much...

==>should have been

have almost got it to wrk... :)
0
 
girionisCommented:
Just get rid of the characters that you do not want. Use regular expressions to do it.
0
 
illusionz70Author Commented:
i want the whole string .... coz i will sign the whole string intact with the newline characters also.....

0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
girionisCommented:
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.
0
 
illusionz70Author Commented:
is there any way i can store the entire string in a javascript variable along with the newline ??
0
 
illusionz70Author Commented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I'm not sure if it would work, but if I could, well and good ;-)
0
 
MogalManicCommented:
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(escapeChars.charAt(i))>-1) {
            escapeChars.replaceChars(pos, pos+1, replChars[i]));
        }
    }

   return results.toString();
}
%>
...
>> tempS[<%=k%>] =  "<%=escapeNL(attval.elementAt(k).toString())%>";
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I guess the newline characters would anyway be stored as \n in the String obtained by a call to attval.elementAt(k).toString() ?
0
 
illusionz70Author Commented:
MogalManic

your code doesnt compile.
0
 
girionisCommented:
What is the error message?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> i<escapeChars.length

i < escapeChars.length ()
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Better to store it in a variable rather than call the length () method everytime.

int len = escapeChars.length () ;

for ( i = 0 ; i < len ; i ++ )
0
 
illusionz70Author Commented:
THAT I FIGURED...
it says something abt misplaced ")"
also i dont think there is
replaceChars(pos, pos+1, replChars[i]));


0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> THAT I FIGURED...

Then what is the exact error? Can you post the message and the line at which it occurs?

>> escapeChars.replaceChars(pos, pos+1, replChars[i]));

There is an extra ) at the end over there. Remove it.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> replChars

would be relChars.
0
 
illusionz70Author Commented:
is replaceChars a method in java ... my compiler complains . i am using jdk1.3.1
0
 
Mayank SAssociate Director - Product EngineeringCommented:
AFAIK, its not a member of the String class. And:

>> escapeChars.replaceChars(pos, 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] ) ;
0
 
illusionz70Author Commented:
thanks yo all
0
 
girionisCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.