tags:

views:

25

answers:

1

What is the simplest way to use a variable in the URL in servlets.

Eg. http://somesite.com/MyServlet/[ID]

+1  A: 

That's called path info. You can use HttpServletRequest#getPathInfo() to grab it.

String pathInfo = request.getPathInfo(); // "/[ID]"

This however includes the leading slash. You may want to substring it away as follows:

String pathInfo = request.getPathInfo().substring(1); // "[ID]"

This assumes that your servlet is mapped on an url-pattern of /MyServlet/*.

BalusC
Nice BalusC, i never used this. When should i prefer this pathinfo over query string?
Ankit Rathod
@Nitesh: SEO friendly URL's.
BalusC
Ok Great! Thanks.
Ankit Rathod