[英]How to take query string from the request URL passed as string in java and ignore the rest of the URL
The URL "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd" is passed as a string to a Java file and from here I need to ignore till "?" The URL "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd" is passed as a string to a Java file and from here I need to ignore till "?" and store the rest as a string in java.Can anyone help me with this.
并将 rest 作为字符串存储在 java 中。任何人都可以帮我解决这个问题。
Regex is a good option or you can also manipulate URLs using java.net.URL
class:正则表达式是一个不错的选择,或者您也可以使用
java.net.URL
class 操作 URL:
URL url = new URL("http://" + "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd");
System.out.println(url.getPath()); // prints: /index.jsp
System.out.println(url.getQuery()); // prints: accountno=20&password=1234&username=abcd
Regex replacement would be one option here:正则表达式替换将是这里的一种选择:
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String query = url.replaceAll("^.*?(?:\\?|$)", "");
System.out.println(query);
This prints:这打印:
accountno=20&password=1234&username=abcd
public class Main {
public static void main(String[] args) {
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String paramsStr = url.substring(url.indexOf("?") + 1);
System.out.println(paramsStr);
}
}
Output: Output:
accountno=20&password=1234&username=abcd
You can use .split()
您可以使用
.split()
String url = "localhost:9005/index.jsp?accountno=20&password=1234&username=abcd";
String queryString = url.split("\\?")[1];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.