Title: How to do Url patterns in servlets web.xml
Tags: servlets;url-pattern;path-parameter
Question: I have a question on if this is possible to do. I have a profile servlet that has 2 url patterns, /profile and /profile/*  .So in my project I have a user use GET /profile to get all of the profiles in JSON. Each profile has a favorite team string. Also, I use /profile/* for getting specific profiles by doing /profile/2 for example. Then I have profileFavTeam servlet that I want to get all of the data of the profile favorite team. So I need to get the profiles ID so I want the url to be /profile/"ID"/favteam and this would return the teams statistics.
So for example, If the profile ID is 2 has a favorite team called Liverpool, and I entered /profile/2/favteam, it would show the stats of Liverpool
So would my url for profileFavTeam in web.xml look like this because i want the user to put a number in ID   /profile/*/favteam ?
But would this work if I have /profile/* url pattern in my profile servlet?
Comment: Is this for a new project or for reconfiguring a legacy project?


Here is another answer: This would not work. You basically haven path mapping (ending with /*) or extension mapping (beginning with *.). See section 12.2 of the Servlet spec
In  the Web application deployment  descriptor,  the  following  syntax  is  used to define mappings:
■ A string beginning with a  ‘/’  character and ending  with  a  ‘/*’  suffix  is  used  for path mapping.
■ A string beginning with a  ‘*.’  prefix is used  as  an extension mapping.
■ The  empty string  ("")  is  a special URL pattern that exactly maps  to the application's context root,  i.e.,  requests  of  the form http://host:port//.  In  this case  the path info  is  ’/’  and the  servlet  path and  context  path is empty string  (““).
■ A string  containing  only  the  ’/’  character indicates  the "default" servlet  of the application. In this case the  servlet path  is the request URI minus the context  path and the  path info is  null.
■ All other  strings are used for exact  matches  only.
If the  effective  web.xml  (after merging information from fragments and annotations) contains  any url-patterns  that  are mapped  to multiple servlets then the deployment  must fail.
You should implement the Front Controller pattern, with a single Servlet receiving all the requests, and delegate them to the processing objects according to the pattern.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:03/16/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<servlets><url-pattern><path-parameter>', 'Title': 'How to do Url patterns in servlets web.xml', 'ViewCount': 249, 'id': 60713372, 'user_AboutMe': None, 'user_AccountId': 17759873.0, 'user_DisplayName': 'Bobby.lock', 'user_Reputation': 59.0, 'user_Views': 26.0, 'user_id': 12896064.0}}