craftsman
craftsman

Reputation: 15653

how to create vanity url in java web applications?

If we have a person-directory application, where we can check the details of a specific person (for example, person with id 239) at

http://person-directory.com/detail.jsp?id=239

how can we create vanity urls in it? i.e. instead of typing the above url, we use

http://person-directory.com/julius

to open the detail page of person with id 239 and username julius.

Thanks

Umar

Upvotes: 2

Views: 1381

Answers (2)

marcospereira
marcospereira

Reputation: 12202

You could also try to use the url-rewrite filter: http://tuckey.org/urlrewrite/. Of course, you have to change your code to query by username instead of id.

Upvotes: 2

Brian Agnew
Brian Agnew

Reputation: 272357

I suspect you'll have to

  1. map a servlet to the root context (i.e. http://person-directory.com/)
  2. use the HttpServletRequest.getPathInfo() and extract the name from the URL your servlet has been called with
  3. use that name to do a lookup in your backend datasource

Note that a better URL may be http://person-directory/username/julius. Then you could map your UserServlet to http://person-directory/username and provide other servlets on other URLs.

Upvotes: 6

Related Questions