Solved

How to get a substring from a URI

Posted on 2006-11-22
7
874 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
7 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 100 total points
ID: 17999206
String[] x = request.getServletPath().split("/");
String name = x[x.length-1];
0
 
LVL 3

Assisted Solution

by:nishayr
nishayr earned 100 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 100 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

705 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

18 Experts available now in Live!

Get 1:1 Help Now