tags:

views:

39

answers:

1

I am building a site with JSPs and Servlets. How do I map a URL like this example.com/12345 so that I get the response as if the request was example.com/content.jsp?id=12345?

+3  A: 

Use an url-pattern of /*, gather the pathinfo by HttpServletRequest#getPathInfo() and finally forward the request to the desired destination by RequestDispatcher#forward().

Basic kickoff example (business logic and exception handling aside):

String pathInfo = request.getPathInfo();
String id = pathInfo.substring(1); // Get rid of trailing slash.
String newURL = String.format("/content.jsp?id=%d", id);
request.getRequestDispatcher(newURL).forward(request, response);

Alternatively, especially if actually no business logic is involved, you can also use the Tuckey's UrlRewriteFilter for this. This way you can rewrite your URL's the way as you would do with Apache HTTPD's well known mod_rewrite.

BalusC