如何在java Web应用程序中创建虚荣URL?

cra*_*man 2 java url java-ee

如果我们有一个人员目录应用程序,我们可以在那里查看特定人员的详细信息(例如,ID为239的人)

http://person-directory.com/detail.jsp?id=239
Run Code Online (Sandbox Code Playgroud)

我们怎样才能在其中创建虚荣网址?即不是输入上面的网址,我们使用

http://person-directory.com/julius
Run Code Online (Sandbox Code Playgroud)

打开ID为239和用户名julius的人的详细信息页面.

谢谢

欧麦尔

Bri*_*new 6

我怀疑你必须这样做

  1. 将servlet映射到根上下文(即http://person-directory.com/)
  2. 使用HttpServletRequest.getPathInfo()并从您调用servlet的URL中提取名称
  3. 使用该名称在后端数据源中进行查找

请注意,更好的URL可能是http:// person-directory/username/julius.然后,您可以将UserServlet映射到http:// person-directory/username,并在其他URL上提供其他servlet.