Marshal
Marshal

Reputation: 261

How to get http parameters using java?

I'm having a solr query "http://localhost:8080/solr/select/?q=A".I need to read the parameter "q" in my java code.How can I get this?

Thanks, Marshal

Upvotes: 1

Views: 2756

Answers (3)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74800

If you have the query in URL form, you can use URL.getQuery(), and then would have to split the string at = and & to find the right element, like this:

public String getQueryPart(URL url, String key) {
    String query = url.getQuery();
    if(query == null)
       return null;
    String[] parts = query.split("[&=]");
    for(int i = 0; i < parts.length; i+=2) {
       if(parts[i].equals(key)) {
          return parts[i+1];
       }
    }
}

If you want to query multiple parameters, better store the split String once in a map and query this multiple times.

And of course, if you are using this in a servlet or similar server-side code called by this exactly URL, there are better ways to get the parameters in the servlet API (like Deepak wrote).

Upvotes: 1

Marko Bonaci
Marko Bonaci

Reputation: 5716

Depends on you context.
From where are you trying to get this param? How are you using Solr exactly?

BTW, /select is simply a part of a URL used to map requests to the main Solr servlet called (wait for it...) SolrServlet - org.apache.solr.servlet.SolrServlet (deprecated from v4, now solr.StandardRequestHandler is used), which is, due to the fact that it handles search requests, also sometimes referred to as Solr Server.

Upvotes: 0

Deepak
Deepak

Reputation: 2102

request.getParameter("q"); would do the trick.

Upvotes: 7

Related Questions