Solved

jsp - javascript communication

Posted on 2004-08-26
36
980 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:illusionz70
  • 13
  • 12
  • 8
  • +1
36 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11900968
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
 
LVL 21

Expert Comment

by:MogalManic
ID: 11901043
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11901079
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11901425
vector contains strings.
also i need to sign all the strings seperately not as one string.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11901569
You can store them into an array like suggested by MogalManic.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11901578
But instead of using toString () and split it, its better to loop through the Vector and then get the Strings.
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 75 total points
ID: 11901592
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11901929
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
 
LVL 35

Expert Comment

by:girionis
ID: 11902000
Then have a drop down menu and put all the vector's info in there.
0
 
LVL 4

Author Comment

by:illusionz70
ID: 11909745
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11910076
MogalManic
:
the string i get in the vector may already have ',' .. will it still work??

thanks
0
 
LVL 35

Expert Comment

by:girionis
ID: 11910235
You just need a for loop like the sample code in my question.
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 75 total points
ID: 11910461
>> 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
 
LVL 4

Author Comment

by:illusionz70
ID: 11931207
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11931217
have got it working pretty much...

==>should have been

have almost got it to wrk... :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11931809
Just get rid of the characters that you do not want. Use regular expressions to do it.
0
 
LVL 4

Author Comment

by:illusionz70
ID: 11939053
i want the whole string .... coz i will sign the whole string intact with the newline characters also.....

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11939269
>> 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 35

Expert Comment

by:girionis
ID: 11939274
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11939646
is there any way i can store the entire string in a javascript variable along with the newline ??
0
 
LVL 4

Author Comment

by:illusionz70
ID: 11939688
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11939727
I'm not sure if it would work, but if I could, well and good ;-)
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 11940165
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11940572
I guess the newline characters would anyway be stored as \n in the String obtained by a call to attval.elementAt(k).toString() ?
0
 
LVL 4

Author Comment

by:illusionz70
ID: 11940957
MogalManic

your code doesnt compile.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11941353
What is the error message?
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11941474
>> i<escapeChars.length

i < escapeChars.length ()
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11941485
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11941516
THAT I FIGURED...
it says something abt misplaced ")"
also i dont think there is
replaceChars(pos, pos+1, replChars[i]));


0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11943661
>> 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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11943679
>> replChars

would be relChars.
0
 
LVL 4

Author Comment

by:illusionz70
ID: 11949417
is replaceChars a method in java ... my compiler complains . i am using jdk1.3.1
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 11949817
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
 
LVL 21

Accepted Solution

by:
MogalManic earned 100 total points
ID: 11950681
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
 
LVL 4

Author Comment

by:illusionz70
ID: 11989550
thanks yo all
0
 
LVL 35

Expert Comment

by:girionis
ID: 11989555
:)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now