views:

56

answers:

2

Say in my web.xml file, I define a servlet like so:

<url-pattern>/MyURL/*</url-pattern>

How do i access anything passed in the * in my servlet? I'm planning to use use this scheme for pretty(-ish) URLs.

+1  A: 

In the HttpServlet's doGet or doPost method you can use the getRequestURI method of the HttpServletRequest object to retrieve the path part of the URL. Since it sounds like you also want to chop off the part of the path that mapped to the serlvet could use the getServletPath method and then do something like this:

String path = request.getRequestURI();
if(path.startsWith(request.getServletPath())) {
    path = path.substring(request.getServletPath().length());
}
Kevin Loney
+1  A: 

The HttpServletRequest#getPathInfo() is exactly for this purpose.

String path = request.getPathInfo();

That's all. No need to substring the servlet path from it as suggested in another answer here. Also see my answer on your other question.

BalusC