Solved

Java parse URL params

Posted on 2008-10-25
6
1,119 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

13 Experts available now in Live!

Get 1:1 Help Now