tags:

views:

472

answers:

2

I want to parse a descriptive-style URL with slashes in Java (server/books/thrillers/johngrisham/thefirm).

How do I do this?

Edit: My overall idea is to handle the data I receive to do a lookup (therefore using the URL as search criteria) in a database and then return a html pages with data on it.

+3  A: 

Hi,

The split() method of String class can do the work, as commented Ionut before.

ATorras
+4  A: 
String urlToParse = "server/books/thrillers/johngrisham/thefirm";
String[] parsedURL = urlToParse.split("/");

What you will have is an array of strings that you can then work with.

// parsedURL[0] == "server";
// parsedURL[1] == "books";
// parsedURL[2] == "thrillers";
// parsedURL[3] == "johngrisham";
// parsedURL[4] == "thefirm";
Jeremy Cron
Is there any objects I have to handle first? I'd like to accept this URL via a Tomcat server.
day_trader
@myusername - Take a look at the URL class. There are different methods for retrieving specific parts of a URL.
Jeremy Cron