?
Solved

How to get a substring from a URI

Posted on 2006-11-22
7
Medium Priority
?
888 Views
Last Modified: 2010-05-18
I need to get a name from differnt URI's

The URI can be in theis formats

http://localhost:8080/chapter_admin/steve

or

http://localhost:8080/chapter_admin/steve?action=dsdsd&L=dasdas&P=dadsdasd&uid=ddasda

I am using this code

String chapter = request.getRequestURI().substring(15);

this code works fine with the first URL but not with the second, I only need "steve" from both these url's , would anyone know how to do this

Thanks
0
Comment
Question by:huzefaq
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17997338
0
 

Author Comment

by:huzefaq
ID: 17997436
no ths actually gets
/chapter_admin/alabama/
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 17999206
String[] x = request.getServletPath().split("/");
String name = x[x.length-1];
0
 
LVL 3

Assisted Solution

by:nishayr
nishayr earned 400 total points
ID: 18000474
Hey there,

You could also try

URL url = new URL(request.getRequestURI());

then you have access to all the individual components of the url using:

url.getHost(), url.getPort, url.getProtocol() etc....

Thanks,
Nish
0
 
LVL 2

Assisted Solution

by:glottis
glottis earned 400 total points
ID: 18001232
String chapter = request.getRequestURI();
int index = chapter.lastIndexOf("/");
chapter = chapter.substring(index);
index = chapter.indexOf("?");
if (index > 0)
  chapter = chapter.substring(0, index);
System.out.println(chapter);

Try that, maybe that would help you out.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

750 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