r/javahelp • • Nov 09 '20

Solved Extracting a query string from a url where the '?' is missing.

I have created a Java HTTPservlet in which I use reqeust.getQueryString to read the ? (=QueryString) like https://localhost:8080/homepage/?1234567 from a url of an existing application. The problem is the URL being sent from the application is without the ?, so I cannot distinguish between the Path and QueryString, like this:

https://.../somePage/1234567

Is there a way to extract the number from the url without using Regex? I do not have access to the code of the application that creates the url, so I can't get it to send it properly as a queryString. I hesitate to use Regex because there might be more parameters that need to be read and maintaining the regex will be a pain in the ass. Is there another workaround besides Regex?

17 Upvotes

8 comments sorted by

•

u/AutoModerator Nov 09 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

12

u/Raph0007 Nov 09 '20

I don't know the answer to this, but what I do know is that the thing you described is a common and "standard" thing in HTTP APIs. Instead of having http://localhost:8080/api?param1=value1, which is known as query param, you can have http://localhost:8080/api/param1. This version, which is accepted in OpenAPI definitions and supported across many HTTP frameworks, is called path param. So I suppose if you google this keyword in combination with the framework you use, you should get the desired answer.

I know that, for example, in Ktor, you can easily define path params by just including {curly braces} around the parameter in the mapping string:

get("/api/{param1}") {
    // "param1" is available through call.parameters["param1"]
}

I suppose it's gonna be somewhat similar for you.

5

u/sombreProgrammer Nov 09 '20

Something like this is exactly what I was looking for. Thank you!

5

u/RhoOfFeh Nov 09 '20

This is the most common way of building RESTful APIs.

The most common solution in Java these days is using Spring Boot with annotations to set up methods to be called for each endpoint. Spring does its magic and you wind inside a method with all the parameters pre-filled from the URL.

That being said, you should be able to do a String.split("/") on the URL and use the resulting array of values to get the parameters you need.

2

u/PFive Nov 09 '20

The answer to your question is a resounding Yes. There is a way to do this. These are called path variables (or path params), as opposed to query parameters (or querystring params). See this question for some discussion on how to take advantage of this in java: https://stackoverflow.com/questions/11552248/when-to-use-queryparam-vs-pathparam

2

u/wildjokers Nov 09 '20

Any Java web framework will parse URL's for you. What framework are you using?

2

u/astronautplutox86 Nov 09 '20

If you get the urls, can you split the the url's with "\" and return the last string ?

I am not sure what is the standard way to extract this , as this is the way I think most of the url's are structured at major companies with profiles.

1

u/Python4fun Nov 09 '20

If you know the url of your page then you just need to break at the end of it.