0

In my jsp page I'm having some javascript code,to take all the values that are entered in textboxes. And i'm putting those in array then passing to next page. This is my javascript function

function gt2()
{
    var pqr="100";
    var arr=new Array();
    var x=<%=height%>;
    var attstr=null;
    for(var t=0; t<x; t++)
    {
        var a="inputText"+t;
           
        var e=document.getElementById(a);
        var val= e.value;
            
        arr[t]=val;
                
        if(t==0)
        {
            attstr=arr[t]+",";
        }
                
        if((t!=x)&&(t!=0))
        {
            if(t==x-1)
            {
                attstr+=arr[t];
            }
            else
            {
                attstr+=arr[t]+",";
            }
        }
    }
    window.location.assign("gt_Iba2?value="+attstr);
}    

In the second jsp page I'm getting that value.

String newValue=request.getParameter("value");
System.out.println("!###The new value in second jsp is "+newValue);
String[] allvalues=newValue.split(",");
System.out.println("The length of allvalues is "+allvalues.length);

I have five textboxes in my first jsp. My code is correctly picking the values from those textboxes and passing to other page only if it doesn't contain some special characters. img1

Output for above condition is fine for me.

!###The new value in second jsp is A|B|C|Dbcdrt,A|B|C|Dbcdrt,A|B|C|Dbcdrt,A|B|C|Dbcdrt,A|B|C|Dbcdrt
The length of allvalues is 5

So when I'm adding any characters like #,&,% the array is breaking.Means it is not taking other values after that character.

In this caseimg2

I have added '&' symbol on third textbox. So I'm getting output like this

!###The new value in second jsp is A|B|C|Dbcdrtdf,A|B|C|Dbcdrtdf,A|B|C|Dbcdrtdf 
The length of allvalues is 3

After that '&' symbol it is not showing any value. The size is also three.If I add this in second text box the size will be two.Why this behaviour on these symbols?I need to pass those symbols also.What wrong am I doing here?I need some help

Thanks

2 Answers 2

1

The & symbol seperates the get parameters in the url (parameter1=value&parameter2=value2). So everything after the symbol is treated as a second parameter.

If you use the javascript function encodeURIComponent to encode attrstr then it should work.

Sign up to request clarification or add additional context in comments.

3 Comments

Same behaviour is for '#' symbol also.?
Not exactly, the # symbol is for link targets in a webside, like that one when you click on a notification at the top of this side it jumps directly to the answer/comment. This isn't even sent to the server.
? and % can break things too
1

The problem you have is that the characters #&%? are all reserved characters when used as part of a URI/URL. You should use:

var val= encodeURIComponent (e.value);

this will URI escape those characters and the URI will remain legal.

Personally I would use Array.join to concatenate your array with commas too.

    function gt2()
    {
        var pqr="100";
        var arr=new Array();
        var x=<%=height%>;

        for(var t=0;t<x;t++)
        {
            var a="inputText"+t;

            var e=document.getElementById(a);
            var val= encodeURIComponent (e.value);

            arr[t]=val;
        }

        var attrstr = arr.join(',');

        window.location.assign("gt_Iba2?value="+attstr);
    }

P.S. In your loop (t!=x) will always be true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.