Java gets parameters on the link
In Java, we often need to get parameters from URL links, such as getting parameters in HTTP requests in web development. This article will introduce several common methods to implement Java to obtain parameters on the link.
Use class
Java providesClass to handle URL links. We can use the method of this class to get the parameters on the link.
javaCopy code import ; import ; import ; import ; public class URLParamsExample { public static void main(String[] args) { try { String urlString = "/?name=John&age=25&city=New%20York"; URL url = new URL(urlString); String query = (); String[] params = ("&"); Map<String, String> paramMap = new HashMap<>(); for (String param : params) { String[] keyValue = ("="); String key = (keyValue[0], "UTF-8"); String value = (keyValue[1], "UTF-8"); (key, value); } // Get parameter values String name = ("name"); String age = ("age"); String city = ("city"); ("name: " + name); ("age: " + age); ("city: " + city); } catch (Exception e) { (); } } }
In the above example, we first create aURLobject, then usegetQuery()Method gets the query part in the link. Next, we extract the parameters and corresponding values through string segmentation and decoding, and store them in oneHashMapmiddle. Finally, we useget()Method fromHashMapGet the parameter value in
Use class
Apart fromURLClass, Java also providesClasses to parse and manipulate URIs. We can use the method of this class to get the parameters on the link.
javaCopy code import ; import ; import ; import ; public class URIParamsExample { public static void main(String[] args) { try { String urlString = "/?name=John&age=25&city=New%20York"; URI uri = new URI(urlString); String query = (); String[] params = ("&"); Map<String, String> paramMap = new HashMap<>(); for (String param : params) { String[] keyValue = ("="); String key = (keyValue[0], "UTF-8"); String value = (keyValue[1], "UTF-8"); (key, value); } // Get parameter values String name = ("name"); String age = ("age"); String city = ("city"); ("name: " + name); ("age: " + age); ("city: " + city); } catch (Exception e) { (); } } }
In the above example, we create aURIobject, then usegetQuery()The method gets the query part in the link and parses the parameters and corresponding values in a similar way.
Using third-party libraries
In addition to the Java standard library, there are also some third-party libraries that can simplify the operation of obtaining link parameters. For example, commonly used libraries include Apache HttpComponents and Spring frameworksUriComponentsBuilderClass, etc. Sample code using the Apache HttpComponents library:
javaCopy code import ; import ; import ; import ; import ; import ; public class ApacheHttpParamsExample { public static void main(String[] args) { try { String urlString = "/?name=John&age=25&city=New%20York"; URI uri = new URI(urlString); List<NameValuePair> params = (uri, StandardCharsets.UTF_8); // Get parameter values String name = null; String age = null; String city = null; for (NameValuePair param : params) { if (().equals("name")) { name = (); } else if (().equals("age")) { age = (); } else if (().equals("city")) { city = (); } } ("name: " + name); ("age: " + age); ("city: " + city); } catch (Exception e) { (); } } }
In the above example, we use the Apache HttpComponents library()Method to parse parameters in URL links and store them inNameValuePairin the object list. The above are several common methods to implement Java to obtain parameters on the link. According to actual needs, select the appropriate method to parse the URL link and obtain the parameter value. Hope this article can be helpful to you!
It is a class provided by Java for handling unified resource locators (URLs). URLs are strings used to identify and locate resources on the Internet, such as web pages, pictures, audio, etc.Classes provide a range of methods to parse, build, and process URLs. The following isURLSome common methods and functions of the class:
- Create URL object: can be usedURLA class constructor to create a URL object, for example:
javaCopy code URL url = new URL("");
- Get various parts of the URL: Can be usedURLClass methods to obtain various parts of the URL, such as protocol, host, port, path, etc., for example:
javaCopy code String protocol = (); String host = (); int port = (); String path = ();
- URL encoding and decoding: Can be usedURLStatic method of class()and()To encode and decode URLs to handle special characters and spaces in URLs, for example:
javaCopy code String encodedURL = ("", "UTF-8"); String decodedURL = ("http%3A%2F%", "UTF-8");
- Get the content of the URL: You can useURLClassicopenStream()Methods open the URL's input stream to read the content pointed to by the URL, for example:
javaCopy code InputStream inputStream = (); // Read data from the input stream here
- Connect, download and upload files: UseURLClassicopenConnection()Methods can be obtainedURLConnectionObject, thereby performing more advanced operations, such as establishing connections, downloading files, uploading files, etc. The above is justSome basic usages of classes,URLThe class also provides some other methods and functions, such as determining whether the URL is equal, obtaining the default port, etc. Appropriate methods and functions need to be selected and used according to specific needs and usage scenarios.
Summarize
This is the article about getting the parameters on Java links. For more related content on Java links, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!