Solved

How to get a substring from a URI

Posted on 2006-11-22
7
876 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error with Java/Cache JDBC Classpath 2 31
Java / Linux and Regular Expressions 11 71
Java Jpanels and Jframe 8 28
javap bin 2 28
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

920 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

16 Experts available now in Live!

Get 1:1 Help Now