Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java parse URL params

Posted on 2008-10-25
6
Medium Priority
?
1,200 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 60

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

879 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