0

Actually I am a student and a beginner I am making a online wish webpage as a test project And I want to display the sender's name in html I have successfully completed with that but I want to get the URL string parameter text How can I do it

My website address pattern is like www.subdomain.domain.tld/page/?n=(sender_encoded_name)

So I want to fetch data from n parameter of the URL and decode it.

I had searched alot online regarding this and got confused So please help me with the actual method

I tried using this code,

var reqParam = URLDecoder.decode(reqParam, "UTF-8")

but I am a little bit confused do please help me as necessary...

Akshay Garg
  • 1,010
  • 8
  • 15

3 Answers3

0

You can try it for getting query parameter values-

var urlObj = new URL("http://www.subdomain.domain.tld/page/?n=a%20b%20c&m=a%20b%20c%20d");
var params = urlObj.searchParams;
params.get('n');
params.get('m');
Akshay Garg
  • 1,010
  • 8
  • 15
0

In Java you can use Pattern Matching with a Regular Expression to gather the data you want from the URL String (or any string for that matter):

String urlString = "www.subdomain.domain.tld/page/?n=(sender_encoded_name)";

List<String> list = new ArrayList<>();
String regexString = "\\Q?n=(\\E(?s)(.*?)\\Q)\\E";
Pattern pattern = Pattern.compile("(?iu)" + regexString);
Matcher matcher = pattern.matcher(urlString);
while (matcher.find()) {
    String match = matcher.group(1).trim();
    list.add(match);
}

// Display all items found matching the supplied pattern
for(int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
}

Console output will be:

sender_encoded_name

To put this into a method you might have:

public List<String> getEncodedName(String urlString) {
    List<String> list = new ArrayList<>();
    String regexString = "\\Q?n=(\\E(?s)(.*?)\\Q)\\E";
    Pattern pattern = Pattern.compile("(?iu)" + regexString);
    Matcher matcher = pattern.matcher(urlString);
    while (matcher.find()) {
        String match = matcher.group(1).trim();
        list.add(match);
    }
    return list;
}

And to use it:

String urlString = "www.subdomain.domain.tld/page/?n=(sender_encoded_name)";
List<String> list = getEncodedName(urlString);

// Display all items found matching the supplied pattern
for(int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
}

Console output will be:

sender_encoded_name

Regular Expression Explanation:

enter image description here

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22
-1

I think, you will find interesting this page:

https://api.jquery.com/jquery.get/

An example that maybe can help you in PHP:

echo 'Hello ' . htmlspecialchars($_GET['name' ]) . '!';

Imagine that your URL is http://example.com/?name=Andrew

Then, you will see something like: Hello Andrew!