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. inst开发者_如何学Pythonead 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
I suspect you'll have to
- map a servlet to the root context (i.e. http://person-directory.com/)
- use the HttpServletRequest.getPathInfo() and extract the name from the URL your servlet has been called with
- 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.
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.
精彩评论