Solved

Java parse URL params

Posted on 2008-10-25
6
1,150 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 65
hashmap order 17 62
junit as external jar or library 7 58
DTD and JAVA versions 1 55
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

734 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