2

i success encode & decode the url parameter but how can i get the parameter after decode?? the reason i encode all query strings into a parameter just to prevent user change the parameter on the address bar.

For example

Page A

  function fnlink()
    {
        param1 = encodeURIComponent("INSCODE=91&NAME=LEE&EMAIL=abc");
    url = "/home/test/test2.jsp?"+param1;

    location.href= url; 
    }

Page B

url : http://localhost:9080/home/test/test2.jsp?INSCODE%3D91%26NAME%3DLEE%26EMAIL%3Dabc

user3835327
  • 1,194
  • 1
  • 14
  • 44
  • 2
    Possible duplicate of [Parse a URI String into Name-Value Collection](http://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection) – Klitos Kyriacou Mar 06 '17 at 14:03
  • http://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection shows you how to use `URL.getQuery` to extract the query part and then use `String.split` to get the individual parts into a map. Then use the map's methods to check & get values for specific parameters. – Klitos Kyriacou Mar 06 '17 at 14:05
  • 1
    The method `getParameter` of request object returns the parameter you're looking already decoded. Not clear what's your problem. – freedev Mar 06 '17 at 14:05
  • @freedev `getParameter` is a method that the OP wished existed. – Klitos Kyriacou Mar 06 '17 at 14:07
  • Just so you are aware.. You are appending the QueryString parameters wrong. It should be: `request.getRequestURL() + "?" + request.getQueryString()`.. Note the question mark. – Brandon Mar 06 '17 at 14:17
  • ok simple.. what i want is how to get parameter inscode, name, email.. java 7 not 8 .. – user3835327 Mar 06 '17 at 15:07

1 Answers1

2

You should not encode the entire parameters string "INSCODE=91&NAME=LEE&EMAIL=abc" with encodeURIComponent.

Each parameter should be encoded separately. Use a Javascript function like this to add your parameters at query string:

/**
* Add a URL parameter 
* @param {url}   string  url 
* @param {param} string  the key to set
* @param {value} string  value 
*/
var addParam = function(url, param, value) {
   param = encodeURIComponent(param);
   var a = document.createElement('a');
   param += (value ? "=" + encodeURIComponent(value) : ""); 
   a.href = url;
   a.search += (a.search ? "&" : "") + param;
   return a.href;
}
Community
  • 1
  • 1
freedev
  • 25,946
  • 8
  • 108
  • 125