Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a URL in Java

Tags:

java

php

I am looking for an equivalent to PHP's "parse_url" function in Java. I am not running in Tomcat. I have query strings saved in a database that I'm trying to break apart into individual parameters. I'm working inside of Pentaho, so I only have the Java SE classes to work with. I know I could write a plugin or something, but if I'm going to do all that I'll just write the script in PHP and be done with it.

TLDR: Looking for a Java standard class/function that takes a String and spits out an array of parameters.

Thanks,

Roger

like image 681
Roger W. Avatar asked Feb 12 '26 19:02

Roger W.


1 Answers

You can accomplish that using java.net.URL:

URL url = new URL("http://hostname:port/path?arg=value#anchor");
String protocol = url.getProtocol(); // http
String host = url.getHost(); // hostname
String path = url.getPath(); // /path
int port = url.getPort(); // port
String query = url.getQuery(); // arg=value
String ref = url.getRef(); // anchor
like image 194
Eng.Fouad Avatar answered Feb 15 '26 08:02

Eng.Fouad