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
Solved

Java parse URL params

Posted on 2008-10-25
6
1,139 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 47
null output 3 35
Java: The Public Class Main 4 30
Selenium docs api java index 3 48
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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:
This video teaches viewers about errors in exception handling.

839 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