Solved

Java parse URL params

Posted on 2008-10-25
6
1,124 Views
Last Modified: 2013-11-23
Hi,
Can someone tell me how I can parse Get values from a REST request like:
http://server.com/?&tag1=value1&tag2=value2...

There's gotta be a lib or something to let me break it down to an array and get something like:
value1 = array[tag1];
0
Comment
Question by:ecuguru
  • 2
  • 2
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Farzad Akbarnejad
ID: 22806250
Hello,
Here is sample code:

public class ExampleServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      String param1 = request.getParameter("tag1");
      // Now param1 holds value1
     }
}
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22806275
You can get a listing of parameter names OR if you use same parameter name like:

tag=value1&tag=value2&tag=value3

You can get an array using the getParameterValues, otherwise you have to get each param specifically like FarzadA has shown.

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html
0
 

Author Comment

by:ecuguru
ID: 22869536
My java app handles web requests, it's not a servlet.  
Is there a way to make this work without the servlet aspect?  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22869753
Maybe this will be more helpful for REST.

http://developer.yahoo.com/java/howto-parseRestJava.html

So are you getting the entire request as a string as shown in example?
0
 

Author Comment

by:ecuguru
ID: 22870827
Yep.. I'm getting a string that is:
URLString = "http://localhost/server?&value1=value1&value2=value2"
And I'd like to parse for the values..  I'm putting it into a new URL, but can't find a good parser lib yet.
0
 
LVL 14

Accepted Solution

by:
Farzad Akbarnejad earned 500 total points
ID: 22873961
Hello,
Maybe the following code will help you. Copy it into a class and run and test it. Post your comment if you have any question.

-FA

import java.util.HashMap;

import java.util.Map;

import java.util.StringTokenizer;
 

public class Main {

    public static void main(String[] args) {

        String URLString = "http://localhost/server?&tag1=value1&tag2=value2&tag3=value3";

        String valStr = URLString.substring(URLString.indexOf("?") + 1);

        StringTokenizer st = new StringTokenizer(valStr, "&");

        Map<String, String> arr = new HashMap();

        while (st.hasMoreTokens()) {

            String pair = st.nextToken();

            String lhs = pair.substring(0, pair.indexOf("="));

            String rhs  = pair.substring(pair.indexOf("=") + 1);

            arr.put(lhs, rhs);

        }

        

        System.out.println("tag1=" + arr.get("tag1"));

        System.out.println("tag2=" + arr.get("tag2"));

        System.out.println("tag3=" + arr.get("tag3"));

    }

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Free Alternative to JIRA 4 91
Unable to start eclipse ? 17 133
java.io.BufferedReader   readLine()  //returns the entire file ? 4 60
windows explorer path to command prompt 5 33
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now