Anuj Balan
Anuj Balan

Reputation: 7729

Special characters being removed automatically from request

I am setting notes(String) which contains a mix of special char's, number, alphabets, etc, in a hidden variable notesValue, and opening a new jsp(nextPage).

var Url='<%=request.getContextPath()%>/nextPage.jsp?notesValue='+document.forms[0].notesValue.value;

var Bars = 'directories=no, location=no,menubar=no,status=no,titlebar=no';
var Options='scrollbar=yes,width=350,height=200,resizable=no';
var Features=Bars+' '+Options;
alert("Url being sent ---> "+Url);
var Win=open(Url,'Doc5',Features);

At nextPage.jsp side, am doing:

String notes = request.getParameter("notesValue");

Here am getting only plain text and all the special characters are gone.

For eg: notesValue which I set was: "New Notes &';>/..."

What I received on nextPage.jsp is : New Notes\

What's going on ?

Upvotes: 1

Views: 1536

Answers (2)

Suresh Atta
Suresh Atta

Reputation: 121998

Encode the string result

encodeURIComponent(document.forms[0].notesValue.value);

No need to add any plugin.Its built in function.

Upvotes: 1

Abubakkar
Abubakkar

Reputation: 15644

When you are sending data using this method :

nextPage.jsp?notesValue='+document.forms[0].notesValue.value;

You are using GET method for sending the data as you are appending the data with the URL. But there are many special characters invloved have special meaning in the URL (like &,>,<,;, etc.)

When these characters are not used in their special role inside a URL, they need to be encoded

so you need to encode them first if you need to send them as values o that they are not considered as special URL characters.

Here's a list of URL character encoding

You can use POST method instead to avoid this.

Also read this: How to encode URL in javascript

Upvotes: 1

Related Questions