Question

I'm working on an HttpServlet and trying to define a url-pattern with a wildcard, but not finding much documentation.

The path I want to capture is "resource/{id}/action"

I've tried my annotation as:

@WebServlet("/resource/*/action")

but this doesn't match, though the more basic "resource/*" works okay.

Also, is there any way I can automatically pull out my {id} wildcard, rather than having to parse the url manually?

Was it helpful?

Solution

I'm think you try to solve wrong task. It's really unusual decision to map servlet on wildcard like this. Take a look on Spring MVC framework there you can write methods like this

@RequestMapping("/owners/{ownerId}/pets/{petId}", method=RequestMethod.GET)
public String findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
  Owner owner = ownerService.findOwner(ownderId);  
  Pet pet = owner.getPet(petId);  
  model.addAttribute("pet", pet);  
  return "displayPet"; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top