Solved

How to get a substring from a URI

Posted on 2006-11-22
7
880 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
[X]
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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

691 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