1

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 ?

Anuj Balan
  • 7,629
  • 23
  • 58
  • 92

2 Answers2

1

Encode the string result

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

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Using 'encodeURIComponent' did the trick. Thanks a lot. One doubt, I am just calling a jsp (with window.open) from javascript of other jsp. Where is Get and Post involved in this ? How can I convert this to POST ? – Anuj Balan May 09 '13 at 06:02
  • I misunderstood.You are not using any form right?? So encode data is enough. – Suresh Atta May 09 '13 at 06:05
  • Yes. If I am calling any servlet, I usually use method="post" – Anuj Balan May 09 '13 at 06:06
  • If you use html forms then GET and POST comes in to picture.As of now they are not related to your line,As you are directlly triggering the url – Suresh Atta May 09 '13 at 06:06
1

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

Community
  • 1
  • 1
Abubakkar
  • 15,488
  • 8
  • 55
  • 83